golang-error-handling

Implements idiomatic Go error handling with wrapping, inspection, and structured logging.

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-error-handling-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-error-handling
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-error-handling
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-error-handling-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires github.com/samber/oops, and includes references (resource) components.

What problem does it solve? Go codebases often suffer from swallowed errors, duplicate log entries, and high-cardinality error messages that break log aggregation and alerting. This Skill enforces idiomatic error handling patterns so errors are created, wrapped, inspected, and logged correctly for production systems. ## Core Features & Use Cases - Error Creation and Wrapping: Guides sentinel errors, custom error types, fmt.Errorf with %w vs %v, and lowercase error string conventions. - Error Inspection and Combination: Covers errors.Is, errors.As, Go 1.26+ errors.AsType, and errors.Join for combining independent failures. - Production Logging: Enforces the single handling rule (log OR return, never both), slog structured logging, low-cardinality messages, and samber/oops for stack traces and structured attributes. - Use Case: When writing a Go HTTP service, apply this Skill to wrap repository errors with context, translate them at API boundaries with %v, and log once at the handler with structured slog attributes. ## Quick Start Review the error handling in my Go service and fix any swallowed errors, log-and-return violations, or missing error wrapping.

Frequently Asked Questions about golang-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I wrap errors in Go with context?

Use fmt.Errorf with the %w verb to wrap errors while preserving the chain, for example fmt.Errorf("fetching user: %w", err). Use %v instead of %w at public API boundaries to prevent callers from unwrapping internal error types.

What is the difference between errors.Is and errors.As in Go?

errors.Is matches an error against a sentinel value anywhere in the wrapped chain, while errors.As extracts a specific typed error from the chain. For Go 1.26+, errors.AsType[T] offers simpler generic syntax for type extraction.

Should I log an error or return it in Go?

Handle each error exactly once: either log it or return it, never both. Logging and returning causes duplicate entries at every layer, cluttering log aggregation tools. Return with context and let the top-level handler log it.

When should I use samber/oops instead of the standard errors package?

Use samber/oops for production errors needing stack traces, user or tenant context, error codes, or structured attributes. It is a drop-in replacement compatible with errors.Is and errors.As, keeping variable data out of low-cardinality log messages.

How do I combine multiple errors in Go?

Use errors.Join (Go 1.20+) to combine independent errors, such as validation failures across multiple fields or shutdown errors from multiple resources. The joined error remains inspectable with errors.Is and errors.As for each inner error.

When is it acceptable to use panic in Go?

Panic is only acceptable for truly unrecoverable states like programmer errors or invalid initialization, never for expected failures such as network timeouts. Recover from panics with deferred functions at goroutine boundaries like HTTP handlers.