Skip to content

Commit a0fc3e0

Browse files
committed
废弃 Match 类函数,提供 Is 类函数
1 parent 40d1679 commit a0fc3e0

9 files changed

Lines changed: 94 additions & 141 deletions

File tree

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## ✒ 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.8.0
4+
5+
> 此版本发布于 2026-01-26
6+
7+
* 废弃 Match 类函数,提供 Is 类函数
8+
39
### v0.7.2
410

511
> 此版本发布于 2024-08-21

README.en.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,13 @@ func main() {
5454
berr := errors.BadRequest("id is wrong")
5555
ferr := errors.Forbidden("user isn't allowed")
5656
nerr := errors.NotFound("book not found")
57-
rerr := errors.RequireLogin("user requires login")
58-
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)
59-
60-
isBadRequest := errors.MatchBadRequest(berr)
61-
isForbidden := errors.MatchForbidden(ferr)
62-
isNotFound := errors.MatchNotFound(nerr)
63-
isRequireLogin := errors.MatchRequireLogin(rerr)
64-
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\nisRequireLogin: %+v\n", isBadRequest, isForbidden, isNotFound, isRequireLogin)
65-
}
57+
fmt.Printf("%+v\n%+v\n%+v\n", berr, ferr, nerr)
6658

59+
isBadRequest := errors.IsBadRequest(berr)
60+
isForbidden := errors.IsForbidden(ferr)
61+
isNotFound := errors.IsNotFound(nerr)
62+
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\n", isBadRequest, isForbidden, isNotFound)
63+
}
6764
```
6865

6966
* [basic](_examples/basic.go)

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,13 @@ func main() {
5454
berr := errors.BadRequest("id is wrong")
5555
ferr := errors.Forbidden("user isn't allowed")
5656
nerr := errors.NotFound("book not found")
57-
rerr := errors.RequireLogin("user requires login")
58-
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)
59-
60-
isBadRequest := errors.MatchBadRequest(berr)
61-
isForbidden := errors.MatchForbidden(ferr)
62-
isNotFound := errors.MatchNotFound(nerr)
63-
isRequireLogin := errors.MatchRequireLogin(rerr)
64-
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\nisRequireLogin: %+v\n", isBadRequest, isForbidden, isNotFound, isRequireLogin)
65-
}
57+
fmt.Printf("%+v\n%+v\n%+v\n", berr, ferr, nerr)
6658

59+
isBadRequest := errors.IsBadRequest(berr)
60+
isForbidden := errors.IsForbidden(ferr)
61+
isNotFound := errors.IsNotFound(nerr)
62+
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\n", isBadRequest, isForbidden, isNotFound)
63+
}
6764
```
6865

6966
* [basic](_examples/basic.go)

_examples/basic.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ func main() {
4040
berr := errors.BadRequest("id is wrong")
4141
ferr := errors.Forbidden("user isn't allowed")
4242
nerr := errors.NotFound("book not found")
43-
rerr := errors.RequireLogin("user requires login")
44-
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)
45-
46-
isBadRequest := errors.MatchBadRequest(berr)
47-
isForbidden := errors.MatchForbidden(ferr)
48-
isNotFound := errors.MatchNotFound(nerr)
49-
isRequireLogin := errors.MatchRequireLogin(rerr)
50-
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\nisRequireLogin: %+v\n", isBadRequest, isForbidden, isNotFound, isRequireLogin)
43+
fmt.Printf("%+v\n%+v\n%+v\n", berr, ferr, nerr)
44+
45+
isBadRequest := errors.IsBadRequest(berr)
46+
isForbidden := errors.IsForbidden(ferr)
47+
isNotFound := errors.IsNotFound(nerr)
48+
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\n", isBadRequest, isForbidden, isNotFound)
5149
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

types.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
package errors
66

77
const (
8-
codeBadRequest = 400
9-
codeForbidden = 403
10-
codeNotFound = 404
11-
codeRequireLogin = 1000
8+
codeBadRequest = 400
9+
codeForbidden = 403
10+
codeNotFound = 404
1211
)
1312

1413
// BadRequest returns *Error with bad request code.
@@ -26,27 +25,35 @@ func NotFound(message string, args ...any) *Error {
2625
return Wrap(codeNotFound, message, args...)
2726
}
2827

29-
// RequireLogin returns *Error with require login code.
30-
func RequireLogin(message string, args ...any) *Error {
31-
return Wrap(codeRequireLogin, message, args...)
28+
// IsBadRequest checks err with bad request code.
29+
func IsBadRequest(err error) bool {
30+
return IsCode(err, codeBadRequest)
31+
}
32+
33+
// IsForbidden checks err with forbidden code.
34+
func IsForbidden(err error) bool {
35+
return IsCode(err, codeForbidden)
36+
}
37+
38+
// IsNotFound checks err with not found code.
39+
func IsNotFound(err error) bool {
40+
return IsCode(err, codeNotFound)
3241
}
3342

3443
// MatchBadRequest matches err with bad request code.
44+
// Deprecated: Use IsBadRequest instead because this name is 'ugly' to me.
3545
func MatchBadRequest(err error) bool {
36-
return Match(err, codeBadRequest)
46+
return IsBadRequest(err)
3747
}
3848

3949
// MatchForbidden matches err with forbidden code.
50+
// Deprecated: Use IsForbidden instead because this name is 'ugly' to me.
4051
func MatchForbidden(err error) bool {
41-
return Match(err, codeForbidden)
52+
return IsForbidden(err)
4253
}
4354

4455
// MatchNotFound matches err with not found code.
56+
// Deprecated: Use IsNotFound instead because this name is 'ugly' to me.
4557
func MatchNotFound(err error) bool {
46-
return Match(err, codeNotFound)
47-
}
48-
49-
// MatchRequireLogin matches err with require login code.
50-
func MatchRequireLogin(err error) bool {
51-
return Match(err, codeRequireLogin)
58+
return IsNotFound(err)
5259
}

types_test.go

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -126,48 +126,8 @@ func TestNotFound(t *testing.T) {
126126
}
127127
}
128128

129-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestRequireLogin$
130-
func TestRequireLogin(t *testing.T) {
131-
testCases := []struct {
132-
message string
133-
args []any
134-
wantCode int32
135-
wantMessage string
136-
}{
137-
{
138-
message: "xxx",
139-
args: nil,
140-
wantCode: codeRequireLogin,
141-
wantMessage: "xxx",
142-
},
143-
{
144-
message: "xxx %d%.2f",
145-
args: nil,
146-
wantCode: codeRequireLogin,
147-
wantMessage: "xxx %d%.2f",
148-
},
149-
{
150-
message: "xxx %d%s%+v",
151-
args: []any{1, ".", true},
152-
wantCode: codeRequireLogin,
153-
wantMessage: "xxx 1.true",
154-
},
155-
}
156-
157-
for _, testCase := range testCases {
158-
err := RequireLogin(testCase.message, testCase.args...)
159-
if err.Code() != testCase.wantCode {
160-
t.Errorf("err.Code() %d != testCase.wantCode %d", err.Code(), testCase.wantCode)
161-
}
162-
163-
if err.Message() != testCase.wantMessage {
164-
t.Errorf("err.Message() %s != testCase.wantMessage %s", err.Message(), testCase.wantMessage)
165-
}
166-
}
167-
}
168-
169-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchBadRequest$
170-
func TestMatchBadRequest(t *testing.T) {
129+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsBadRequest$
130+
func TestIsBadRequest(t *testing.T) {
171131
testCases := []struct {
172132
message string
173133
}{
@@ -178,14 +138,14 @@ func TestMatchBadRequest(t *testing.T) {
178138

179139
for _, testCase := range testCases {
180140
err := BadRequest(testCase.message)
181-
if !MatchBadRequest(err) {
182-
t.Errorf("err %+v not match code %d", err, err.Code())
141+
if !IsBadRequest(err) {
142+
t.Errorf("err %+v not code %d", err, err.Code())
183143
}
184144
}
185145
}
186146

187-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchForbidden$
188-
func TestMatchForbidden(t *testing.T) {
147+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsForbidden$
148+
func TestIsForbidden(t *testing.T) {
189149
testCases := []struct {
190150
message string
191151
}{
@@ -196,14 +156,14 @@ func TestMatchForbidden(t *testing.T) {
196156

197157
for _, testCase := range testCases {
198158
err := Forbidden(testCase.message)
199-
if !MatchForbidden(err) {
200-
t.Errorf("err %+v not match code %d", err, err.Code())
159+
if !IsForbidden(err) {
160+
t.Errorf("err %+v not code %d", err, err.Code())
201161
}
202162
}
203163
}
204164

205-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchNotFound$
206-
func TestMatchNotFound(t *testing.T) {
165+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsNotFound$
166+
func TestIsNotFound(t *testing.T) {
207167
testCases := []struct {
208168
message string
209169
}{
@@ -214,26 +174,8 @@ func TestMatchNotFound(t *testing.T) {
214174

215175
for _, testCase := range testCases {
216176
err := NotFound(testCase.message)
217-
if !MatchNotFound(err) {
218-
t.Errorf("err %+v not match code %d", err, err.Code())
219-
}
220-
}
221-
}
222-
223-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchRequireLogin$
224-
func TestMatchRequireLogin(t *testing.T) {
225-
testCases := []struct {
226-
message string
227-
}{
228-
{
229-
message: "xxx",
230-
},
231-
}
232-
233-
for _, testCase := range testCases {
234-
err := RequireLogin(testCase.message)
235-
if !MatchRequireLogin(err) {
236-
t.Errorf("err %+v not match code %d", err, err.Code())
177+
if !IsNotFound(err) {
178+
t.Errorf("err %+v not code %d", err, err.Code())
237179
}
238180
}
239181
}

unwrap.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func CodeMessage(err error, defaultCode int32, defaultMessage string, args ...an
6767
return code, message
6868
}
6969

70-
// Match unwraps error and check if its code equals to code.
71-
func Match(err error, code int32) bool {
70+
// IsCode unwraps error and check if its code equals to code.
71+
func IsCode(err error, code int32) bool {
7272
if err == nil {
7373
return code == 0
7474
}
@@ -87,3 +87,9 @@ func Match(err error, code int32) bool {
8787

8888
return false
8989
}
90+
91+
// Match unwraps error and check if its code equals to code.
92+
// Deprecated: Use IsCode instead because this name is 'ugly' to me.
93+
func Match(err error, code int32) bool {
94+
return IsCode(err, code)
95+
}

unwrap_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -193,51 +193,51 @@ func TestCodeMessage(t *testing.T) {
193193
}
194194
}
195195

196-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatch$
197-
func TestMatch(t *testing.T) {
196+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsCode$
197+
func TestIsCode(t *testing.T) {
198198
testErr := &testError{}
199199

200200
testCases := []struct {
201-
err error
202-
code int32
203-
match bool
201+
err error
202+
code int32
203+
is bool
204204
}{
205205
{
206-
err: nil,
207-
code: 0,
208-
match: true,
206+
err: nil,
207+
code: 0,
208+
is: true,
209209
},
210210
{
211-
err: nil,
212-
code: 999,
213-
match: false,
211+
err: nil,
212+
code: 999,
213+
is: false,
214214
},
215215
{
216-
err: io.EOF,
217-
code: 999,
218-
match: false,
216+
err: io.EOF,
217+
code: 999,
218+
is: false,
219219
},
220220
{
221-
err: testErr,
222-
code: testErr.Code(),
223-
match: true,
221+
err: testErr,
222+
code: testErr.Code(),
223+
is: true,
224224
},
225225
{
226-
err: Wrap(1000, "wow"),
227-
code: 1000,
228-
match: true,
226+
err: Wrap(1000, "wow"),
227+
code: 1000,
228+
is: true,
229229
},
230230
{
231-
err: Wrap(1000, "eof").With(io.EOF),
232-
code: 1000,
233-
match: true,
231+
err: Wrap(1000, "eof").With(io.EOF),
232+
code: 1000,
233+
is: true,
234234
},
235235
}
236236

237237
for _, testCase := range testCases {
238-
match := Match(testCase.err, testCase.code)
239-
if match != testCase.match {
240-
t.Errorf("match %+v != testCase.match %+v", match, testCase.match)
238+
is := IsCode(testCase.err, testCase.code)
239+
if is != testCase.is {
240+
t.Errorf("got %+v != want %+v", is, testCase.is)
241241
}
242242
}
243243
}

0 commit comments

Comments
 (0)