Skip to content

Commit

Permalink
put stack traces behind feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Jul 25, 2024
1 parent 85abbfa commit 158c82f
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions runtime/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,24 @@ type UnexpectedError struct {
Stack []byte
}

var StackTracesEnabled = true

var _ InternalError = UnexpectedError{}

func (UnexpectedError) IsInternalError() {}

func NewUnexpectedError(message string, arg ...any) UnexpectedError {
return UnexpectedError{
Err: fmt.Errorf(message, arg...),
Stack: debug.Stack(),
}
return NewUnexpectedErrorFromCause(fmt.Errorf(message, arg...))
}

func NewUnexpectedErrorFromCause(err error) UnexpectedError {
var stack []byte
if StackTracesEnabled {
stack = debug.Stack()
}
return UnexpectedError{
Err: err,
Stack: debug.Stack(),
Stack: stack,
}
}

Expand All @@ -169,7 +172,12 @@ func (e UnexpectedError) Unwrap() error {
}

func (e UnexpectedError) Error() string {
return fmt.Sprintf("internal error: %s\n%s", e.Err.Error(), e.Stack)
message := e.Err.Error()
if len(e.Stack) == 0 {
return fmt.Sprintf("unexpected error: %s", message)
} else {
return fmt.Sprintf("unexpected error: %s\n%s", message, e.Stack)
}
}

// DefaultUserError is the default implementation of UserError interface.
Expand Down

0 comments on commit 158c82f

Please sign in to comment.