Agent skill
go-error-checking
Type-safe error inspection using errors.Is and errors.As
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/testing/go-error-checking-jamesprial-claudefiles
SKILL.md
Error Checking
Pattern
Use errors.Is to check sentinel errors and errors.As to extract error types.
errors.Is - Check Sentinel Errors
// CORRECT
if errors.Is(err, user.ErrNotFound) {
return http.StatusNotFound
}
// WRONG - breaks with wrapped errors
if err == user.ErrNotFound { }
errors.As - Extract Error Types
// CORRECT - extracts specific error type
var pathErr *fs.PathError
if errors.As(err, &pathErr) {
log.Printf("failed path: %s", pathErr.Path)
log.Printf("operation: %s", pathErr.Op)
}
// WRONG - only checks direct type
if pathErr, ok := err.(*fs.PathError); ok { }
Complete Example
func HandleError(err error) {
// Check sentinel errors
if errors.Is(err, user.ErrNotFound) {
fmt.Println("User not found")
return
}
// Extract custom error types
var validationErr *ValidationError
if errors.As(err, &validationErr) {
fmt.Printf("Validation failed: %v\n", validationErr.Fields)
return
}
// Extract standard library errors
var pathErr *fs.PathError
if errors.As(err, &pathErr) {
fmt.Printf("File operation failed: %s\n", pathErr.Path)
return
}
// Generic error
fmt.Printf("Unknown error: %v\n", err)
}
Key Differences
errors.Is: Checks if error matches a sentinel value (works through wrapping)errors.As: Extracts error of specific type (works through wrapping)- Never use
==or type assertions for wrapped errors
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?