Skip to content

Commit ca597a9

Browse files
committed
增加 Code 函数获取 error 对应的 code
1 parent 22f1fe6 commit ca597a9

7 files changed

Lines changed: 56 additions & 2 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.3.0
4+
5+
> 此版本发布于 2023-06-18
6+
7+
* 增加 Code 函数获取 error 对应的 code
8+
39
### v0.2.1
410

511
> 此版本发布于 2023-04-24

README.en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func main() {
6161
// Use WithMsg to carry a message.
6262
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
6363
fmt.Println(err.Error())
64+
fmt.Println(errors.Code(err))
6465
fmt.Println(errors.Msg(err))
6566
}
6667
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func main() {
6161
// Use WithMsg to carry a message.
6262
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
6363
fmt.Println(err.Error())
64+
fmt.Println(errors.Code(err))
6465
fmt.Println(errors.Msg(err))
6566
}
6667
```

_examples/basic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ func main() {
4747
// Use WithMsg to carry a message.
4848
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
4949
fmt.Println(err.Error())
50+
fmt.Println(errors.Code(err))
5051
fmt.Println(errors.Msg(err))
5152
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

errors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
"fmt"
1010
)
1111

12+
const (
13+
codeNil = 0
14+
codeUnknown = 1
15+
)
16+
1217
const (
1318
nilString = "<nil>"
1419
)
@@ -114,6 +119,20 @@ func Is(err error, code int32) bool {
114119
return ok
115120
}
116121

122+
// Code returns the code of err.
123+
func Code(err error) int32 {
124+
if err == nil {
125+
return codeNil
126+
}
127+
128+
e, ok := err.(*Error)
129+
if !ok {
130+
return codeUnknown
131+
}
132+
133+
return e.code
134+
}
135+
117136
// Msg returns the msg of err.
118137
func Msg(err error) string {
119138
if err == nil {

errors_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package errors
66

77
import (
88
"errors"
9+
"io"
910
"testing"
1011
)
1112

@@ -24,6 +25,31 @@ func TestError(t *testing.T) {
2425
}
2526
}
2627

28+
// go test -v -cover -run=^TestCode$
29+
func TestCode(t *testing.T) {
30+
code := int32(500)
31+
32+
got := Code(nil)
33+
if got != codeNil {
34+
t.Errorf("got %d is wrong", got)
35+
}
36+
37+
got = Code(Wrap(nil, code))
38+
if got != codeNil {
39+
t.Errorf("got %d is wrong", got)
40+
}
41+
42+
got = Code(io.EOF)
43+
if got != codeUnknown {
44+
t.Errorf("got %d is wrong", got)
45+
}
46+
47+
got = Code(Wrap(errors.New("500"), code))
48+
if got != code {
49+
t.Errorf("got %d is wrong", got)
50+
}
51+
}
52+
2753
// go test -v -cover -run=^TestMsg$
2854
func TestMsg(t *testing.T) {
2955
code := int32(500)

0 commit comments

Comments
 (0)