Skip to content

Commit 9c3b0c0

Browse files
committed
增加 RegisterStatuses 函数
1 parent 4212928 commit 9c3b0c0

5 files changed

Lines changed: 48 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.5.1
4+
5+
> 此版本发布于 2023-06-19
6+
7+
* 增加 RegisterStatuses 函数
8+
39
### v0.5.0
410

511
> 此版本发布于 2023-06-19

_examples/status.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ func main() {
5353
// Try to remove errors.WithMsg("hello") above and run again.
5454
code, msg = status.Parse(err)
5555
fmt.Println(code, msg)
56+
57+
// Also, you can register many statuses at one time by RegisterStatuses.
58+
status.RegisterStatuses(
59+
status.New(123, "abc", nil),
60+
status.New(666, "xxx", nil),
61+
)
5662
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

status/status.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func RegisterStatus(status *Status) {
7070
statusesLock.Unlock()
7171
}
7272

73+
// RegisterStatuses registers some statuses to local statuses so that we can use it in Parse.
74+
func RegisterStatuses(registerStatus ...*Status) {
75+
statusesLock.Lock()
76+
defer statusesLock.Unlock()
77+
78+
for _, status := range registerStatus {
79+
statuses = append(statuses, status)
80+
}
81+
}
82+
7383
// Parse parses err and returns its code and error created from msg.
7484
func Parse(err error) (int32, string) {
7585
if err == nil {

status/status_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ func TestRegisterStatus(t *testing.T) {
2525
}
2626
}
2727

28+
// go test -v -cover -run=^TestRegisterStatuses$
29+
func TestRegisterStatuses(t *testing.T) {
30+
registered := []*Status{
31+
New(123, "abc", nil),
32+
New(666, "xxx", nil),
33+
}
34+
35+
RegisterStatuses(registered...)
36+
37+
for i := range registered {
38+
found := false
39+
for _, status := range statuses {
40+
if status == registered[i] {
41+
found = true
42+
break
43+
}
44+
}
45+
46+
if !found {
47+
t.Error("registered status not found", statuses)
48+
}
49+
}
50+
}
51+
2852
func TestParse(t *testing.T) {
2953
codeBadRequest := int32(http.StatusBadRequest)
3054
codeForbidden := int32(http.StatusForbidden)

0 commit comments

Comments
 (0)