Skip to content
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

fix: Correct errors.WithStack behaviour #984

Merged
merged 8 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ func NewKV(key string, value any) KV {
// pairs provided.
//
// A stacktrace will be yielded if formatting with a `+`, e.g `fmt.Sprintf("%+v", err)`.
// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func New(message string, keyvals ...KV) error {
return newError(message, keyvals...)
return withStackTrace(message, 1, keyvals...)
}

// Wrap creates a new error of the given message that contains
// the given inner error, suffixing any key-value pairs provided.
// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func Wrap(message string, inner error, keyvals ...KV) error {
err := newError(message, keyvals...)
err := withStackTrace(message, 1, keyvals...)
err.inner = inner
return err
}
Expand All @@ -54,19 +60,28 @@ func Is(err, target error) bool {
return errors.Is(err, target)
}

// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func WithStack(err error, keyvals ...KV) error {
return withStackTrace(err.Error(), keyvals...)
return withStackTrace(err.Error(), 1, keyvals...)
}

func newError(message string, keyvals ...KV) *defraError {
return withStackTrace(message, keyvals...)
}

func withStackTrace(message string, keyvals ...KV) *defraError {
// withStackTrace creates a `defraError` with a stacktrace and the given key-value pairs.
//
// The stacktrace will skip the top `depthToSkip` frames, allowing frames/calls generated from
// within this package to not polute the resultant stacktrace.
//
// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func withStackTrace(message string, depthToSkip int, keyvals ...KV) *defraError {
stackBuffer := make([]uintptr, MaxStackDepth)

// Skip the first X frames as they are part of this library (and dependencies) and are
// best hidden.
length := runtime.Callers(4, stackBuffer[:])
// best hidden, also account for any parent calls within this library.
const depthFromHereToSkip int = 2
length := runtime.Callers(depthFromHereToSkip+depthToSkip, stackBuffer[:])
stack := stackBuffer[:length]
stackText := toString(stack)

Expand Down
70 changes: 24 additions & 46 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,20 @@ func TestErrorIsDefraError(t *testing.T) {
}

func TestErrorWithStack(t *testing.T) {
err := errors.New("gndjdhs")
errorMessage := "gndjdhs"
err := errors.New(errorMessage)

errWithStach := WithStack(err)
errWithStack := WithStack(err)

result := fmt.Sprintf("%+v", errWithStach)
result := fmt.Sprintf("%+v", errWithStack)

/*
The Go test flag `-race` messes with the stacktrace causing this function's frame to be ommited from
the stacktrace, as our CI runs with the `-race` flag, these assertions need to be disabled.
// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)

// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktrace: err := Error\\(errorMessage\\)", result)
*/

// As noted above, we cannot assert that this function's stack frame is included in the trace,
// however we should still assert that the error message is present.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: ", err.Error()), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorWithStack: errWithStack := WithStack\\(err\\)", result)

// Assert that the next line of the stacktrace is also present.
assert.Regexp(t, ".*\\/testing/testing.go:[0-9]+ \\([a-zA-Z0-9]*\\)", result)
Expand Down Expand Up @@ -146,23 +139,16 @@ func TestErrorFmtvWithStacktrace(t *testing.T) {
const errorMessage string = "gndjdhs"

err := New(errorMessage)
result := fmt.Sprintf("%+v", err)

/*
The Go test flag `-race` messes with the stacktrace causing this function's frame to be ommited from
the stacktrace, as our CI runs with the `-race` flag, these assertions need to be disabled.
result := fmt.Sprintf("%+v", err)

// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktrace: err := Error\\(errorMessage\\)", result)
*/
// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)

// As noted above, we cannot assert that this function's stack frame is included in the trace,
// however we should still assert that the error message is present.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: ", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktrace: err := New\\(errorMessage\\)", result)

// Assert that the next line of the stacktrace is also present.
assert.Regexp(t, ".*\\/testing/testing.go:[0-9]+ \\([a-zA-Z0-9]*\\)", result)
Expand All @@ -174,21 +160,13 @@ func TestErrorFmtvWithStacktraceAndKvps(t *testing.T) {
err := New(errorMessage, NewKV("Kv1", 1), NewKV("Kv2", "2"))
result := fmt.Sprintf("%+v", err)

/*
The Go test flag `-race` messes with the stacktrace causing this function's frame to be ommited from
the stacktrace, as our CI runs with the `-race` flag, these assertions need to be disabled.

// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Kv1: 1, Kv2: 2\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktraceAndKvps: err := Error\\(errorMessage\\)", result)
*/

// As noted above, we cannot assert that this function's stack frame is included in the trace,
// however we should still assert that the error message is present.
assert.Regexp(t, fmt.Sprintf("^%s\\. Kv1: 1, Kv2: 2\\. Stack: ", errorMessage), result)
// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Kv1: 1, Kv2: 2\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)

// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktraceAndKvps: err := New\\(errorMessage, NewKV\\(\"Kv1\", 1\\), NewKV\\(\"Kv2\", \"2\"\\)\\)", result)

// Assert that the next line of the stacktrace is also present.
assert.Regexp(t, ".*\\/testing/testing.go:[0-9]+ \\([a-zA-Z0-9]*\\)", result)
Expand Down