samber / samber/oops

Feature Request: Allow adding context to an existing OopsError without regenerating stack trace

Open
#81 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
990
Forks
45
Avg merge
5d 9h
Merged PRs (30d)
5

Description

Problem

When adding context (attributes, user info, tags, etc.) to an existing OopsError, the current API requires wrapping the error again, which regenerates the stack trace and changes its origin point.

Example
// Create an error with stack trace pointing to this line
originalErr := oops.Code("ERR001").Errorf("database connection failed")

// Add context by wrapping - this regenerates the stack trace!
// Stack trace now points to this line instead of the original error
enrichedErr := oops.With("user_id", 123).Wrap(originalErr)

This behavior causes:

  • The stack trace no longer points to where the error actually occurred
  • Multiple context additions create multiple unnecessary stack traces
  • It's impossible to preserve the original error location while adding context

Suggestion

I've come up with about three ideas:

1. Add With*() methods on OopsError

Add methods that allow adding context without regenerating the stack trace:

// Add context
func (o OopsError) WithContext(kv ...any) OopsError

// Add user information
func (o OopsError) WithUser(userID string, userData ...any) OopsError

// Add tags
func (o OopsError) WithTags(tags ...string) OopsError

// Add other metadata
func (o OopsError) WithTenant(tenantID string, tenantData ...any) OopsError
func (o OopsError) WithCode(code string) OopsError
func (o OopsError) WithDomain(domain string) OopsError
func (o OopsError) WithHint(hint string) OopsError
func (o OopsError) WithPublic(public string) OopsError
func (o OopsError) WithOwner(owner string) OopsError
func (o OopsError) WithTrace(trace string) OopsError
func (o OopsError) WithDuration(duration time.Duration) OopsError
2. Modify copy() to preserve stacktrace

Currently, OopsErrorBuilder.copy() explicitly does NOT copy the stacktrace field (I don’t understand why it’s implemented this way):

func (o OopsErrorBuilder) copy() OopsErrorBuilder {
    return OopsErrorBuilder{
        // ...
        // stacktrace: o.stacktrace, // Not copied as it's generated per error
    }
}

If copy() preserved the stacktrace, we could do:

if oe, ok := oops.AsOops(err); ok {
    builder := oops.OopsErrorBuilder(oe)
    err = oops.OopsError(builder.With("user_id", 123))
    // Stack trace would be preserved!
}

This would require copying the stacktrace in the copy() method:

func (o OopsErrorBuilder) copy() OopsErrorBuilder {
    return OopsErrorBuilder{
        // ... existing fields ...
        stacktrace: o.stacktrace,  // Copy stacktrace
    }
}

This may be problematic as it alters the current behavior.

3. Add Apply() method to OopsErrorBuilder

Add a method that merges builder metadata into an existing OopsError while preserving its stack trace:

// Apply applies the builder's metadata to an existing OopsError
// without regenerating its stack trace
func (o OopsErrorBuilder) Apply(oe OopsError) OopsError {
    o2 := oe

    // Merge context
    if len(o.context) > 0 {
        o2.context = lo.Assign(map[string]any{}, oe.context, o.context)
    }

    // Merge tags
    if len(o.tags) > 0 {
        o2.tags = lo.Uniq(append(oe.tags, o.tags...))
    }

    // Override other fields if set
    if o.code != "" {
        o2.code = o.code
    }
    // ... other fields

    // Preserve original stacktrace
    return o2
}

I'm happy to contribute if the solution looks good to you!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with OopsErrorBuilder.copy(), OopsError, and oops.AsOops to understand how metadata and stack traces are currently copied or regenerated. Compare the three proposed approaches and confirm the chosen API preserves the original stack trace while adding context; the issue does not name a specific test or file path.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.