implement a thin wrapper around stdlib errors (#5733)

This solves #5679 , and also implements #5515, #5509 and lays the ground for #5516

With the introduction of Wrap, Is and As in the standard library, we've now got built-in support for wrapping.

On top of that, a common pattern in the community is to define errors tailored to the context of each project while still conforming to the error and Unwrap interfaces (see Upspin, Chromium)

The output now includes stack traces and additional info
This commit is contained in:
Roberto Dip
2022-05-18 11:47:55 -03:00
committed by GitHub
parent ade929bc90
commit 894fa22c71
9 changed files with 655 additions and 213 deletions
+35
View File
@@ -0,0 +1,35 @@
package ctxerr
import (
"regexp"
"testing"
"github.com/stretchr/testify/require"
)
type mockStack struct {
trace []string
}
func (s mockStack) List() []string {
return s.trace
}
func buildStack(depth int) stack {
if depth == 0 {
return newStack(0)
}
return buildStack(depth - 1)
}
func TestStack(t *testing.T) {
trace := buildStack(maxDepth)
lines := trace.List()
require.Equal(t, len(lines), len(trace))
re := regexp.MustCompile(`server/contexts/ctxerr\.buildStack \(stack_test.go:\d+\)$`)
for i, line := range lines {
require.Regexpf(t, re, line, "expected line %d to match %q, got %q", i, re, line)
}
}