golang-error-handling

Create, wrap, inspect, and log Go errors with structured context.

2|Updated Mar 13, 2023
One-click install
npx skills add https://github.com/haipham22/golang-sample --skill golang-error-handling-haipham22
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-error-handling
Source: https://github.com/haipham22/golang-sample/tree/main/.agents/skills/golang-error-handling
Command: npx skills add https://github.com/haipham22/golang-sample --skill golang-error-handling-haipham22

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you design Go error handling that is consistent, debuggable, and production-safe, so failures are wrapped with context, inspected correctly, and logged without duplication.

Core Features & Use Cases

  • Error creation and wrapping: Create lowercase, low-cardinality errors and preserve chains with proper wrapping.
  • Error inspection and aggregation: Match sentinels, extract typed errors, and combine independent failures with joined errors.
  • Production logging discipline: Apply the single handling rule, use structured logging, and avoid exposing internal details to users.
  • Use Case: Ideal for reviewing services, handlers, repositories, middleware, and background jobs that need reliable Go error behavior across layered systems.

Quick Start

Use this skill to review the attached Go code and rewrite its error handling so errors are wrapped, logged once, and emitted with structured context.

Frequently Asked Questions about golang-error-handling

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

FAQPage Schema
What is the best way to wrap and inspect errors in a layered Go service?

The best way to handle errors in a layered Go service is wrapping them with the %w verb to preserve context, then inspecting the chain using errors.Is and errors.As to match sentinels and extract typed errors.

How do I combine independent failures when handling golang errors?

To combine independent failures in golang error handling, use errors.Join to aggregate multiple distinct errors into a single error value, allowing you to report concurrent or parallel failures without losing individual error context.

How do I prevent duplicate structured logs for the same Go error?

To prevent duplicate structured logs for the same Go error, apply the single-handling rule: wrap the error with context at lower layers and log it only once at the application boundary using structured logging.

When do I use errors.As versus errors.Is for Go error inspection?

Use errors.Is to check if an error in the chain matches a specific sentinel error value, while errors.As is used to extract a specific typed error from the chain, allowing you to access custom fields on the structured error.

Does this approach enforce any specific conventions for Go error strings?

Yes, this error handling approach enforces lowercase error strings to maintain consistency and low cardinality, ensuring that wrapped error chains remain clean, debuggable, and production-safe.

Can I use slog to add structured context to wrapped Go errors?

Yes, you can use structured slog or oops context to attach key-value pairs to wrapped Go errors, enriching the failure logs with operational context without exposing internal system details to the end user.