golang-error-handling
npx machina-cli add skill saifoelloh/golang-best-practices-skill/error-handling --openclawGolang Error Handling
Expert-level error handling review for Go applications. Ensures proper error wrapping, context propagation, and error checking that preserves error chains and enables proper timeout/cancellation handling.
When to Apply
Use this skill when:
- Reviewing error propagation patterns
- Checking context usage and cancellation
- Auditing error wrapping with %w vs %v
- Investigating timeout or cancellation issues
- Verifying nil checks for interfaces
- Ensuring errors.Is/As usage
Rule Categories by Priority
| Priority | Count | Focus |
|---|---|---|
| Critical | 3 | Prevents error chain loss, context leaks |
| High | 3 | Correctness and reliability |
| Medium | 1 | Code quality |
Rules Covered (7 total)
Critical Issues (3)
critical-error-wrapping- Use %w for error wrapping, not %vcritical-error-shadow- Don't shadow err variable in nested scopescritical-context-leak- Always defer cancel() after context creation
High-Impact Patterns (3)
high-context-propagation- Propagate context through call chainhigh-error-is-as- Use errors.Is/As instead of ==high-interface-nil- Check interface nil correctly
Medium Improvements (1)
medium-sentinel-error-usage- Use sentinel errors for stable categories
How to Use
For Error Handling Review
- Scan code for error handling patterns
- Check against rules in priority order (Critical first)
- Verify error wrapping preserves error chains
- Ensure context propagation for timeouts/cancellation
- Check for proper nil interface handling
Common Patterns
Error chain review:
Check this code for proper error wrapping
Context propagation audit:
Verify context is propagated correctly
Error checking patterns:
Review error handling for wrapped errors
Trigger Phrases
This skill activates when you say:
- "Review error handling"
- "Check error wrapping"
- "Verify context propagation"
- "Check for context leaks"
- "Review error chains"
- "Audit error patterns"
Output Format
## Critical Error Handling Issues: X
### [Rule Name] (Line Y)
**Issue**: Brief description
**Impact**: Lost error chain / Context leak / Wrong error check
**Fix**: Suggested correction
**Example**:
```go
// Corrected code
Related Skills
- golang-concurrency-safety - For goroutine context patterns
- golang-clean-architecture - For error handling across layers
Philosophy
Based on Go best practices:
- Error chains are valuable - Use %w to preserve error context
- Context is essential - Always propagate for cancellation/timeout
- Errors are values - Use errors.Is/As for wrapped error checking
- Nil interfaces are tricky - Check correctly to avoid bugs
Notes
- Focus on error handling correctness
- All examples show proper error wrapping patterns
- Context propagation is critical for production systems
- Related to concurrency but focused on error semantics
Source
git clone https://github.com/saifoelloh/golang-best-practices-skill/blob/main/error-handling/SKILL.mdView on GitHub Overview
Expert-level Go error handling review focusing on proper wrapping, context propagation, and nil checks. It helps preserve error chains, ensure correct context usage, and improves error detection with errors.Is/As.
How This Skill Works
The skill scans Go code for error handling patterns, prioritizes issues by critical/high/medium rules, and suggests concrete fixes to preserve error chains using %w, propagate context, and avoid nil interface pitfalls. It provides examples and quick-start steps to apply changes.
When to Use It
- Reviewing error propagation patterns
- Checking context usage and cancellation
- Auditing error wrapping with %w vs %v
- Verifying errors.Is/As usage
- Investigating timeouts or cancellation issues
Quick Start
- Step 1: Scan code for error handling patterns and %w usage
- Step 2: Replace incorrect %v wraps with %w and add errors.Is checks
- Step 3: Ensure context propagation and deferred cancel() are implemented
Best Practices
- Use %w for wrapping, not %v
- Avoid shadowing the err variable in nested scopes
- Always defer cancel() after context creation
- Propagate context through the call chain
- Use errors.Is/As for wrapped error checks
Example Use Cases
- Wrap errors with %w to preserve chain and inspect with errors.Is
- Propagate context across API boundaries to ensure timeouts are honored
- Check interface nil correctly to avoid nil interface bugs
- Use sentinel errors for stable categories and errors.Is
- Avoid shadowing variables when handling errors in nested scopes