agentleFS
RoninForgeRoninForge/roninforge-go/skills/go-error-wrap/SKILL.md

go-error-wrap

Skill0 starsChanged 7 months agoMITView on GitHub

Convert plain or %v-formatted errors to wrapped errors with %w. Replace == sentinel comparisons with errors.Is. Replace concrete-type checks with errors.As. Define sentinel errors and typed error structs where useful.

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When refactoring a codebase whose error handling does not preserve the chain, or when adding error handling to new code and you want to do …

    line 10
  2. Very specific:

    Never wrap context.Canceled as an error in HTTP handlers.

    line 77
  3. Very specific:

    After conversion, run go vet ./...

    line 83

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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

Wrap Go Errors

When to Use

When refactoring a codebase whose error handling does not preserve the chain, or when adding error handling to new code and you want to do it right the first time.

Instructions

  1. Scan for fmt.Errorf("...: %v", err) and convert each to %w:

    // before
    return fmt.Errorf("load user: %v", err)
    // after
    return fmt.Errorf("load user: %w", err)
  2. Scan for err == X (where X is an exported error) and convert to errors.Is:

    // before
    if err == sql.ErrNoRows { ... }
    // after
    if errors.Is(err, sql.ErrNoRows) { ... }
  3. Scan for type assertions on errors and convert to errors.As:

    // before
    if pe, ok := err.(*os.PathError); ok { ... }
    // after
    var pe *os.PathError
    if errors.As(err, &pe) { ... }
  4. Define sentinel errors where callers must distinguish. Place them at the top of the package file:

    var (
        ErrNotFound      = errors.New("not found")
        ErrAlreadyExists = errors.New("already exists")
    )
  5. For richer errors, define a struct with Error() and optionally Unwrap():

    type ValidationError struct {
        Field   string
        Message string
    }
    func (e *ValidationError) Error() string {
        return fmt.Sprintf("%s: %s", e.Field, e.Message)
    }
  6. For aggregating multiple errors (batch processing, multi-file validation, parallel work), use errors.Join:

    var errs []error
    for _, item := range items {
        if err := process(item); err != nil {
            errs = append(errs, fmt.Errorf("%s: %w", item.ID, err))
        }
    }
    return errors.Join(errs...)
  7. Wrap at boundaries (package edge, layer transition), not at every layer. Each wrap should add meaningful context.
  8. Never wrap context.Canceled as an error in HTTP handlers. Treat it as a no-op return.

    if errors.Is(err, context.Canceled) { return }
  9. After conversion, run go vet ./... and staticcheck ./.... Confirm tests still pass.
  10. Replace panic(err) calls with a returned error unless the error truly indicates an impossible program state.

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.