clean-code-errors
Skill3 starsChanged 8 months agoMITView on GitHub
Clean Code error handling principles for Go. Use when writing, editing, or reviewing error handling in Go code — error returns, error wrapping, sentinel errors, custom error types, error checking patterns, and panic/recover usage.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- rundops/dopsword for word, first seen Mar 24, 2026
Other versions1 project uses a version of this.
Word for word · 1
- rundops/dops★ 3100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository19 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- bubbletea-patternsSkill
- charm-stylingSkill
- clean-code-boundariesSkill
- clean-code-designSkill
- clean-code-functionsSkill
- clean-code-namingSkill
- clean-code-solidSkill
- clean-code-testingSkill
- cli-structureSkill
- create-runbookSkill
- demo-recordingSkill
- mutation-testingSkill
- shell-scriptsSkill
- spec-workflowSkill
- tdd-workflowSkill
- tui-layoutSkill
- tui-testingSkill
- wizard-formsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
13 rules. About 0 are common, 6 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈6
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 27
Always add context when propagating errors with fmt.Errorf and %w:
- Very specific: line 36
Use %w (not %v) so callers can use errors.Is and errors.As
- Distinctive: line 47
Keep sentinels minimal — only for conditions that change caller behavior
- Distinctive: line 52
Use custom types when the caller needs structured information from the error:
- Very specific: line 70
Do not overuse — simple fmt.Errorf wrapping is sufficient for most cases
- Distinctive: line 88
Use early returns to keep the happy path unindented
- Distinctive: line 92
Every error return must be checked
- Distinctive: line 93
If you genuinely don't need the error, document why:
- Distinctive: line 97
Never use blank identifier for errors silently
- Very specific: line 102
Do not use panic for input validation, missing files, network errors, etc.
- Very specific: line 104
If you're writing panic followed by a string, you almost certainly want return error instead
- Very specific: line 108
If a function returns (T, error), a nil error means T is valid — always
- Very specific: line 109
Do not return (nil, nil) to mean "not found" — use a sentinel error or a boolean:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Clean Code Error Handling for Go
Apply these principles when writing or modifying error handling in Go.
Fail Fast
- Validate inputs at system boundaries (CLI args, config, API inputs) immediately
- Return errors at the first point of failure — don't continue in an inconsistent state
- Precondition checks go at the top of the function:
func Deploy(ctx context.Context, target string) error { if target == "" { return errors.New("target must not be empty") } // ... proceed with valid input }
Error Wrapping
- Always add context when propagating errors with
fmt.Errorfand%w:result, err := s.store.Get(ctx, id) if err != nil { return nil, fmt.Errorf("get deployment %s: %w", id, err) } - The wrap message should describe what this function was doing, not restate the underlying error
- This creates a chain:
"deploy to prod: get deployment abc123: connection refused" - Use
%w(not%v) so callers can useerrors.Isanderrors.As
Sentinel Errors
- Define package-level sentinel errors for conditions callers need to check:
var ( ErrNotFound = errors.New("not found") ErrConflict = errors.New("conflict") ) - Keep sentinels minimal — only for conditions that change caller behavior
- Callers check with
errors.Is(err, ErrNotFound), which works through wrap chains
Custom Error Types
- Use custom types when the caller needs structured information from the error:
type ValidationError struct { Field string Message string } func (e *ValidationError) Error() string { return fmt.Sprintf("validation: %s: %s", e.Field, e.Message) } - Callers extract with
errors.As:var ve *ValidationError if errors.As(err, &ve) { // handle validation error with access to ve.Field } - Do not overuse — simple
fmt.Errorfwrapping is sufficient for most cases
Error Handling Patterns
- Handle the error OR return it — never both (don't log AND return):
// Bad: error is handled twice if err != nil { log.Error("failed to deploy", "err", err) return err // caller will also log it } // Good: return with context, let the caller decide if err != nil { return fmt.Errorf("deploy: %w", err) } - Log errors at the top of the call stack (usually
mainor the HTTP/CLI handler) - Use early returns to keep the happy path unindented
Never Ignore Errors
- Every error return must be checked
- If you genuinely don't need the error, document why:
_ = w.Close() // best-effort cleanup; write already succeeded - Never use blank identifier for errors silently
Panic and Recover
panicis for programmer errors (bugs), never for expected runtime conditions- Do not use
panicfor input validation, missing files, network errors, etc. recoverbelongs only in top-level middleware (HTTP handler, CLI root) — never in library code- If you're writing
panicfollowed by a string, you almost certainly wantreturn errorinstead
Do Not Return Nil for Errors
- If a function returns
(T, error), a nil error means T is valid — always - Do not return
(nil, nil)to mean "not found" — use a sentinel error or a boolean:// Bad func Find(id string) (*Thing, error) { // returns (nil, nil) if not found — caller must check both // Good func Find(id string) (*Thing, error) { // returns (nil, ErrNotFound) if not found
Spec Traceability
- Specs should define error scenarios explicitly
- Each spec error case becomes a test case
- Error messages should be user-meaningful when they surface in CLI output
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

