agentleFS
rundopsrundops/dops/.claude/skills/clean-code-errors/SKILL.md

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

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
  1. Very specific:

    Always add context when propagating errors with fmt.Errorf and %w:

    line 27
  2. Very specific:

    Use %w (not %v) so callers can use errors.Is and errors.As

    line 36
  3. Distinctive:

    Keep sentinels minimal — only for conditions that change caller behavior

    line 47
  4. Distinctive:

    Use custom types when the caller needs structured information from the error:

    line 52
  5. Very specific:

    Do not overuse — simple fmt.Errorf wrapping is sufficient for most cases

    line 70
  6. Distinctive:

    Use early returns to keep the happy path unindented

    line 88
  7. Distinctive:

    Every error return must be checked

    line 92
  8. Distinctive:

    If you genuinely don't need the error, document why:

    line 93
  9. Distinctive:

    Never use blank identifier for errors silently

    line 97
  10. Very specific:

    Do not use panic for input validation, missing files, network errors, etc.

    line 102
  11. Very specific:

    If you're writing panic followed by a string, you almost certainly want return error instead

    line 104
  12. Very specific:

    If a function returns (T, error), a nil error means T is valid — always

    line 108
  13. Very specific:

    Do not return (nil, nil) to mean "not found" — use a sentinel error or a boolean:

    line 109

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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.Errorf and %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 use errors.Is and errors.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.Errorf wrapping 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 main or 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

  • panic is for programmer errors (bugs), never for expected runtime conditions
  • Do not use panic for input validation, missing files, network errors, etc.
  • recover belongs only in top-level middleware (HTTP handler, CLI root) — never in library code
  • If you're writing panic followed by a string, you almost certainly want return error instead

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

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.