go-error-handling

Implement idiomatic Go error handling with wrapping, custom types, and sentinels.

2|Updated Feb 14, 2025
One-click install
npx skills add https://github.com/gofhir/validator --skill go-error-handling-gofhir
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-error-handling
Source: https://github.com/gofhir/validator/tree/main/.claude/skills/go-error-handling
Command: npx skills add https://github.com/gofhir/validator --skill go-error-handling-gofhir

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides clear, idiomatic patterns for handling errors in Go, ensuring robust and maintainable code.

Core Features & Use Cases

  • Error Wrapping: Add context to errors using fmt.Errorf with %w.
  • Custom Error Types: Define custom error structs with Unwrap() for detailed error information.
  • Error Sentinels: Use exported error variables for specific error conditions.
  • Use Case: When an API call fails, wrap the underlying network error with a descriptive message indicating which API was called, making debugging much faster.

Quick Start

Use the go-error-handling skill to demonstrate how to wrap an error with context.

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 in Go to add context about which API call failed?

To wrap errors in Go, use `fmt.Errorf` with the `%w` verb to add descriptive context to an underlying network error, making debugging much faster by indicating which API was called.

What is the idiomatic Go way to check for specific sentinel errors?

The idiomatic Go way to check for specific sentinel errors is by using exported error variables and inspecting them with `errors.Is` to see if a target error matches any error in the chain.

How do I create custom error types in Go that support unwrapping?

To create custom error types in Go, define custom error structs that implement the `Unwrap()` method, allowing detailed error information and supporting cross-layer error propagation via `errors.As`.

What's the best way to propagate errors across different layers in a Go application?

The best way to propagate errors across layers in a Go application is through error wrapping with `%w` and custom error types implementing `Unwrap()`, ensuring robust and maintainable cross-layer context.

When should I use `errors.As` versus `errors.Is` for Go error handling?

Use `errors.Is` when checking if an error matches a specific sentinel value, and use `errors.As` when you need to extract detailed information by assigning the error to a custom error type implementing `Unwrap()`.