forked from asahasrabuddhe/zapdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
61 lines (50 loc) · 1.61 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package zapdriver
import (
"os"
"regexp"
"runtime"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
type fakeErr struct{}
// manually set the frames to allow asserting stacktraces
func (fakeErr) StackTrace() errors.StackTrace {
pc1, _, _, _ := runtime.Caller(0)
pc2, _, _, _ := runtime.Caller(0)
return []errors.Frame{
errors.Frame(pc1),
errors.Frame(pc2),
}
}
func (fakeErr) Error() string {
return "fake error: underlying error"
}
func TestFmtStack(t *testing.T) {
stacktrace := stackdriverFmtError{fakeErr{}}.Error()
assert.Equal(t, `fake error: underlying error
goroutine 1 [running]:
github.com/blendle/zapdriver.fakeErr.StackTrace()
/error_test.go:18 +0x1337
github.com/blendle/zapdriver.fakeErr.StackTrace()
/error_test.go:19 +0x1337`, makeStackTraceStable(stacktrace))
}
// cleanup local paths & local function pointers
func makeStackTraceStable(str string) string {
re := regexp.MustCompile(`(?m)^\t.+(\/\S+:\d+) \+0x.+$`)
str = re.ReplaceAllString(str, "\t${1} +0x1337")
dir, _ := os.Getwd()
str = strings.ReplaceAll(str, dir, "")
return str
}
func ExampleSkipFmtStackTraces() {
logger, _ := NewProduction()
logger.Error("with exception", zap.Error(errors.New("internal error")), ErrorReport(runtime.Caller(0)))
logger, _ = NewProduction(WrapCore(ServiceName("service"), ReportAllErrors(true)))
logger.Error("with exception", zap.Error(errors.New("internal error")))
logger, _ = NewProduction(WrapCore(ServiceName("service"), SkipFmtStackTraces(true)))
logger.Error("without exception", zap.Error(errors.New("internal error")))
// Output:
}