bateau84/skills

golang-error-handling

Wrap, propagate, and inspect errors in Go — %w, errors.Is, errors.As, errors.Join, sentinel errors, custom error types, panic/recover, and the single handling rule.

ソースを見る

使い方

  1. インストールコマンドをコピーし、自分の環境へ導入する前にソースを確認します。
  2. 必要なツール、認証情報、対応環境を原文で確認します。
  3. 入力と成果物を指定し、出力を資料と照合します。
npx skills add https://github.com/bateau84/skills --skill golang-error-handling

開始前の確認

  • 導入にはモデル契約、API枠、ファイルへのアクセス許可は含まれません。
  • スター数や導入数は活動の指標であり、独立した品質・安全性試験ではありません。
リポジトリの原文

見出し、例、コード、表、リンク、参照画像を含む原文を表示しています。

Adapted from samber/cc-skills-golang@golang-error-handling. Pair with golang-common-practice for shared verification guidance.

Persona: You are a Go reliability engineer. You treat every error as an event that must either be handled or propagated with context — silent failures and duplicate logs are equally unacceptable.

Modes:

  • Coding mode — writing new error handling code. Follow the best practices sequentially; inspect relevant adjacent code for swallowed errors and log-and-return pairs. An authorized read-only collaborator may help when available, but delegation is not required.
  • Review mode — reviewing a PR's error handling changes. Focus on the diff: check for swallowed errors, missing wrapping context, log-and-return pairs, and panic misuse. Sequential.
  • Audit mode — auditing existing error handling across a codebase. Cover every applicable independent category (creation, wrapping, single-handling rule, panic/recover, structured logging).

Go Error Handling Best Practices

This skill guides the creation of robust, idiomatic error handling in Go applications. Follow these principles to write maintainable, debuggable, and production-ready error code.

Best Practices Summary

  1. Returned errors MUST always be checked — NEVER discard with _
  2. Errors MUST be wrapped with context using fmt.Errorf("{context}: %w", err)
  3. Error strings MUST be lowercase, without trailing punctuation
  4. Use `%w` internally, `%v` at system boundaries to control error chain exposure
  5. MUST use `errors.Is` for sentinel matching and `errors.As`/`errors.AsType` for typed chain inspection instead of direct comparison or bare type assertions. For Go 1.26+, prefer errors.AsType[T](err) when T implements error; use errors.As(err, &target) for Go <1.26 or for non-error interface targets.
  6. SHOULD use `errors.Join` (Go 1.20+) to combine independent errors
  7. Errors MUST be either logged OR returned, NEVER both (single handling rule)
  8. Use sentinel errors for expected conditions, custom types for carrying data
  9. NEVER use `panic` for expected error conditions — reserve for truly unrecoverable states
  10. SHOULD use `slog` (Go 1.21+) for structured error logging — not fmt.Println or log.Printf
  11. Use `samber/oops` for production errors needing stack traces, user/tenant context, or structured attributes
  12. Log HTTP requests with structured middleware capturing method, path, status, and duration
  13. Use log levels to indicate error severity
  14. Never expose technical errors to users — translate internal errors to user-friendly messages, log technical details separately
  15. Keep log grouping low-cardinality — at logging/APM boundaries, keep message templates stable and attach IDs, paths, line numbers, and counts as structured attributes. Error values may include useful operational context, but avoid putting high-cardinality data into the stable log message used for grouping.

Detailed Reference

  • [Error Creation](./references/error-creation.md) — How to create errors that tell the story: error messages should be lowercase, no punctuation, and describe what happened without prescribing action. Covers sentinel errors (one-time preallocation for performance), custom error types (for carrying rich context), and the decision table for which to use when.
  • [Error Wrapping and Inspection](./references/error-wrapping.md) — Why fmt.Errorf("{context}: %w", err) beats fmt.Errorf("{context}: %v", err) (chains vs concatenation). How to inspect chains with errors.Is, errors.As, and Go 1.26+ errors.AsType for type-safe error handling, and errors.Join for combining independent errors.
  • [Error Handling Patterns and Logging](./references/error-handling.md) — The single handling rule: errors are either logged OR returned, NEVER both (prevents duplicate logs cluttering aggregators). Panic/recover design, samber/oops for production errors, and slog structured logging integration for APM tools.

Parallelizing Error Handling Audits

When auditing error handling across a large codebase, cover each applicable category below. One practitioner can do this sequentially. If parallel collaborators are available and authorized, divide read-only inspection and reconcile findings; coordinate any overlapping repairs explicitly rather than assuming host locks or isolated files.

  • Error creation — validate errors.New/fmt.Errorf usage, low-cardinality messages, custom types
  • Error wrapping — audit %w vs %v, verify errors.Is/errors.As patterns
  • Single handling rule — find log-and-return violations, swallowed errors, discarded errors (_)
  • Panic/recover — audit panic usage, verify recovery at goroutine boundaries
  • Structured logging — verify slog usage at error sites, check for PII in error messages

Cross-References

  • For the full samber/oops API, builder patterns, and logger integration, consult its official documentation (→ See golang-pkg-go-dev skill)
  • → See golang-observability for structured logging setup, log levels, and request logging middleware
  • → See golang-safety for nil interface trap and nil error comparison pitfalls
  • → See golang-naming for error naming conventions (ErrNotFound, PathError)
  • → See golang-continuous-integration skill for automated AI-driven code review in CI using these guidelines

References

タスクに関連する項目

同じリポジトリから

関連する Skills

すべての Skills
bateau84
コミュニティ

behavioral-spec

Derive bounded, implementation-independent requirements from agreed product intent through goal refinement, coverage lenses, obstacle analysis, provenance tracking, and counterexample checks. Use to select, author, and reconcile Behavioral Requirements (BR), Quality Scenarios (QS), and Obligation Contracts (OC) without expanding scope. Not for concrete API/schema/protocol realization (use architectural-spec) or human-centered stories and scenarios (use user-stories-and-scenarios).

1時間の導入数
1
GitHub Stars
0
更新日
10月18日
bateau84
コミュニティ

desktop-ui-design

Desktop GUI design for native and hybrid applications: platform conventions (macOS HIG, Windows Fluent, GNOME HIG), menus, keyboard shortcuts, window management, session persistence, native vs Electron/Tauri trade-offs, and accessibility APIs. Use for Swift/AppKit, C/WinUI, GTK+, or Electron/Tauri/Flutter desktop design. Add web-ui-design for a web app shipped as a desktop wrapper. Not for browser-only interfaces, terminal applications (use tui-design), or command-line tools (use cli-design).

1時間の導入数
1
GitHub Stars
0
更新日
10月18日
bateau84
コミュニティ

git-commit-discipline

Small coherent commits, explanatory messages, shared-working-tree discipline, and safe undo/recovery. Use when asked to commit, when a diff mixes concerns, when agents share a working tree, or when undoing or recovering Git changes. Not for content conflicts (use git-conflicts).

1時間の導入数
1
GitHub Stars
0
更新日
10月18日
bateau84
コミュニティ

golang-dependency-injection

google/wire, uber-go/dig, uber-go/fx, samber/do, manual constructor injection, and service containers in Go. Use when designing service architecture, setting up dependency injection, refactoring tightly coupled code, or managing singletons and service factories.

1時間の導入数
1
GitHub Stars
0
更新日
10月18日