← 返回 Skills 市场
samber

Golang Samber Oops

作者 Samuel Berthe · GitHub ↗ · v1.1.2 · MIT-0
cross-platform ✓ 安全检测通过
164
总下载
0
收藏
1
当前安装
3
版本数
在 OpenClaw 中安装
/install golang-samber-oops
功能描述
Structured error handling in Golang with samber/oops — error builders, stack traces, error codes, error context, error wrapping, error attributes, user-facin...
使用说明 (SKILL.md)

Persona: You are a Go engineer who treats errors as structured data. Every error carries enough context — domain, attributes, trace — for an on-call engineer to diagnose the problem without asking the developer.

samber/oops Structured Error Handling

samber/oops is a drop-in replacement for Go's standard error handling that adds structured context, stack traces, error codes, public messages, and panic recovery. Variable data goes in .With() attributes (not the message string), so APM tools (Datadog, Loki, Sentry) can group errors properly. Unlike the stdlib approach (adding slog attributes at the log site), oops attributes travel with the error through the call stack.

Why use samber/oops

Standard Go errors lack context — you see connection failed but not which user triggered it, what query was running, or the full call stack. samber/oops provides:

  • Structured context — key-value attributes on any error
  • Stack traces — automatic call stack capture
  • Error codes — machine-readable identifiers
  • Public messages — user-safe messages separate from technical details
  • Low-cardinality messages — variable data in .With() attributes, not the message string, so APM tools group errors properly

This skill is not exhaustive. Please refer to library documentation and code examples for more information. Context7 can help as a discoverability platform.

Core pattern: Error builder chain

All oops errors use a fluent builder pattern:

err := oops.
    In("user-service").           // domain/feature
    Tags("database", "postgres").  // categorization
    Code("network_failure").       // machine-readable identifier
    User("user-123", "email", "[email protected]").  // user context
    With("query", query).          // custom attributes
    Errorf("failed to fetch user: %s", "timeout")

Terminal methods:

  • .Errorf(format, args...) — create a new error
  • .Wrap(err) — wrap an existing error
  • .Wrapf(err, format, args...) — wrap with a message
  • .Join(err1, err2, ...) — combine multiple errors
  • .Recover(fn) / .Recoverf(fn, format, args...) — convert panic to error

Error builder methods

Methods Use case
.With("key", value) Add custom key-value attribute (lazy func() any values supported)
.WithContext(ctx, "key1", "key2") Extract values from Go context into attributes (lazy values supported)
.In("domain") Set the feature/service/domain
.Tags("auth", "sql") Add categorization tags (query with err.HasTag("tag"))
.Code("iam_authz_missing_permission") Set machine-readable error identifier/slug
.Public("Could not fetch user.") Set user-safe message (separate from technical details)
.Hint("Runbook: https://doc.acme.org/doc/abcd.md") Add debugging hint for developers
.Owner("team/slack") Identify responsible team/owner
.User(id, "k", "v") Add user identifier and attributes
.Tenant(id, "k", "v") Add tenant/organization context and attributes
.Trace(id) Add trace / correlation ID (default: ULID)
.Span(id) Add span ID representing a unit of work/operation (default: ULID)
.Time(t) Override error timestamp (default: time.Now())
.Since(t) Set duration based on time since t (exposed via err.Duration())
.Duration(d) Set explicit error duration
.Request(req, includeBody) Attach *http.Request (optionally including body)
.Response(res, includeBody) Attach *http.Response (optionally including body)
oops.FromContext(ctx) Start from an OopsErrorBuilder stored in a Go context

Common scenarios

Database/repository layer

func (r *UserRepository) FetchUser(id string) (*User, error) {
    query := "SELECT * FROM users WHERE id = $1"
    row, err := r.db.Query(query, id)
    if err != nil {
        return nil, oops.
            In("user-repository").
            Tags("database", "postgres").
            With("query", query).
            With("user_id", id).
            Wrapf(err, "failed to fetch user from database")
    }
    // ...
}

HTTP handler layer

func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request) {
    userID := getUserID(r)

    err := h.service.CreateUser(r.Context(), userID)
    if err != nil {
        return oops.
            In("http-handler").
            Tags("endpoint", "/users").
            Request(r, false).
            User(userID).
            Wrapf(err, "create user failed")
    }

    w.WriteHeader(http.StatusCreated)
}

Service layer with reusable builder

func (s *UserService) CreateOrder(ctx context.Context, req CreateOrderRequest) error {
    builder := oops.
        In("order-service").
        Tags("orders", "checkout").
        Tenant(req.TenantID, "plan", req.Plan).
        User(req.UserID, "email", req.UserEmail)

    product, err := s.catalog.GetProduct(ctx, req.ProductID)
    if err != nil {
        return builder.
            With("product_id", req.ProductID).
            Wrapf(err, "product lookup failed")
    }

    if product.Stock \x3C req.Quantity {
        return builder.
            Code("insufficient_stock").
            Public("Not enough items in stock.").
            With("requested", req.Quantity).
            With("available", product.Stock).
            Errorf("insufficient stock for product %s", req.ProductID)
    }

    return nil
}

Error wrapping best practices

DO: Wrap directly, no nil check needed

// ✓ Good — Wrap returns nil if err is nil
return oops.Wrapf(err, "operation failed")

// ✗ Bad — unnecessary nil check
if err != nil {
    return oops.Wrapf(err, "operation failed")
}
return nil

DO: Add context at each layer

Each architectural layer SHOULD add context via Wrap/Wrapf — at least once per package boundary (not necessarily at every function call).

// ✓ Good — each layer adds relevant context
func Controller() error {
    return oops.In("controller").Trace(traceID).Wrapf(Service(), "user request failed")
}

func Service() error {
    return oops.In("service").With("op", "create_user").Wrapf(Repository(), "db operation failed")
}

func Repository() error {
    return oops.In("repository").Tags("database", "postgres").Errorf("connection timeout")
}

DO: Keep error messages low-cardinality

Error messages MUST be low-cardinality for APM aggregation. Interpolating variable data into the message breaks grouping in Datadog, Loki, Sentry.

// ✗ Bad — high-cardinality, breaks APM grouping
oops.Errorf("failed to process user %s in tenant %s", userID, tenantID)

// ✓ Good — static message + structured attributes
oops.With("user_id", userID).With("tenant_id", tenantID).Errorf("failed to process user")

Panic recovery

oops.Recover() MUST be used in goroutine boundaries. Convert panics to structured errors:

func ProcessData(data string) (err error) {
    return oops.
        In("data-processor").
        Code("panic_recovered").
        Hint("Check input data format and dependencies").
        With("panic_value", r).
        Recover(func() {
            riskyOperation(data)
        })
}

Accessing error information

samber/oops errors implement the standard error interface. Access additional info:

if oopsErr, ok := err.(oops.OopsError); ok {
    fmt.Println("Code:", oopsErr.Code())
    fmt.Println("Domain:", oopsErr.Domain())
    fmt.Println("Tags:", oopsErr.Tags())
    fmt.Println("Context:", oopsErr.Context())
    fmt.Println("Stacktrace:", oopsErr.Stacktrace())
}

// Get public-facing message with fallback
publicMsg := oops.GetPublic(err, "Something went wrong")

Output formats

fmt.Printf("%+v\
", err)       // verbose with stack trace
bytes, _ := json.Marshal(err)  // JSON for logging
slog.Error(err.Error(), slog.Any("error", err))  // slog integration

Context propagation

Carry error context through Go contexts:

func middleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        builder := oops.
            In("http").
            Request(r, false).
            Trace(r.Header.Get("X-Trace-ID"))

        ctx := oops.WithBuilder(r.Context(), builder)
        next.ServeHTTP(w, r.WithContext(ctx))
    })
}

func handler(ctx context.Context) error {
    return oops.FromContext(ctx).Tags("handler", "users").Errorf("something failed")
}

For assertions, configuration, and additional logger examples, see Advanced patterns.

References

Cross-References

  • → See samber/cc-skills-golang@golang-error-handling skill for general error handling patterns
  • → See samber/cc-skills-golang@golang-observability skill for logger integration and structured logging
安全使用建议
This skill is an instructional guide for using the samber/oops Go library (no code is installed and no credentials are requested). Before enabling it, verify you have Go on PATH and that your project actually uses github.com/samber/oops (or you intend to adopt it). Also review the examples to ensure they match your error-handling policies and coding standards. Note: the SKILL.md declares allowed tools (shell/git/linting) as typical agent capabilities — the skill's instructions themselves don't perform shell/file operations, but if your agent runs skills autonomously, consider whether you want to permit those tool operations platform-wide.
能力评估
Purpose & Capability
Name, description, and content focus on structured error handling with samber/oops; the only declared runtime dependency is the Go binary, which is appropriate for Go code examples and guidance.
Instruction Scope
SKILL.md contains examples, patterns, and guidance specific to samber/oops; it does not instruct the agent to read unrelated files, collect system secrets, or transmit data to external endpoints beyond normal documentation references.
Install Mechanism
There is no install spec and no code files to write or execute — this is an instruction-only skill, which minimizes install-time risk.
Credentials
The skill requests no environment variables or credentials. The guidance and examples do not require secrets or unrelated credentials.
Persistence & Privilege
always is false and the skill does not request persistent system presence or modification of other skills/config; autonomous invocation is allowed by platform default but the skill itself is instruction-only and not asking for elevated privileges.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install golang-samber-oops
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /golang-samber-oops 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.1.2
- Updated version metadata to 1.1.2 in SKILL.md. - Fixed a minor typo: "informations" → "information" in the documentation.
v1.1.1
- Added evals/evals.json file for new evaluation or testing support. - Updated version metadata to 1.1.1 in SKILL.md. - No functionality changes to skill logic or documentation content.
v0.1.0
Initial release – brings structured error handling to Go with samber/oops integration. - Provides error builders with stack traces, error codes, attributes, user/developer messages, and panic recovery. - Documents core builder pattern and method usages for common scenarios (repository, HTTP handler, services). - Emphasizes best practices: per-layer context, low-cardinality messages, and APM tool compatibility. - Includes concise tables and code samples for easy adoption. - Suitable for projects already importing github.com/samber/oops.
元数据
Slug golang-samber-oops
版本 1.1.2
许可证 MIT-0
累计安装 1
当前安装数 1
历史版本数 3
常见问题

Golang Samber Oops 是什么?

Structured error handling in Golang with samber/oops — error builders, stack traces, error codes, error context, error wrapping, error attributes, user-facin... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 164 次。

如何安装 Golang Samber Oops?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install golang-samber-oops」即可一键安装,无需额外配置。

Golang Samber Oops 是免费的吗?

是的,Golang Samber Oops 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Golang Samber Oops 支持哪些平台?

Golang Samber Oops 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Golang Samber Oops?

由 Samuel Berthe(@samber)开发并维护,当前版本 v1.1.2。

💬 留言讨论