-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcustom_error_test.go
131 lines (119 loc) · 3 KB
/
custom_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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package api2
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/starius/api2"
)
type MyError struct {
MyCode int
}
func (e MyError) Error() string {
return "my error"
}
func TestErrorType(t *testing.T) {
type HelloRequest struct {
Ok bool
MyError bool
WrappedError bool
DoublyWrappedError bool
GenericError bool
WrappedGenericError bool
}
type HelloResponse struct {
}
helloHandler := func(ctx context.Context, req *HelloRequest) (res *HelloResponse, err error) {
if req.Ok {
return &HelloResponse{}, nil
}
if req.MyError {
return nil, MyError{MyCode: 123}
}
if req.WrappedError {
return nil, fmt.Errorf("failed: %w", MyError{MyCode: 123})
}
if req.DoublyWrappedError {
return nil, fmt.Errorf("failed: %w", fmt.Errorf("error: %w", MyError{MyCode: 123}))
}
if req.GenericError {
return nil, errors.New("generic error")
}
if req.WrappedGenericError {
return nil, fmt.Errorf("error: %w", errors.New("generic error"))
}
panic("bad input")
}
routes := []api2.Route{
{
Method: http.MethodPost,
Path: "/hello",
Handler: helloHandler,
Transport: &api2.JsonTransport{
Errors: map[string]error{
"MyError": MyError{},
},
},
},
}
mux := http.NewServeMux()
api2.BindRoutes(mux, routes)
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := api2.NewClient(routes, server.URL)
ctx := context.Background()
t.Run("ok", func(t *testing.T) {
helloRes := &HelloResponse{}
err := client.Call(ctx, helloRes, &HelloRequest{Ok: true})
if err != nil {
t.Errorf("Hello failed: %v.", err)
}
})
t.Run("my error", func(t *testing.T) {
requests := map[string]*HelloRequest{
"my error": &HelloRequest{MyError: true},
"wrapped error": &HelloRequest{WrappedError: true},
"doubly wrapped error": &HelloRequest{DoublyWrappedError: true},
}
for name, req := range requests {
t.Run(name, func(t *testing.T) {
helloRes := &HelloResponse{}
err := client.Call(ctx, helloRes, req)
if err == nil {
t.Fatalf("Hello did not fail.")
}
myErr, ok := err.(MyError)
if !ok {
t.Fatalf("The error is not MyError.")
}
if myErr.MyCode != 123 {
t.Fatalf("MyCode is %d, want %d.", myErr.MyCode, 123)
}
})
}
})
t.Run("generic error", func(t *testing.T) {
requests := map[string]*HelloRequest{
"generic error": &HelloRequest{GenericError: true},
"wrapped generic error": &HelloRequest{WrappedGenericError: true},
}
for name, req := range requests {
t.Run(name, func(t *testing.T) {
helloRes := &HelloResponse{}
err := client.Call(ctx, helloRes, req)
if err == nil {
t.Fatalf("Hello did not fail.")
}
if !strings.Contains(err.Error(), "generic error") {
t.Fatalf("Error is %q, should contain %q.", err.Error(), "generic error")
}
if _, ok := err.(MyError); ok {
t.Fatalf("The error is MyError.")
}
})
}
})
}