Skip to content

Commit 51eba14

Browse files
authored
Merge pull request #2 from FishGoddess/develop
v0.6.1-alpha
2 parents 356a03f + e7073fe commit 51eba14

7 files changed

Lines changed: 147 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.6.1-alpha
4+
5+
> 此版本发布于 2024-08-10
6+
7+
* 增加常用错误快捷方式
8+
39
### v0.6.0-alpha
410

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

README.en.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func main() {
4141
// Also, we provide some useful information carrier for you.
4242
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
4343
fmt.Println(err)
44+
45+
// What's more, we provide some shortcuts for you.
46+
// All these ways are returning a *Error and you are free to use all methods on *Error.
47+
berr := errors.BadRequest("id is wrong")
48+
ferr := errors.Forbidden("user isn't allowed")
49+
nerr := errors.NotFound("book not found")
50+
rerr := errors.RequireLogin("user requires login")
51+
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)
4452
}
4553

4654
```

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func main() {
4141
// Also, we provide some useful information carrier for you.
4242
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
4343
fmt.Println(err)
44+
45+
// What's more, we provide some shortcuts for you.
46+
// All these ways are returning a *Error and you are free to use all methods on *Error.
47+
berr := errors.BadRequest("id is wrong")
48+
ferr := errors.Forbidden("user isn't allowed")
49+
nerr := errors.NotFound("book not found")
50+
rerr := errors.RequireLogin("user requires login")
51+
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)
4452
}
4553

4654
```

_examples/basic.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@ func main() {
2727
// Also, we provide some useful information carrier for you.
2828
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
2929
fmt.Println(err)
30+
31+
// What's more, we provide some shortcuts for you.
32+
// All these ways are returning a *Error and you are free to use all methods on *Error.
33+
berr := errors.BadRequest("id is wrong")
34+
ferr := errors.Forbidden("user isn't allowed")
35+
nerr := errors.NotFound("book not found")
36+
rerr := errors.RequireLogin("user requires login")
37+
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)
3038
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

types.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2024 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 errors
6+
7+
const (
8+
CodeBadRequest = 400
9+
CodeForbidden = 403
10+
CodeNotFound = 404
11+
CodeRequireLogin = 1000
12+
)
13+
14+
func BadRequest(message string) *Error {
15+
return Wrap(CodeBadRequest, message)
16+
}
17+
18+
func Forbidden(message string) *Error {
19+
return Wrap(CodeForbidden, message)
20+
}
21+
22+
func NotFound(message string) *Error {
23+
return Wrap(CodeNotFound, message)
24+
}
25+
26+
func RequireLogin(message string) *Error {
27+
return Wrap(CodeRequireLogin, message)
28+
}

types_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2024 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 errors
6+
7+
import "testing"
8+
9+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestBadRequest$
10+
func TestBadRequest(t *testing.T) {
11+
testCases := []struct {
12+
message string
13+
code int32
14+
}{
15+
{
16+
message: "id is nil",
17+
code: CodeBadRequest,
18+
},
19+
}
20+
21+
for _, testCase := range testCases {
22+
err := BadRequest(testCase.message)
23+
if err.Code() != testCase.code {
24+
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
25+
}
26+
}
27+
}
28+
29+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestForbidden$
30+
func TestForbidden(t *testing.T) {
31+
testCases := []struct {
32+
message string
33+
code int32
34+
}{
35+
{
36+
message: "id is nil",
37+
code: CodeForbidden,
38+
},
39+
}
40+
41+
for _, testCase := range testCases {
42+
err := Forbidden(testCase.message)
43+
if err.Code() != testCase.code {
44+
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
45+
}
46+
}
47+
}
48+
49+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestNotFound$
50+
func TestNotFound(t *testing.T) {
51+
testCases := []struct {
52+
message string
53+
code int32
54+
}{
55+
{
56+
message: "id is nil",
57+
code: CodeNotFound,
58+
},
59+
}
60+
61+
for _, testCase := range testCases {
62+
err := NotFound(testCase.message)
63+
if err.Code() != testCase.code {
64+
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
65+
}
66+
}
67+
}
68+
69+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestRequireLogin$
70+
func TestRequireLogin(t *testing.T) {
71+
testCases := []struct {
72+
message string
73+
code int32
74+
}{
75+
{
76+
message: "id is nil",
77+
code: CodeRequireLogin,
78+
},
79+
}
80+
81+
for _, testCase := range testCases {
82+
err := RequireLogin(testCase.message)
83+
if err.Code() != testCase.code {
84+
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)