Skip to content

Commit b648d8b

Browse files
committed
增加 ParseOrDefault 函数
1 parent 9c3b0c0 commit b648d8b

3 files changed

Lines changed: 15 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.2
4+
5+
> 此版本发布于 2023-07-27
6+
7+
* 增加 ParseOrDefault 函数
8+
39
### v0.5.1
410

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

status/status.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ func RegisterStatuses(registerStatus ...*Status) {
8282

8383
// Parse parses err and returns its code and error created from msg.
8484
func Parse(err error) (int32, string) {
85+
return ParseOrDefault(err, CodeUnknown, MsgUnknown)
86+
}
87+
88+
// ParseOrDefault parses err and returns its code and error created from msg.
89+
func ParseOrDefault(err error, defaultCode int32, defaultMsg string) (int32, string) {
8590
if err == nil {
8691
return CodeOK, ""
8792
}
@@ -97,6 +102,6 @@ func Parse(err error) (int32, string) {
97102
}
98103
}
99104

100-
msg := errors.MsgOrDefault(err, MsgUnknown)
101-
return CodeUnknown, msg
105+
msg := errors.MsgOrDefault(err, defaultMsg)
106+
return defaultCode, msg
102107
}

status/status_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/FishGoddess/errors"
99
)
1010

11+
// go test -v -cover -run=^TestRegisterStatus$
1112
func TestRegisterStatus(t *testing.T) {
1213
registered := New(666, "xxx", nil)
1314
RegisterStatus(registered)
@@ -49,6 +50,7 @@ func TestRegisterStatuses(t *testing.T) {
4950
}
5051
}
5152

53+
// go test -v -cover -run=^TestParse$
5254
func TestParse(t *testing.T) {
5355
codeBadRequest := int32(http.StatusBadRequest)
5456
codeForbidden := int32(http.StatusForbidden)

0 commit comments

Comments
 (0)