Skip to content

Commit

Permalink
Merge pull request #3492 from onflow/revert-3392-supun/deterministic-…
Browse files Browse the repository at this point in the history
…errors
  • Loading branch information
turbolent authored Jul 25, 2024
2 parents 9ac4307 + 158c82f commit 85e1b4c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions runtime/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package errors

import (
"fmt"
"runtime/debug"

"golang.org/x/xerrors"
)
Expand Down Expand Up @@ -141,22 +142,28 @@ func (e MemoryError) Error() string {
//
// NOTE: This error is not used for errors occur due to bugs in a user-provided program.
type UnexpectedError struct {
Err error
Err error
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...),
}
return NewUnexpectedErrorFromCause(fmt.Errorf(message, arg...))
}

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

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

func (e UnexpectedError) Error() string {
return fmt.Sprintf("internal error: %s", e.Err.Error())
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 85e1b4c

Please sign in to comment.