Skip to content

Commit 4212928

Browse files
committed
增加 status 相关特性
1 parent 8c431d3 commit 4212928

14 files changed

Lines changed: 326 additions & 80 deletions

HISTORY.md

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

3+
### v0.5.0
4+
5+
> 此版本发布于 2023-06-19
6+
7+
* 优化代码,调整内置错误
8+
* 增加 status 相关特性
9+
310
### v0.4.0
411

512
> 此版本发布于 2023-06-18

README.en.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func main() {
5353
err = errors.BadRequest(nil) // Classic enough! Ah :)
5454
err = errors.Forbidden(nil) // Classic enough! Ah :)
5555
err = errors.NotFound(nil) // Classic enough! Ah :)
56+
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
5657
err = errors.InternalServerError(nil) // Classic enough! Ah :)
57-
err = errors.Timeout(nil)
58-
err = errors.NetworkError(nil)
5958
err = errors.DBError(nil)
59+
err = errors.PageTokenInvalid(nil)
6060

6161
// Use WithMsg to carry a message.
6262
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
@@ -67,6 +67,9 @@ func main() {
6767

6868
```
6969

70+
* [basic](_examples/basic.go)
71+
* [status](_examples/status.go)
72+
7073
### 👥 Contributing
7174

7275
If you find that something is not working as expected, just fork and fix by yourself or open an _**issue**_ :).

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func main() {
5353
err = errors.BadRequest(nil) // Classic enough! Ah :)
5454
err = errors.Forbidden(nil) // Classic enough! Ah :)
5555
err = errors.NotFound(nil) // Classic enough! Ah :)
56+
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
5657
err = errors.InternalServerError(nil) // Classic enough! Ah :)
57-
err = errors.Timeout(nil)
58-
err = errors.NetworkError(nil)
5958
err = errors.DBError(nil)
59+
err = errors.PageTokenInvalid(nil)
6060

6161
// Use WithMsg to carry a message.
6262
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
@@ -67,6 +67,9 @@ func main() {
6767

6868
```
6969

70+
* [basic](_examples/basic.go)
71+
* [status](_examples/status.go)
72+
7073
### 👥 贡献者
7174

7275
如果您觉得 **Errors** 缺少您需要的功能,那就 fork 到自己仓库随便玩。当然,也可以提 _**issue**_ :)。

_examples/basic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ func main() {
3939
err = errors.BadRequest(nil) // Classic enough! Ah :)
4040
err = errors.Forbidden(nil) // Classic enough! Ah :)
4141
err = errors.NotFound(nil) // Classic enough! Ah :)
42+
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
4243
err = errors.InternalServerError(nil) // Classic enough! Ah :)
43-
err = errors.Timeout(nil)
44-
err = errors.NetworkError(nil)
4544
err = errors.DBError(nil)
45+
err = errors.PageTokenInvalid(nil)
4646

4747
// Use WithMsg to carry a message.
4848
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))

_examples/status.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2023 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
"io"
10+
11+
"github.com/FishGoddess/errors"
12+
"github.com/FishGoddess/errors/status"
13+
)
14+
15+
var (
16+
errAuthFailed = errors.New("auth failed")
17+
)
18+
19+
func isAuthFailed(err error) bool {
20+
return err == errAuthFailed
21+
}
22+
23+
func main() {
24+
// We provide a way to transfer errors to status.
25+
// A status can be used by a server like grpc server with google.status.
26+
// For example, we have an auth failed error which will be returned by service.
27+
// However, we usually return a status code represents it in server,
28+
// and we usually return a human-being readable msg instead of the error msg.
29+
// So it has a gap between service error and server status.
30+
// You can register a status with server code and msg, then use Parse to restore them.
31+
authFailedStatus := status.New(1000, "you should check auth", isAuthFailed)
32+
status.RegisterStatus(authFailedStatus)
33+
34+
// Get code and msg from error.
35+
code, msg := status.Parse(errAuthFailed)
36+
fmt.Println(code, msg)
37+
38+
// Of course, you can use errors.Wrap to get an error with msg.
39+
// Then register a status about it.
40+
errCode := int32(123456)
41+
err := errors.Wrap(io.EOF, errCode, errors.WithMsg("hello"))
42+
43+
isError := func(err error) bool {
44+
return errors.Is(err, errCode)
45+
}
46+
47+
// As you can see, the error code and status code don't need to be the same.
48+
statusCode := int32(654321)
49+
status.RegisterStatus(status.New(statusCode, "i am status", isError))
50+
51+
// The msg will be the one set to error using WithMsg.
52+
// The registered msg would be used only if the error doesn't have a set msg.
53+
// Try to remove errors.WithMsg("hello") above and run again.
54+
code, msg = status.Parse(err)
55+
fmt.Println(code, msg)
56+
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func New(text string) error {
1414
return errors.New(text)
1515
}
1616

17-
// NewF returns a string error.
18-
func NewF(text string, params ...interface{}) error {
17+
// Newf returns a string error formatted with params.
18+
func Newf(text string, params ...interface{}) error {
1919
return fmt.Errorf(text, params...)
2020
}

errors_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ func TestNew(t *testing.T) {
1616
}
1717
}
1818

19-
// go test -v -cover -run=^TestNewF$
20-
func TestNewF(t *testing.T) {
21-
err := NewF("test %d %.2f", 123, 3.14)
19+
// go test -v -cover -run=^TestNewf$
20+
func TestNewf(t *testing.T) {
21+
err := Newf("test %d %.2f", 123, 3.14)
2222
if err.Error() != "test 123 3.14" {
2323
t.Errorf("err.Error() %s != 'test 123 3.14'", err.Error())
2424
}

extension.go

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,10 @@
55
package errors
66

77
const (
8-
codeTimeout = 1000
9-
codeNetworkError = 1100
10-
codeDBError = 1200
8+
codeDBError = 1100
9+
codePageTokenInvalid = 1200
1110
)
1211

13-
// Timeout returns a timeout error.
14-
func Timeout(err error, opts ...Option) error {
15-
return Wrap(err, codeTimeout, opts...)
16-
}
17-
18-
// IsTimeout if err is timeout.
19-
func IsTimeout(err error) bool {
20-
return Is(err, codeTimeout)
21-
}
22-
23-
// UnwrapTimeout if err is timeout.
24-
func UnwrapTimeout(err error) (error, bool) {
25-
return Unwrap(err, codeTimeout)
26-
}
27-
28-
// NetworkError returns a network error.
29-
func NetworkError(err error, opts ...Option) error {
30-
return Wrap(err, codeNetworkError, opts...)
31-
}
32-
33-
// IsNetworkError if err is network error.
34-
func IsNetworkError(err error) bool {
35-
return Is(err, codeNetworkError)
36-
}
37-
38-
// UnwrapNetworkError if err is network error.
39-
func UnwrapNetworkError(err error) (error, bool) {
40-
return Unwrap(err, codeNetworkError)
41-
}
42-
4312
// DBError returns a db error.
4413
func DBError(err error, opts ...Option) error {
4514
return Wrap(err, codeDBError, opts...)
@@ -54,3 +23,18 @@ func IsDBError(err error) bool {
5423
func UnwrapDBError(err error) (error, bool) {
5524
return Unwrap(err, codeDBError)
5625
}
26+
27+
// PageTokenInvalid returns a page token invalid error.
28+
func PageTokenInvalid(err error, opts ...Option) error {
29+
return Wrap(err, codePageTokenInvalid, opts...)
30+
}
31+
32+
// IsPageTokenInvalid if err is page token invalid.
33+
func IsPageTokenInvalid(err error) bool {
34+
return Is(err, codePageTokenInvalid)
35+
}
36+
37+
// UnwrapPageTokenInvalid if err is page token invalid.
38+
func UnwrapPageTokenInvalid(err error) (error, bool) {
39+
return Unwrap(err, codePageTokenInvalid)
40+
}

extension_test.go

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,6 @@ import (
99
"testing"
1010
)
1111

12-
// go test -v -cover -run=^TestTimeout$
13-
func TestTimeout(t *testing.T) {
14-
err := Timeout(nil)
15-
if err != nil {
16-
t.Error("Timeout is wrong", err)
17-
}
18-
19-
err = Timeout(errors.New("timeout"))
20-
if !IsTimeout(err) {
21-
t.Error("IsTimeout is wrong", err)
22-
}
23-
24-
if e, ok := UnwrapTimeout(err); !ok || e.Error() != "timeout" {
25-
t.Error("Timeout or UnwrapTimeout is wrong", err)
26-
}
27-
}
28-
29-
// go test -v -cover -run=^TestNetworkError$
30-
func TestNetworkError(t *testing.T) {
31-
err := NetworkError(nil)
32-
if err != nil {
33-
t.Error("NetworkError is wrong", err)
34-
}
35-
36-
err = NetworkError(errors.New("network error"))
37-
if !IsNetworkError(err) {
38-
t.Error("IsNetworkError is wrong", err)
39-
}
40-
41-
if e, ok := UnwrapNetworkError(err); !ok || e.Error() != "network error" {
42-
t.Error("NetworkError or UnwrapNetworkError is wrong", err)
43-
}
44-
}
45-
4612
// go test -v -cover -run=^TestDBError$
4713
func TestDBError(t *testing.T) {
4814
err := DBError(nil)
@@ -59,3 +25,20 @@ func TestDBError(t *testing.T) {
5925
t.Error("DBError or UnwrapDBError is wrong", err)
6026
}
6127
}
28+
29+
// go test -v -cover -run=^TestPageTokenInvalid$
30+
func TestPageTokenInvalid(t *testing.T) {
31+
err := PageTokenInvalid(nil)
32+
if err != nil {
33+
t.Error("PageTokenInvalid is wrong", err)
34+
}
35+
36+
err = PageTokenInvalid(errors.New("page token invalid"))
37+
if !IsPageTokenInvalid(err) {
38+
t.Error("IsPageTokenInvalid is wrong", err)
39+
}
40+
41+
if e, ok := UnwrapPageTokenInvalid(err); !ok || e.Error() != "page token invalid" {
42+
t.Error("PageTokenInvalid or UnwrapPageTokenInvalid is wrong", err)
43+
}
44+
}

0 commit comments

Comments
 (0)