From 85abbfa214c777aac1e0685097b57a2b324746f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Thu, 25 Jul 2024 14:44:04 -0700 Subject: [PATCH 1/2] Revert "Remove stacktrace from errors" --- runtime/errors/errors.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/runtime/errors/errors.go b/runtime/errors/errors.go index 949f4896d9..a6bbd39fa1 100644 --- a/runtime/errors/errors.go +++ b/runtime/errors/errors.go @@ -20,6 +20,7 @@ package errors import ( "fmt" + "runtime/debug" "golang.org/x/xerrors" ) @@ -141,7 +142,8 @@ 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 _ InternalError = UnexpectedError{} @@ -150,13 +152,15 @@ func (UnexpectedError) IsInternalError() {} func NewUnexpectedError(message string, arg ...any) UnexpectedError { return UnexpectedError{ - Err: fmt.Errorf(message, arg...), + Err: fmt.Errorf(message, arg...), + Stack: debug.Stack(), } } func NewUnexpectedErrorFromCause(err error) UnexpectedError { return UnexpectedError{ - Err: err, + Err: err, + Stack: debug.Stack(), } } @@ -165,7 +169,7 @@ func (e UnexpectedError) Unwrap() error { } func (e UnexpectedError) Error() string { - return fmt.Sprintf("internal error: %s", e.Err.Error()) + return fmt.Sprintf("internal error: %s\n%s", e.Err.Error(), e.Stack) } // DefaultUserError is the default implementation of UserError interface. From 158c82fb57ef1d17690782b1bcb040eaaa66eafb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Thu, 25 Jul 2024 14:50:47 -0700 Subject: [PATCH 2/2] put stack traces behind feature flag --- runtime/errors/errors.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/runtime/errors/errors.go b/runtime/errors/errors.go index a6bbd39fa1..d465852747 100644 --- a/runtime/errors/errors.go +++ b/runtime/errors/errors.go @@ -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, } } @@ -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.