AgentSkillsCN

go-error-handling

Go 应用程序中的错误处理最佳实践,包括错误包装、领域级错误、哨兵错误与类型化错误的区别,以及 gRPC 错误映射策略。

SKILL.md
--- frontmatter
name: go-error-handling
description: Best practices for error handling in Go applications, including error wrapping, domain errors, sentinel vs typed errors, and gRPC error mapping.

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

code
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

DocumentDescriptionWhen to Use
error-wrapping.mdHow to wrap errors with %w for chain inspectionUse when returning errors from functions to preserve the error chain. Essential if callers need to use errors.Is() or errors.As().
error-types.mdWhen to use sentinel vs typed errorsUse 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.mdDefining and using domain-specific errorsUse 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.mdgRPC error handling and status codesUse when implementing gRPC handlers. Covers mapping domain errors to gRPC status codes and adding rich error details like field violations.