go-error-handling

Implement Go error handling with wrapping, unwrapping, and sentinel errors.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill go-error-handling
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-error-handling
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-go/skills/go-error-handling
Command: npx skills add https://github.com/TheBushidoCollective/han --skill go-error-handling

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Provides robust patterns for errors in Go, including wrapping, sentinel errors, custom error types, and best practices with errors package.

Core Features & Use Cases

  • Error Wrapping: Wrap context with errors for better debugging.
  • Sentinel Errors: Define and compare custom error values.
  • Custom Error Types: Create structured error types with additional context.

Quick Start

Implement a function that returns a wrapped error using fmt.Errorf("...%w...") and demonstrate error inspection with errors.Is/As.

Frequently Asked Questions about go-error-handling

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

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

Wrap errors with context using fmt.Errorf with the %w verb: fmt.Errorf("operation failed: %w", err). This preserves the original error for inspection with errors.Is and errors.As while adding descriptive context at each function boundary.

What are sentinel errors and when should I use them?

Sentinel errors are predefined error values you compare against using errors.Is to signal specific failure modes. Define them as package-level variables (e.g., var ErrNotFound = errors.New("not found")) to enable callers to handle particular error types without parsing error messages.

How do I create custom error types in Go?

Define a custom type implementing the error interface by providing an Error() string method. Include fields for context data, then create instances with all relevant information so callers can extract details using errors.As for type assertion and field access.

How do I inspect and handle specific errors in Go?

Use errors.Is to compare against sentinel errors and errors.As to extract custom error types by their struct type. Both functions unwrap error chains automatically, so they work with wrapped errors returned from nested function calls.

When should I use panic and recover for error handling?

Reserve panic and recover for truly unrecoverable conditions or programming errors, not expected failures. Most application errors should return error values; panic/recover is appropriate for invariant violations or initialization failures that leave the program in an invalid state.

Can I collect and handle multiple errors in Go?

Go's errors package doesn't provide built-in multi-error support, but you can implement a custom error type holding a slice of errors, or use third-party multi-error packages. Return this collection when multiple independent failures occur, then iterate or aggregate them for reporting.