-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
js: Print stack trace when exception in handleSummary() (#3416)
Co-authored-by: Mihail Stoykov <312246+mstoykov@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
122 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package errext | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// Format formats the given error as a message (string) and a map of fields. | ||
// In case of [Exception], it uses the stack trace as the error message. | ||
// In case of [HasHint], it also adds the hint as a field. | ||
func Format(err error) (string, map[string]interface{}) { | ||
if err == nil { | ||
return "", nil | ||
} | ||
|
||
errText := err.Error() | ||
var xerr Exception | ||
if errors.As(err, &xerr) { | ||
errText = xerr.StackTrace() | ||
} | ||
|
||
fields := make(map[string]interface{}) | ||
var herr HasHint | ||
if errors.As(err, &herr) { | ||
fields["hint"] = herr.Hint() | ||
} | ||
|
||
return errText, fields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package errext_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.k6.io/k6/errext" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Nil", func(t *testing.T) { | ||
t.Parallel() | ||
errorText, fields := errext.Format(nil) | ||
assert.Equal(t, "", errorText) | ||
assert.Empty(t, fields) | ||
}) | ||
|
||
t.Run("Simple", func(t *testing.T) { | ||
t.Parallel() | ||
errorText, fields := errext.Format(errors.New("simple error")) | ||
assert.Equal(t, "simple error", errorText) | ||
assert.Empty(t, fields) | ||
}) | ||
|
||
t.Run("Exception", func(t *testing.T) { | ||
t.Parallel() | ||
err := fakeException{error: errors.New("simple error"), stack: "stack trace"} | ||
errorText, fields := errext.Format(err) | ||
assert.Equal(t, "stack trace", errorText) | ||
assert.Empty(t, fields) | ||
}) | ||
|
||
t.Run("Hint", func(t *testing.T) { | ||
t.Parallel() | ||
err := errext.WithHint(errors.New("error with hint"), "hint message") | ||
errorText, fields := errext.Format(err) | ||
assert.Equal(t, "error with hint", errorText) | ||
assert.Equal(t, map[string]interface{}{"hint": "hint message"}, fields) | ||
}) | ||
|
||
t.Run("ExceptionWithHint", func(t *testing.T) { | ||
t.Parallel() | ||
err := fakeException{error: errext.WithHint(errors.New("error with hint"), "hint message"), stack: "stack trace"} | ||
errorText, fields := errext.Format(err) | ||
assert.Equal(t, "stack trace", errorText) | ||
assert.Equal(t, map[string]interface{}{"hint": "hint message"}, fields) | ||
}) | ||
} | ||
|
||
type fakeException struct { | ||
error | ||
stack string | ||
abort errext.AbortReason | ||
} | ||
|
||
func (e fakeException) StackTrace() string { | ||
return e.stack | ||
} | ||
|
||
func (e fakeException) AbortReason() errext.AbortReason { | ||
return e.abort | ||
} | ||
|
||
func (e fakeException) Unwrap() error { | ||
return e.error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters