Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#81 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
go
Ambito
backend

Direzione di ricerca

Inizia con OopsErrorBuilder.copy(), OopsError e oops.AsOops per capire come vengono attualmente copiati o rigenerati i metadati e le tracce dello stack. Confronta i tre approcci proposti e conferma che l’API scelta preservi la traccia dello stack originale aggiungendo al contempo contesto; l’issue non specifica alcun test o percorso di file.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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!

Lingua principale
Go
Stelle
996
Fork
45
Merge medio
1m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di samber/oops

Tutte le issue di samber/oops

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.