Feature Request: Allow adding context to an existing OopsError without regenerating stack trace
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di samber/oops
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
-
Can you add LastPublic(), LastCode() API?Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 55/100
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 2 giorni
-
test-failure
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
teramoby/speedle-plus#129 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
containerd/containerd#14485 ·
I maintainer di solito rispondono entro 1 giorno
-
bug needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno