Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@kmtym1998

Description

@kmtym1998

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions