go-error-handling

📁 imrenagi/skills 📅 Jan 24, 2026
4
总安装量
3
周安装量
#53612
全站排名
安装命令
npx skills add https://github.com/imrenagi/skills --skill go-error-handling

Agent 安装分布

claude-code 2
antigravity 2
windsurf 1
opencode 1
codex 1
gemini-cli 1

Skill 文档

Go Error Handling Skill

This skill provides guidelines for implementing robust error handling in Go applications. Apply these practices when writing, reviewing, or refactoring Go code that involves error creation, propagation, or handling.

When to Use This Skill

Use this skill when:

  • Creating new functions or methods that return errors
  • Implementing repository or service layers that interact with databases or external services
  • Building gRPC or HTTP handlers that need to translate errors to status codes
  • Reviewing code for proper error handling patterns
  • Debugging error propagation issues

Core Principles

Error Handling Decision Flowchart

Error Occurs
    │
    ▼
Is it from an external dependency (DB, API, etc)?
    │
    ├── YES → Map to domain error + wrap with %w
    │
    └── NO → Is it a business rule violation?
              │
              ├── YES → Return appropriate domain error
              │
              └── NO → Wrap and propagate with context

At Handler Layer:
    │
    ▼
Log full error, return safe message to client

References

Document Description When to Use
error-wrapping.md How to wrap errors with %w for chain inspection Use when returning errors from functions to preserve the error chain. Essential if callers need to use errors.Is() or errors.As().
error-types.md When to use sentinel vs typed errors Use when deciding between var ErrXxx = errors.New() and custom error structs. Prefer typed errors when you need to attach metadata like resource IDs or permissions.
error-domain-error.md Defining and using domain-specific errors Use when building repository or service layers. Maps technology-specific errors (e.g., sql.ErrNoRows) to domain errors to decouple business logic from infrastructure.
server-grpc-error.md gRPC error handling and status codes Use when implementing gRPC handlers. Covers mapping domain errors to gRPC status codes and adding rich error details like field violations.