-
Notifications
You must be signed in to change notification settings - Fork 0
/
errcode_test.go
180 lines (155 loc) · 3.57 KB
/
errcode_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package errcode
import (
"errors"
"fmt"
"math/rand"
"net/http"
"testing"
)
func randomUniqueCode() int {
code := rand.Int()
if _, ok := codes[code]; ok {
return randomUniqueCode()
}
return code
}
func TestNewError(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("duplicate code: not panic")
}
}()
code := randomUniqueCode()
_ = NewError(code, http.StatusInternalServerError, "Internal Server Error")
_ = NewError(code, http.StatusBadRequest, "Bad Request")
}
func TestUnwrap(t *testing.T) {
err := errors.New("error")
ec := NewError(randomUniqueCode(), http.StatusInternalServerError, "Error")
tests := []struct {
name string
arg error
want error
}{
{
name: "unwrap",
arg: err,
want: err,
},
{
name: "unwrap with code",
arg: ec,
want: ec,
},
{
name: "wrap",
arg: fmt.Errorf("wrap: %w", fmt.Errorf("wrap: %w", err)),
want: err,
},
{
name: "wrap with code",
arg: fmt.Errorf("wrap: %w", fmt.Errorf("wrap: %w", ec)),
want: ec,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Unwrap(tt.arg); !errors.Is(got, tt.want) {
t.Errorf("Unwrap() = %v, want %v", got, tt.want)
}
})
}
}
func TestHardUnwrap(t *testing.T) {
err := errors.New("error")
ec := NewError(randomUniqueCode(), http.StatusInternalServerError, "Error")
tests := []struct {
name string
arg error
want error
}{
{
name: "unwrap",
arg: err,
want: nil,
},
{
name: "unwrap with code",
arg: ec,
want: ec,
},
{
name: "wrap",
arg: fmt.Errorf("wrap: %w", fmt.Errorf("wrap: %w", err)),
want: nil,
},
{
name: "wrap with code",
arg: fmt.Errorf("wrap: %w", fmt.Errorf("wrap: %w", ec)),
want: ec,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, gotOK := HardUnwrap(tt.arg)
if tt.want == nil {
if gotOK {
t.Errorf("HardUnwrap() = %v, want nil", got)
}
return
}
if !gotOK || !errors.Is(got, tt.want) {
t.Errorf("HardUnwrap() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleNewError() {
err := NewError(1, http.StatusInternalServerError, "Internal Server Error")
fmt.Println(err)
// Output: 1 - Internal Server Error
}
func ExampleError_HTTPStatusCode() {
err := NewError(2, http.StatusNotFound, "Not Found")
fmt.Println(err.HTTPStatusCode())
// Output: 404
}
func ExampleError_Message() {
err := NewError(3, http.StatusBadRequest, "Invalid %v", "username")
fmt.Println(err.Message())
// Output: Invalid username
}
func ExampleUnwrap() {
// Define error
ec := NewError(4, http.StatusBadRequest, "Invalid %v")
// Wrap error in many times (on many functions) for easy tracking
err := fmt.Errorf("service: %w", fmt.Errorf("repo: %w", ec.WithArgs("username")))
// Unwrap to get code, http status code and message to respond to the client
u := Unwrap(err)
fmt.Println(err)
fmt.Println(u)
// Output:
// service: repo: 4 - Invalid username
// 4 - Invalid username
}
func ExampleHardUnwrap() {
// Define error
ec := NewError(5, http.StatusBadRequest, "Invalid %v")
// Wrap error in many times (on many functions) for easy tracking
err := fmt.Errorf("service: %w", fmt.Errorf("repo: %w", ec.WithArgs("username")))
// Unwrap to get code, http status code and message to respond to the client
u, ok := HardUnwrap(err)
fmt.Println(err)
fmt.Println(u)
fmt.Println(ok)
fmt.Println(u.Code())
fmt.Println(u.HTTPStatusCode())
fmt.Println(u.Message())
// Output:
// service: repo: 5 - Invalid username
// 5 - Invalid username
// true
// 5
// 400
// Invalid username
}