Agent skill
go-error-wrapping
Wrap errors with context using fmt.Errorf %w pattern
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/development/go-error-wrapping
SKILL.md
Error Wrapping with %w
Pattern
Use fmt.Errorf with %w to add context while preserving the error chain.
CORRECT
func ReadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config %s: %w", path, err)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parse config %s: %w", path, err)
}
return &cfg, nil
}
WRONG
// Bad: Loses error chain
return nil, fmt.Errorf("read config failed: %v", err)
// Bad: No context
return nil, err
// Bad: String concatenation
return nil, errors.New("read config: " + err.Error())
Key Rules
- Use
%wto wrap, not%vor%s - Add meaningful context (file names, IDs, operations)
- Keep messages lowercase, no punctuation
- Preserve original error for type checking
Example Chain
// Error flows up with context at each layer:
// "process user 123: read config /etc/app.conf: open /etc/app.conf: no such file or directory"
func ProcessUser(id int) error {
cfg, err := ReadConfig("/etc/app.conf")
if err != nil {
return fmt.Errorf("process user %d: %w", id, err)
}
// use cfg...
return nil
}
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?