Skip to content

Commit 63feffc

Browse files
committed
调整
1 parent 8203321 commit 63feffc

29 files changed

Lines changed: 194 additions & 1163 deletions

.gitattributes

Lines changed: 0 additions & 22 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ jobs:
99
test-project:
1010
runs-on: ubuntu-20.04
1111
steps:
12+
- name: Setup
13+
uses: actions/setup-go@v4
14+
with:
15+
go-version: "1.21"
16+
- run: go version
17+
1218
- name: Checkout
13-
uses: actions/checkout@v3
19+
uses: actions/checkout@v4
1420

1521
- name: Test
16-
run: make test
22+
run: make test

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.8.0
4+
5+
> 此版本发布于 2024-08-09
6+
7+
* 重构,调整使用姿势
8+
39
### v0.5.2
410

511
> 此版本发布于 2023-07-27

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2022 FishGoddess
3+
Copyright (c) 2024 FishGoddess
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the "Software"),

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
.PHONY: test fmt
1+
.PHONY: fmt test
22

3-
all: test fmt
4-
5-
test:
6-
go test -cover ./...
3+
all: fmt test
74

85
fmt:
9-
go fmt ./...
6+
go fmt ./...
7+
8+
test:
9+
go test -v -cover -count=1 -test.cpu=1 ./...

README.en.md

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Go Doc](_icons/godoc.svg)](https://pkg.go.dev/github.com/FishGoddess/errors)
44
[![License](_icons/license.svg)](https://opensource.org/licenses/MIT)
5-
[![License](_icons/coverage.svg)](_icons/coverage.svg)
5+
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/errors/actions/workflows/test.yml/badge.svg)
77

88
**Errors** is a lib for handling error gracefully in Go.
@@ -25,50 +25,26 @@ import (
2525
"github.com/FishGoddess/errors"
2626
)
2727

28-
const (
29-
codeTestError = 10000 // Your error's code
30-
)
28+
func main() {
29+
// Use wrap function to create an *Error error which has code and message.
30+
err := errors.Wrap(1000, "need login")
31+
fmt.Println(err)
3132

32-
// TestError returns a test error.
33-
func TestError(err error) error {
34-
return errors.Wrap(err, codeTestError)
35-
}
33+
// You can get code and message of err anytime.
34+
fmt.Println(err.Code(), err.Message())
3635

37-
// IsTestError if err is test error.
38-
func IsTestError(err error) bool {
39-
return errors.Is(err, codeTestError)
40-
}
36+
// Try these ways to get code and message!
37+
// You will get default code or message if err doesn't have a code or message.
38+
fmt.Println(errors.Code(err, 6699), errors.Message(err, "default message"))
39+
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))
4140

42-
func main() {
43-
// We provide three graceful ways to handle error in Go: Wrap() and Unwrap() and Is().
44-
// Wrap wraps error with a code and Unwrap returns one error with this code.
45-
// Is returns one error is with the same code or not.
46-
// As you can see, we define two functions above, and this is the basic way to use this lib.
47-
err := TestError(errors.New("something wrong"))
48-
if IsTestError(err) {
49-
fmt.Println("I got a test error")
50-
}
51-
52-
// Also, we provide some basic errors for you:
53-
err = errors.BadRequest(nil) // Classic enough! Ah :)
54-
err = errors.Forbidden(nil) // Classic enough! Ah :)
55-
err = errors.NotFound(nil) // Classic enough! Ah :)
56-
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
57-
err = errors.InternalServerError(nil) // Classic enough! Ah :)
58-
err = errors.DBError(nil)
59-
err = errors.PageTokenInvalid(nil)
60-
61-
// Use WithMsg to carry a message.
62-
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
63-
fmt.Println(err.Error())
64-
fmt.Println(errors.Msg(err))
65-
fmt.Println(errors.MsgOrDefault(io.EOF, "default error message"))
41+
// Also, we provide some useful information carrier for you.
42+
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller().WithArgs("key", "value")
43+
fmt.Println(err)
6644
}
67-
6845
```
6946

7047
* [basic](_examples/basic.go)
71-
* [status](_examples/status.go)
7248

7349
### 👥 Contributing
7450

README.md

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Go Doc](_icons/godoc.svg)](https://pkg.go.dev/github.com/FishGoddess/errors)
44
[![License](_icons/license.svg)](https://opensource.org/licenses/MIT)
5-
[![License](_icons/coverage.svg)](_icons/coverage.svg)
5+
[![Coverage](_icons/coverage.svg)](_icons/coverage.svg)
66
![Test](https://github.com/FishGoddess/errors/actions/workflows/test.yml/badge.svg)
77

88
**Errors** 是一个用于优雅地处理 Go 中错误的库。
@@ -25,50 +25,26 @@ import (
2525
"github.com/FishGoddess/errors"
2626
)
2727

28-
const (
29-
codeTestError = 10000 // Your error's code
30-
)
28+
func main() {
29+
// Use wrap function to create an *Error error which has code and message.
30+
err := errors.Wrap(1000, "need login")
31+
fmt.Println(err)
3132

32-
// TestError returns a test error.
33-
func TestError(err error) error {
34-
return errors.Wrap(err, codeTestError)
35-
}
33+
// You can get code and message of err anytime.
34+
fmt.Println(err.Code(), err.Message())
3635

37-
// IsTestError if err is test error.
38-
func IsTestError(err error) bool {
39-
return errors.Is(err, codeTestError)
40-
}
36+
// Try these ways to get code and message!
37+
// You will get default code or message if err doesn't have a code or message.
38+
fmt.Println(errors.Code(err, 6699), errors.Message(err, "default message"))
39+
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))
4140

42-
func main() {
43-
// We provide three graceful ways to handle error in Go: Wrap() and Unwrap() and Is().
44-
// Wrap wraps error with a code and Unwrap returns one error with this code.
45-
// Is returns one error is with the same code or not.
46-
// As you can see, we define two functions above, and this is the basic way to use this lib.
47-
err := TestError(errors.New("something wrong"))
48-
if IsTestError(err) {
49-
fmt.Println("I got a test error")
50-
}
51-
52-
// Also, we provide some basic errors for you:
53-
err = errors.BadRequest(nil) // Classic enough! Ah :)
54-
err = errors.Forbidden(nil) // Classic enough! Ah :)
55-
err = errors.NotFound(nil) // Classic enough! Ah :)
56-
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
57-
err = errors.InternalServerError(nil) // Classic enough! Ah :)
58-
err = errors.DBError(nil)
59-
err = errors.PageTokenInvalid(nil)
60-
61-
// Use WithMsg to carry a message.
62-
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
63-
fmt.Println(err.Error())
64-
fmt.Println(errors.Msg(err))
65-
fmt.Println(errors.MsgOrDefault(io.EOF, "default error message"))
41+
// Also, we provide some useful information carrier for you.
42+
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller().WithArgs("key", "value")
43+
fmt.Println(err)
6644
}
67-
6845
```
6946

7047
* [basic](_examples/basic.go)
71-
* [status](_examples/status.go)
7248

7349
### 👥 贡献者
7450

_examples/basic.go

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 FishGoddess. All rights reserved.
1+
// Copyright 2024 FishGoddess. All rights reserved.
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

@@ -11,42 +11,20 @@ import (
1111
"github.com/FishGoddess/errors"
1212
)
1313

14-
const (
15-
codeTestError = 10000 // Your error's code
16-
)
17-
18-
// TestError returns a test error.
19-
func TestError(err error) error {
20-
return errors.Wrap(err, codeTestError)
21-
}
22-
23-
// IsTestError if err is test error.
24-
func IsTestError(err error) bool {
25-
return errors.Is(err, codeTestError)
26-
}
27-
2814
func main() {
29-
// We provide three graceful ways to handle error in Go: Wrap() and Unwrap() and Is().
30-
// Wrap wraps error with a code and Unwrap returns one error with this code.
31-
// Is returns one error is with the same code or not.
32-
// As you can see, we define two functions above, and this is the basic way to use this lib.
33-
err := TestError(errors.New("something wrong"))
34-
if IsTestError(err) {
35-
fmt.Println("I got a test error")
36-
}
15+
// Use wrap function to create an *Error error which has code and message.
16+
err := errors.Wrap(1000, "need login")
17+
fmt.Println(err)
18+
19+
// You can get code and message of err anytime.
20+
fmt.Println(err.Code(), err.Message())
3721

38-
// Also, we provide some basic errors for you:
39-
err = errors.BadRequest(nil) // Classic enough! Ah :)
40-
err = errors.Forbidden(nil) // Classic enough! Ah :)
41-
err = errors.NotFound(nil) // Classic enough! Ah :)
42-
err = errors.RequestTimeout(nil) // Classic enough! Ah :)
43-
err = errors.InternalServerError(nil) // Classic enough! Ah :)
44-
err = errors.DBError(nil)
45-
err = errors.PageTokenInvalid(nil)
22+
// Try these ways to get code and message!
23+
// You will get default code or message if err doesn't have a code or message.
24+
fmt.Println(errors.Code(err, 6699), errors.Message(err, "default message"))
25+
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))
4626

47-
// Use WithMsg to carry a message.
48-
err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
49-
fmt.Println(err.Error())
50-
fmt.Println(errors.Msg(err))
51-
fmt.Println(errors.MsgOrDefault(io.EOF, "default error message"))
27+
// Also, we provide some useful information carrier for you.
28+
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller().WithArgs("key", "value")
29+
fmt.Println(err)
5230
}

_examples/status.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

errors.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
// Copyright 2023 FishGoddess. All rights reserved.
1+
// Copyright 2024 FishGoddess. All rights reserved.
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

55
package errors
66

77
import (
88
"errors"
9-
"fmt"
109
)
1110

12-
// New returns a string error.
1311
func New(text string) error {
1412
return errors.New(text)
1513
}
1614

17-
// Newf returns a string error formatted with params.
18-
func Newf(text string, params ...interface{}) error {
19-
return fmt.Errorf(text, params...)
15+
// Is is a shortcut of errors.Is.
16+
func Is(err, target error) bool {
17+
return errors.Is(err, target)
18+
}
19+
20+
// As is a shortcut of errors.As.
21+
func As(err error, target any) bool {
22+
return errors.As(err, target)
23+
}
24+
25+
// Unwrap is a shortcut of errors.Unwrap.
26+
func Unwrap(err error) error {
27+
return errors.Unwrap(err)
28+
}
29+
30+
// Join is a shortcut of errors.Join.
31+
func Join(errs ...error) error {
32+
return errors.Join(errs...)
2033
}

0 commit comments

Comments
 (0)