skills/golang-error-handling/SKILL.md
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. For samber/oops specifics → See `golang-samber-oops` skill; for slog handler ecosystem → See `golang-samber-slog` skill.
npx skillsauth add rockcookies/skills golang-error-handlingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
Modes:
Community default. A company skill that explicitly supersedes
golang-error-handlingskill takes precedence.
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.
_fmt.Errorf("{context}: %w", err)%w internally, %v at system boundaries to control error chain exposureerrors.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.errors.Join (Go 1.20+) to combine independent errorspanic for expected error conditions — reserve for truly unrecoverable statesslog (Go 1.21+) for structured error logging — not fmt.Println or log.Printfsamber/oops for production errors needing stack traces, user/tenant context, or structured attributesError 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 fmt.Errorf("{context}: %w", err) beats fmt.Errorf("{context}: %v", err) (chains vs concatenation). How to inspect chains with errors.Is, errors.As, and Go 1.26+ errors.AsType for type-safe error handling, and errors.Join for combining independent errors.
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, samber/oops for production errors, and slog structured logging integration for APM tools.
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:
errors.New/fmt.Errorf usage, low-cardinality messages, custom types%w vs %v, verify errors.Is/errors.As patterns_)panic usage, verify recovery at goroutine boundariesslog usage at error sites, check for PII in error messagesgolang-samber-oops for full samber/oops API, builder patterns, and logger integrationgolang-observability for structured logging setup, log levels, and request logging middlewaregolang-safety for nil interface trap and nil error comparison pitfallsgolang-naming for error naming conventions (ErrNotFound, PathError)golang-continuous-integration skill for automated AI-driven code review in CI using these guidelinesdata-ai
JS/TS 异步与并发——Promise、`async/await`、并发上限(`Promise.all`/`allSettled`/`p-limit`)、错误传播、取消(`AbortController`/`AbortSignal`)、超时、背压、`worker_threads`、事件循环模型。
development
Pinia stores, state management patterns, store setup, and reactivity with stores.
tools
JSX syntax in Vue (e.g., class vs className, JSX plugin config).
development
Golang skills orchestrator — always active on any Golang coding, review, debug, or setup task. Reads the task context and loads the most relevant skills from samber/cc-skills-golang, often multiple at once: writing a gRPC service loads golang-grpc + golang-testing + golang-error-handling; debugging a panic loads golang-troubleshooting + golang-safety; auditing security loads golang-security + golang-lint + golang-safety. Also: disambiguates competing clusters when two skills seem to overlap (performance vs benchmark vs troubleshooting, samber/lo vs mo vs ro, DI cluster, safety vs security), and configures CLAUDE.md or AGENTS.md to force-trigger skills in a project (/golang-how-to configure).