error-handling-guide

Implement structured error handling patterns for Go applications.

1|Updated Jul 16, 2025
One-click install
npx skills add https://github.com/linskybing/platform-go --skill error-handling-guide
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: error-handling-guide
Source: https://github.com/linskybing/platform-go/tree/main/.github/skills/error-handling-guide
Command: npx skills add https://github.com/linskybing/platform-go --skill error-handling-guide

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a cohesive framework for error handling in Go applications, enabling predictable error propagation, context-rich logging, and safe recovery.

Core Features & Use Cases

  • Custom error types for domain errors
  • Error wrapping with context using %w
  • Structured logging and mapping errors to HTTP statuses
  • Panic recovery and retry logic
  • Error handling in goroutines and concurrent tasks
  • Guidance on best practices and anti-patterns

Quick Start

Integrate the error-handling patterns into your Go services by defining domain errors, wrappers, and recovery middleware. Start by adding the custom error types and wrapping helpers to your service layer, wire MapErrorToHTTPStatus in your HTTP handlers, and enable PanicRecoveryMiddleware and Retry helpers where appropriate.

Frequently Asked Questions about error-handling-guide

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

FAQPage Schema
How do I handle errors consistently across Go microservices and API services?

Consistent error handling in Go microservices requires structured patterns with custom domain error types, error wrapping using %w for context propagation, and mapping errors to HTTP statuses. This approach ensures predictable error propagation and context-rich logging across API services and background workers.

What is the best way to propagate errors with context in Go applications?

Error propagation with context in Go applications is best achieved by wrapping errors using the %w verb. This structured error wrapping preserves the original error chain while adding contextual information, enabling reliable error tracing and consistent logging throughout service layers.

How do I map custom domain errors to HTTP status codes in Go?

Mapping custom domain errors to HTTP status codes involves defining specific error types and wiring a MapErrorToHTTPStatus function into your HTTP handlers. This translates domain-specific errors into appropriate HTTP responses, ensuring clients receive accurate status codes for API failures.

How do I safely recover from panics in Go goroutines and concurrent tasks?

Recovering from panics in Go goroutines requires implementing PanicRecoveryMiddleware within your concurrent tasks. This safe recovery mechanism intercepts runtime panics, preventing goroutine crashes and allowing background workers to maintain operational stability during unexpected failures.

How do I implement retry logic with exponential backoff for Go background workers?

Retry logic with exponential backoff for Go background workers is implemented using dedicated retry helpers. These helpers automatically re-execute failed operations with increasing delays, providing resilient error recovery for transient failures in microservices and concurrent processing tasks.

Does this Go error-handling approach work for both API services and background workers?

Yes, this structured error-handling approach applies directly to API services, microservices, and background workers. It provides domain-specific error types, panic recovery, and consistent error logging, ensuring safe and predictable error propagation across various Go application architectures.