-
Notifications
You must be signed in to change notification settings - Fork 811
[tmpnet] Add optional stack traces to errors originating from tmpnet #4262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package stacktrace | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| ) | ||
|
|
||
| // If the environment variable STACK_TRACE_ERRORS=1 is set, errors | ||
| // passing through the functions defined in this package will have a | ||
| // stack trace added to them. The following equivalents to stdlib error | ||
| // functions are provided: | ||
| // | ||
| // - `fmt.Errorf` -> `stacktrace.Errorf` | ||
| // - `errors.New` -> `stacktrace.New` | ||
| // | ||
| // Additionally, a stack trace can be added to an existing error with | ||
| // `stacktrace.Wrap(err)`. | ||
|
|
||
| var stackTraceErrors bool | ||
|
|
||
| func init() { | ||
| if os.Getenv("STACK_TRACE_ERRORS") == "1" { | ||
| stackTraceErrors = true | ||
| } | ||
| } | ||
|
|
||
| type StackTraceError struct { | ||
| StackTrace []runtime.Frame | ||
| Cause error | ||
| } | ||
|
|
||
| func (e StackTraceError) Error() string { | ||
| result := e.Cause.Error() | ||
| if !stackTraceErrors { | ||
| return result | ||
| } | ||
|
|
||
| result += "\nStack trace:\n" | ||
| for _, frame := range e.StackTrace { | ||
| result += fmt.Sprintf("%s:%d: %s\n", frame.File, frame.Line, frame.Function) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func (e StackTraceError) Unwrap() error { | ||
| return e.Cause | ||
| } | ||
|
|
||
| func New(msg string) error { | ||
| if !stackTraceErrors { | ||
| return errors.New(msg) | ||
| } | ||
| return wrap(errors.New(msg)) | ||
| } | ||
|
|
||
| // Errorf adds a stack trace to the last argument provided if it is an | ||
| // error and stack traces are enabled. | ||
| func Errorf(format string, args ...any) error { | ||
| if !stackTraceErrors { | ||
| return fmt.Errorf(format, args...) | ||
| } | ||
|
|
||
| // Assume the last argument is an error requring a stack trace if it is of type error | ||
| err, ok := args[len(args)-1].(error) | ||
| if !ok { | ||
| return fmt.Errorf(format, args...) | ||
| } | ||
|
|
||
| newErr := fmt.Errorf(format, args...) | ||
|
|
||
| // If there's already a StackTraceError, preserve its stack but update the cause | ||
| var existingStackErr StackTraceError | ||
| if errors.As(err, &existingStackErr) { | ||
| existingStackErr.Cause = newErr | ||
| return existingStackErr | ||
| } | ||
|
|
||
| // No stack trace exists, capture one now | ||
| return wrap(newErr) | ||
| } | ||
|
|
||
| func Wrap(err error) error { | ||
| if !stackTraceErrors { | ||
| return err | ||
| } | ||
| return wrap(err) | ||
| } | ||
|
|
||
| // wrap adds a stack trace to err if stack traces are enabled and it | ||
| // doesn't already have one. | ||
| func wrap(err error) error { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| // If there's already a StackTraceError in the chain, just return it | ||
| var existingStackErr StackTraceError | ||
| if errors.As(err, &existingStackErr) { | ||
| return err | ||
| } | ||
|
|
||
| // Need to capture a stack trace | ||
| const depth = 32 | ||
| var pcs [depth]uintptr | ||
| skip := 3 // skip wrap, New/Wrap/Errorf, and runtime.Callers | ||
| n := runtime.Callers(skip, pcs[:]) | ||
|
|
||
| frames := runtime.CallersFrames(pcs[:n]) | ||
| var frameSlice []runtime.Frame | ||
|
|
||
| for { | ||
| frame, more := frames.Next() | ||
| frameSlice = append(frameSlice, frame) | ||
| if !more { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return StackTraceError{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to export this type? It seems like we only use this type as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to err on the side of exporting when it comes to library functionality in support of testing to minimize friction with downstream repos like coreth and subnet-evm. |
||
| StackTrace: frameSlice, | ||
| Cause: err, | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if tmpnet has other env vars it supports, but should we prefix them with something like
TMPNET_orTMPNET_DEBUG_?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intentionally avoided prefixing with
TMPNETbecause it's not a tmpnet-specific thing. tmpnet is only the first adopter, but there's no reason for this library to be restricted to it (hence not putting it undertests/fixture/tmpnet).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I think this env var is ideal, just that I think it's good enough for now.