-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
87 lines (76 loc) · 2.13 KB
/
error_test.go
File metadata and controls
87 lines (76 loc) · 2.13 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package errors
import (
"errors"
"testing"
assert "github.com/stretchr/testify/assert"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
type FooError struct {
Code int `json:"errorCode"`
Message string `json:"errorMessage"`
cause error
stack stack
rpcCode codes.Code
}
func NewFooError(Message string, cause ...error) *FooError {
var c error
if len(cause) > 0 {
c = cause[0]
}
return &FooError{
Code: 999,
Message: Message,
cause: c,
stack: getTrace(),
rpcCode: codes.OK,
}
}
func (e *FooError) Error() string { return errorStr(e) }
func (e *FooError) Timeout() bool { return false }
func (e *FooError) Temporary() bool { return false }
func (e *FooError) GetCode() int { return e.Code }
func (e *FooError) GetMessage() string { return e.Message }
func (e *FooError) GetCause() error { return e.cause }
func (e *FooError) GetStack() stack { return e.stack }
func (e *FooError) GRPCStatus() *status.Status {
return status.New(e.rpcCode, e.Message)
}
type test struct {
foo *FooError
expectedInfo string
expectedVerbose string
expectedDebug string
}
var tests = []test{
{
foo: NewFooError("error Message 1"),
expectedInfo: "error 999: error Message 1",
expectedVerbose: "error 999: error Message 1",
},
{
foo: NewFooError("error Message 2", errors.New("causal error")),
expectedInfo: "error 999: error Message 2",
expectedVerbose: "error 999: error Message 2\ncause: causal error",
},
}
func TestVerbosity(t *testing.T) {
SetVerbosity(Info)
for _, test := range tests {
assert.Equal(t, test.expectedInfo, test.foo.Error())
}
SetVerbosity(Verbose)
for _, test := range tests {
assert.Equal(t, test.expectedVerbose, test.foo.Error())
}
// trace portion of this output prevents testing for string match in different contexts
SetVerbosity(Debug)
for _, test := range tests {
assert.NotNil(t, test.foo.Error())
}
// trace portion of this output prevents testing for string match in different contexts
SetVerbosity(Trace)
for _, test := range tests {
assert.NotNil(t, test.foo.Error())
}
}