$npx -y skills add saifoelloh/golang-best-practices-skill --skill error-handlingUnified Go + GORM + PostgreSQL error handling review. Covers error wrapping, context propagation, PostgreSQL error codes (23505, 40001), errors.Is/As mapping, and retry logic. Ensures proper error chains and robust database failure recovery.
| 1 | # Error Handling (Unified Go & DB) |
| 2 | |
| 3 | Expert-level review for error handling in Go services using GORM and PostgreSQL. Ensures errors are correctly identified, preserved via wrapping, mapped to domain errors, and retried where appropriate. |
| 4 | |
| 5 | ## When to Apply |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Reviewing general Go error propagation and context usage |
| 9 | - Debugging database errors (e.g., duplicate keys, serialization failures) |
| 10 | - Auditing error wrapping with `%w` vs `%v` |
| 11 | - Ensuring correct use of `errors.Is` / `errors.As` |
| 12 | - Adding retry logic for serializable transactions or deadlocks |
| 13 | |
| 14 | ## Rule Categories |
| 15 | |
| 16 | | Priority | Count | Focus | |
| 17 | |---|---|---| |
| 18 | | **CRITICAL** | 6 | error chains, context leaks, PgError mapping, retries | |
| 19 | | **HIGH** | 6 | propagation, Is/As usage, RowsAffected, deadlock detection | |
| 20 | | **MEDIUM** | 2 | sentinel errors, structured logging | |
| 21 | |
| 22 | ## Rules Covered (14 total) |
| 23 | |
| 24 | ### Critical Issues (6) |
| 25 | - `critical-error-wrapping` — Use `%w`, not `%v`, to preserve error chains |
| 26 | - `critical-context-leak` — Always `defer cancel()` after context creation |
| 27 | - `critical-error-shadow` — Don't shadow `err` in nested scopes |
| 28 | - `critical-errors-is-as` — Use `errors.Is`/`As` for DB and wrapped errors |
| 29 | - `critical-pg-error-mapping` — Map Postgres codes to domain errors at repo boundary |
| 30 | - `critical-serialization-retry` — Retry transactions on `40001` serialization failure |
| 31 | |
| 32 | ### High-Impact Patterns (6) |
| 33 | - `high-context-propagation` — Propagate context through the call chain |
| 34 | - `high-error-is-as` — Prescriptive rule for using Is/As over string matching |
| 35 | - `high-interface-nil` — Check for nil in interfaces correctly |
| 36 | - `high-rows-affected` — Check `RowsAffected` after Update/Delete |
| 37 | - `high-wrap-with-context` — Wrap DB errors with operation context |
| 38 | - `high-deadlock-detection` — Handle `40P01` deadlocks as retryable |
| 39 | |
| 40 | ### Medium Improvements (2) |
| 41 | - `medium-sentinel-error-usage` — Use sentinel errors for stable categories |
| 42 | - `medium-structured-logging` — Log DB/Go errors with structured fields |
| 43 | |
| 44 | ## Trigger Phrases |
| 45 | |
| 46 | - "Review error handling" |
| 47 | - "Debug this DB error" |
| 48 | - "23505 / 40001 / 40P01" |
| 49 | - "Check for context leaks" |
| 50 | - "Review error chains" |
| 51 | - "Transaction retry" |
| 52 | - "ErrRecordNotFound" |
| 53 | |
| 54 | ## Output Format |
| 55 | |
| 56 | ``` |
| 57 | ## Critical Issues Found: X |
| 58 | |
| 59 | ### [Rule ID] (Line Y) |
| 60 | **Issue**: Description |
| 61 | **Impact**: Context leak / Lost chain / Data inconsistency |
| 62 | **Fix**: Suggested code |
| 63 | **Example**: |
| 64 | ```go |
| 65 | // Corrected code |
| 66 | ``` |
| 67 | ``` |
| 68 | |
| 69 | ## Related Skills |
| 70 | - [gorm-query-patterns](../gorm-query-patterns/SKILL.md) — For `.Error` check safety |
| 71 | - [concurrency-safety](../concurrency-safety/SKILL.md) — For context timeout patterns |