$npx -y skills add samber/cc-skills-golang --skill golang-error-handlingIdiomatic 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 ma
| 1 | **Persona:** 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. |
| 2 | |
| 3 | **Orchestration mode:** Use `ultracode` for auditing error handling across a large codebase — orchestrate the five category sub-agents described in the "Parallelizing Error Handling Audits" section (creation, wrapping, single-handling rule, panic/recover, structured logging) and consolidate their findings. |
| 4 | |
| 5 | **Modes:** |
| 6 | |
| 7 | - **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. |
| 8 | - **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. |
| 9 | - **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). |
| 10 | |
| 11 | > **Community default.** A company skill that explicitly supersedes `samber/cc-skills-golang@golang-error-handling` skill takes precedence. |
| 12 | |
| 13 | # Go Error Handling Best Practices |
| 14 | |
| 15 | 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. |
| 16 | |
| 17 | ## Best Practices Summary |
| 18 | |
| 19 | 1. **Returned errors MUST always be checked** — NEVER discard with `_` |
| 20 | 2. **Errors MUST be wrapped with context** using `fmt.Errorf("{context}: %w", err)` |
| 21 | 3. **Error strings MUST be lowercase**, without trailing punctuation |
| 22 | 4. **Use `%w` internally, `%v` at system boundaries** to control error chain exposure |
| 23 | 5. **MUST use `errors.Is` for sentinel matching and `errors.As`/`errors.AsType` for typed chain inspection** instead of direct comparison or bare type assertions. For Go 1.26+, prefer `errors.AsType[T](err)` when `T` implements `error`; use `errors.As(err, &target)` for Go <1.26 or for non-error interface targets. |
| 24 | 6. **SHOULD use `errors.Join`** (Go 1.20+) to combine independent errors |
| 25 | 7. **Errors MUST be either logged OR returned**, NEVER both (single handling rule) |
| 26 | 8. **Use sentinel errors** for expected conditions, custom types for carrying data |
| 27 | 9. **NEVER use `panic` for expected error conditions** — reserve for truly unrecoverable states |
| 28 | 10. **SHOULD use `slog`** (Go 1.21+) for structured error logging — not `fmt.Println` or `log.Printf` |
| 29 | 11. **Use `samber/oops`** for production errors needing stack traces, user/tenant context, or structured attributes |
| 30 | 12. **Log HTTP requests** with structured middleware capturing method, path, status, and duration |
| 31 | 13. **Use log levels** to indicate error severity |
| 32 | 14. **Never expose technical errors to users** — translate internal errors to user-friendly messages, log technical details separately |
| 33 | 15. **Keep log grouping low-cardinality** — at logging/APM boundaries, keep message templates stable and attach IDs, paths, line numbers, and counts as structured attributes. Error values may include useful operational context, but avoid putting high-cardinality data into the stable log message used for grouping. |
| 34 | |
| 35 | ## Detailed Reference |
| 36 | |
| 37 | - **[Error Creation](./references/error-creation.md)** — 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. |
| 38 | |
| 39 | - **[Error Wrapping and Inspection](./references/error-wrapping.md)** — Why `fmt.Errorf("{context}: %w", err)` beats `fmt.Errorf("{context}: %v", err)` (chains vs concatenation). How to inspect chains with `errors. |