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
|
package structerr
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestGRPCCode(t *testing.T) {
t.Parallel()
for desc, tc := range map[string]struct {
in error
exp codes.Code
}{
"unwrapped status": {
in: status.Error(codes.NotFound, ""),
exp: codes.NotFound,
},
"wrapped status": {
in: fmt.Errorf("context: %w", status.Error(codes.NotFound, "")),
exp: codes.NotFound,
},
"unwrapped status created by helpers": {
in: NewNotFound(""),
exp: codes.NotFound,
},
"wrapped status created by helpers": {
in: fmt.Errorf("context: %w", NewNotFound("")),
exp: codes.NotFound,
},
"double wrapped status created by helpers": {
in: fmt.Errorf("outer: %w", fmt.Errorf("context: %w", NewNotFound(""))),
exp: codes.NotFound,
},
"double helper wrapped status": {
in: NewInvalidArgument("outer: %w", fmt.Errorf("context: %w", NewNotFound(""))),
exp: codes.NotFound,
},
"nil input": {
in: nil,
exp: codes.OK,
},
"no code defined": {
in: errors.New("an error"),
exp: codes.Unknown,
},
} {
t.Run(desc, func(t *testing.T) {
require.Equal(t, tc.exp, GRPCCode(tc.in))
})
}
}
|