-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclassification_test.go
More file actions
45 lines (41 loc) · 1.31 KB
/
Copy pathclassification_test.go
File metadata and controls
45 lines (41 loc) · 1.31 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
package errors_test
import (
"context"
"net/http"
"testing"
"github.com/stackus/errors"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
)
func TestClassificationOfContextAndJoinedErrors(t *testing.T) {
type testCase struct {
err error
typeCode string
httpCode int
grpcCode codes.Code
}
tests := map[string]testCase{
"canceled context": {
context.Canceled, "CANCELED", http.StatusRequestTimeout, codes.Canceled,
},
"deadline exceeded": {
context.DeadlineExceeded, "DEADLINE_EXCEEDED", http.StatusGatewayTimeout, codes.DeadlineExceeded,
},
"wrapped context error": {
errors.Wrap(context.Canceled, "operation canceled"), "CANCELED", http.StatusRequestTimeout, codes.Canceled,
},
"joined identical categories": {
errors.Join(errors.ErrNotFound, errors.ErrNotFound.Msg("missing record")), "NOT_FOUND", http.StatusNotFound, codes.NotFound,
},
"joined classified and ordinary errors": {
errors.Join(errors.ErrNotFound, errors.New("database unavailable")), "UNKNOWN", http.StatusInternalServerError, codes.Unknown,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
require.Equal(t, tt.typeCode, errors.TypeCode(tt.err))
require.Equal(t, tt.httpCode, errors.HTTPCode(tt.err))
require.Equal(t, tt.grpcCode, errors.GRPCCode(tt.err))
})
}
}