Skillshub golang-error-handling
Idiomatic Golang error handling — creation, wrapping with %w, errors.Is/As, errors.Join, custom error types, sentinel errors, panic/recover, the single handling rule, structured logging with slog, HTTP request logging middleware, and samber/oops for production errors. Built to make logs usable at scale with log aggregation 3rd-party tools. Apply when creating, wrapping, inspecting, or logging errors in Go code.
git clone https://github.com/ComeOnOliver/skillshub
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/Harmeet10000/skills/golang-error-handling" ~/.claude/skills/comeonoliver-skillshub-golang-error-handling && rm -rf "$T"
skills/Harmeet10000/skills/golang-error-handling/SKILL.mdPersona: You are a Go reliability engineer. You treat every error as an event that must either be handled or propagated with context — silent failures and duplicate logs are equally unacceptable.
Modes:
- Coding mode — writing new error handling code. Follow the best practices sequentially; optionally launch a background sub-agent to grep for violations in adjacent code (swallowed errors, log-and-return pairs) without blocking the main implementation.
- Review mode — reviewing a PR's error handling changes. Focus on the diff: check for swallowed errors, missing wrapping context, log-and-return pairs, and panic misuse. Sequential.
- Audit mode — auditing existing error handling across a codebase. Use up to 5 parallel sub-agents, each targeting an independent category (creation, wrapping, single-handling rule, panic/recover, structured logging).
Community default. A company skill that explicitly supersedes
skill takes precedence.samber/cc-skills-golang@golang-error-handling
Go Error Handling Best Practices
This skill guides the creation of robust, idiomatic error handling in Go applications. Follow these principles to write maintainable, debuggable, and production-ready error code.
Best Practices Summary
- Returned errors MUST always be checked — NEVER discard with
_ - Errors MUST be wrapped with context using
fmt.Errorf("{context}: %w", err) - Error strings MUST be lowercase, without trailing punctuation
- Use
internally,%w
at system boundaries to control error chain exposure%v - MUST use
anderrors.Is
instead of direct comparison or type assertionerrors.As - SHOULD use
(Go 1.20+) to combine independent errorserrors.Join - Errors MUST be either logged OR returned, NEVER both (single handling rule)
- Use sentinel errors for expected conditions, custom types for carrying data
- NEVER use
for expected error conditions — reserve for truly unrecoverable statespanic - SHOULD use
(Go 1.21+) for structured error logging — notslog
orfmt.Printlnlog.Printf - Use
for production errors needing stack traces, user/tenant context, or structured attributessamber/oops - Log HTTP requests with structured middleware capturing method, path, status, and duration
- Use log levels to indicate error severity
- Never expose technical errors to users — translate internal errors to user-friendly messages, log technical details separately
- Keep error messages low-cardinality — don't interpolate variable data (IDs, paths, line numbers) into error strings; attach them as structured attributes instead (via
at the log site, or viaslogsamber/oops
on the error itself) so APM/log aggregators (Datadog, Loki, Sentry) can group errors properly.With()
Detailed Reference
-
Error Creation — How to create errors that tell the story: error messages should be lowercase, no punctuation, and describe what happened without prescribing action. Covers sentinel errors (one-time preallocation for performance), custom error types (for carrying rich context), and the decision table for which to use when.
-
Error Wrapping and Inspection — Why
beatsfmt.Errorf("{context}: %w", err)
(chains vs concatenation). How to inspect chains withfmt.Errorf("{context}: %v", err)
/errors.Is
for type-safe error handling, anderrors.As
for combining independent errors.errors.Join -
Error Handling Patterns and Logging — The single handling rule: errors are either logged OR returned, NEVER both (prevents duplicate logs cluttering aggregators). Panic/recover design,
for production errors, andsamber/oops
structured logging integration for APM tools.slog
Parallelizing Error Handling Audits
When auditing error handling across a large codebase, use up to 5 parallel sub-agents (via the Agent tool) — each targets an independent error category:
- Sub-agent 1: Error creation — validate
/errors.New
usage, low-cardinality messages, custom typesfmt.Errorf - Sub-agent 2: Error wrapping — audit
vs%w
, verify%v
/errors.Is
patternserrors.As - Sub-agent 3: Single handling rule — find log-and-return violations, swallowed errors, discarded errors (
)_ - Sub-agent 4: Panic/recover — audit
usage, verify recovery at goroutine boundariespanic - Sub-agent 5: Structured logging — verify
usage at error sites, check for PII in error messagesslog
Cross-References
- → See
for full samber/oops API, builder patterns, and logger integrationsamber/cc-skills-golang@golang-samber-oops - → See
for structured logging setup, log levels, and request logging middlewaresamber/cc-skills-golang@golang-observability - → See
for nil interface trap and nil error comparison pitfallssamber/cc-skills-golang@golang-safety - → See
for error naming conventions (ErrNotFound, PathError)samber/cc-skills-golang@golang-naming