agentleFS
narenaryannarenaryan/agent-skills/skills/go/go-error-handling/SKILL.md

go-error-handling

Skill0 starsChanged 39 days agoMITView on GitHub

Use when designing Go error types, choosing error vs panic, or implementing recover — covers structured error types with Unwrap, errors.Is/As, the direct-call rule for recover, re-panic, and panic-as-internal-control-flow for complex parsers

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. 0 are common, 2 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Prefer over err == ErrX (breaks with wrapping) or err.(*PathError) (single-level only).

    line 33
  2. Distinctive:

    Only swallow panics of types you own.

    line 63
  3. Distinctive:

    Never let a control-flow panic escape the package.

    line 71

“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “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

Go Error Handling

Errors are values. Design the type to carry structured context (op, path, cause); callers inspect with errors.Is / errors.As. Panic is for programmer bugs and unrecoverable init — not cross-package flow control.

Structured error types

type PathError struct {
    Op   string   // "open", "unlink"
    Path string
    Err  error    // wrapped cause
}

func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
func (e *PathError) Unwrap() error { return e.Err }

Error strings prefix the op/package: "parse config.yaml: line 4: invalid key".

Inspection: errors.Is / errors.As

if errors.Is(err, fs.ErrNotExist) { ... }      // walks Unwrap chain
var pe *fs.PathError
if errors.As(err, &pe) { use(pe.Op, pe.Path) }

Prefer over err == ErrX (breaks with wrapping) or err.(*PathError) (single-level only).

Recover: direct-call rule

func safe() (err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("panic: %v", r)
        }
    }()
    risky()
    return
}

recover() returns nil unless invoked directly inside a deferred function's body. Moving it into a helper silently breaks recovery. Use named returns so the deferred func can set err.

Re-panic unknown values

defer func() {
    r := recover()
    if r == nil { return }
    e, ok := r.(myLocalError)
    if !ok { panic(r) }   // propagate foreign panics
    err = e.err
}()

Only swallow panics of types you own.

Panic as internal control flow

Parser/regexp packages legitimately panic(localError{…}) deep in the stack and recover at the public API boundary. Rules:

  • Panic value must be an unexported type.
  • Recover at every exported entry point.
  • Never let a control-flow panic escape the package.

When to panic

SituationPanic?
Impossible state / programmer bugyes
Missing required env at inityes
Bad user inputno — return error
Network / I/O failureno — return error
Internal parser control flowyes, bounded by recover at API edge

Pitfalls

  • recover() inside a helper: defer cleanup() where cleanup calls recover returns nil; panic continues. Must live in the deferred func's own body.
  • defer recover(): defers the recover call itself, which runs with no active panic. Wrap in a func literal.
  • Sentinel after wrapping: err == io.EOF fails once wrapped. Use errors.Is.
  • Panic across package boundary: forces every caller to wrap in recover. Convert at the exported boundary.
  • Mutating a returned error: callers may retain it; never modify fields of *myError after return.

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.