Skip to content
Back to skills

Wrapping

ASecurity

Wrap errors with context using fmt.Errorf %w pattern

  • 3 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added February 7, 2026
code-qualitygo

Security analysis

A100/100

Scanned February 12, 2026

npx -y skills add JamesPrial/claudefiles --skill wrapping --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Wrapping?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Wrapping
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/jamesprial-wrapping/badge)](https://www.skillsdirectory.com/skills/jamesprial-wrapping)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: go-error-wrapping
description: Wrap errors with context using fmt.Errorf %w pattern
---

# Error Wrapping with %w

## Pattern
Use `fmt.Errorf` with `%w` to add context while preserving the error chain.

## CORRECT
```go
func ReadConfig(path string) (*Config, error) {
    data, err := os.ReadFile(path)
    if err != nil {
        return nil, fmt.Errorf("read config %s: %w", path, err)
    }

    var cfg Config
    if err := json.Unmarshal(data, &cfg); err != nil {
        return nil, fmt.Errorf("parse config %s: %w", path, err)
    }

    return &cfg, nil
}
```

## WRONG
```go
// Bad: Loses error chain
return nil, fmt.Errorf("read config failed: %v", err)

// Bad: No context
return nil, err

// Bad: String concatenation
return nil, errors.New("read config: " + err.Error())
```

## Key Rules
1. Use `%w` to wrap, not `%v` or `%s`
2. Add meaningful context (file names, IDs, operations)
3. Keep messages lowercase, no punctuation
4. Preserve original error for type checking

## Example Chain
```go
// Error flows up with context at each layer:
// "process user 123: read config /etc/app.conf: open /etc/app.conf: no such file or directory"

func ProcessUser(id int) error {
    cfg, err := ReadConfig("/etc/app.conf")
    if err != nil {
        return fmt.Errorf("process user %d: %w", id, err)
    }
    // use cfg...
    return nil
}
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…