Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ on:

jobs:
test-project:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- run: uname -a
- run: lsb_release -a

- name: Setup
uses: actions/setup-go@v4
with:
go-version: "1.21"
go-version: "1.25"

- run: go version

- name: Checkout
Expand Down
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v0.8.0

> 此版本发布于 2026-01-26

* 废弃 Match 类函数,提供 Is 类函数

### v0.7.2

> 此版本发布于 2024-08-21
Expand Down
15 changes: 6 additions & 9 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@ func main() {
berr := errors.BadRequest("id is wrong")
ferr := errors.Forbidden("user isn't allowed")
nerr := errors.NotFound("book not found")
rerr := errors.RequireLogin("user requires login")
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)

isBadRequest := errors.MatchBadRequest(berr)
isForbidden := errors.MatchForbidden(ferr)
isNotFound := errors.MatchNotFound(nerr)
isRequireLogin := errors.MatchRequireLogin(rerr)
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\nisRequireLogin: %+v\n", isBadRequest, isForbidden, isNotFound, isRequireLogin)
}
fmt.Printf("%+v\n%+v\n%+v\n", berr, ferr, nerr)

isBadRequest := errors.IsBadRequest(berr)
isForbidden := errors.IsForbidden(ferr)
isNotFound := errors.IsNotFound(nerr)
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\n", isBadRequest, isForbidden, isNotFound)
}
```

* [basic](_examples/basic.go)
Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@ func main() {
berr := errors.BadRequest("id is wrong")
ferr := errors.Forbidden("user isn't allowed")
nerr := errors.NotFound("book not found")
rerr := errors.RequireLogin("user requires login")
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)

isBadRequest := errors.MatchBadRequest(berr)
isForbidden := errors.MatchForbidden(ferr)
isNotFound := errors.MatchNotFound(nerr)
isRequireLogin := errors.MatchRequireLogin(rerr)
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\nisRequireLogin: %+v\n", isBadRequest, isForbidden, isNotFound, isRequireLogin)
}
fmt.Printf("%+v\n%+v\n%+v\n", berr, ferr, nerr)

isBadRequest := errors.IsBadRequest(berr)
isForbidden := errors.IsForbidden(ferr)
isNotFound := errors.IsNotFound(nerr)
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\n", isBadRequest, isForbidden, isNotFound)
}
```

* [basic](_examples/basic.go)
Expand Down
14 changes: 6 additions & 8 deletions _examples/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ func main() {
berr := errors.BadRequest("id is wrong")
ferr := errors.Forbidden("user isn't allowed")
nerr := errors.NotFound("book not found")
rerr := errors.RequireLogin("user requires login")
fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)

isBadRequest := errors.MatchBadRequest(berr)
isForbidden := errors.MatchForbidden(ferr)
isNotFound := errors.MatchNotFound(nerr)
isRequireLogin := errors.MatchRequireLogin(rerr)
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\nisRequireLogin: %+v\n", isBadRequest, isForbidden, isNotFound, isRequireLogin)
fmt.Printf("%+v\n%+v\n%+v\n", berr, ferr, nerr)

isBadRequest := errors.IsBadRequest(berr)
isForbidden := errors.IsForbidden(ferr)
isNotFound := errors.IsNotFound(nerr)
fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\n", isBadRequest, isForbidden, isNotFound)
}
4 changes: 2 additions & 2 deletions _icons/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 22 additions & 15 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package errors

const (
codeBadRequest = 400
codeForbidden = 403
codeNotFound = 404
codeRequireLogin = 1000
codeBadRequest = 400
codeForbidden = 403
codeNotFound = 404
)

// BadRequest returns *Error with bad request code.
Expand All @@ -26,27 +25,35 @@ func NotFound(message string, args ...any) *Error {
return Wrap(codeNotFound, message, args...)
}

// RequireLogin returns *Error with require login code.
func RequireLogin(message string, args ...any) *Error {
return Wrap(codeRequireLogin, message, args...)
// IsBadRequest checks err with bad request code.
func IsBadRequest(err error) bool {
return IsCode(err, codeBadRequest)
}

// IsForbidden checks err with forbidden code.
func IsForbidden(err error) bool {
return IsCode(err, codeForbidden)
}

// IsNotFound checks err with not found code.
func IsNotFound(err error) bool {
return IsCode(err, codeNotFound)
}

// MatchBadRequest matches err with bad request code.
// Deprecated: Use IsBadRequest instead because this name is 'ugly' to me.
func MatchBadRequest(err error) bool {
return Match(err, codeBadRequest)
return IsBadRequest(err)
}

// MatchForbidden matches err with forbidden code.
// Deprecated: Use IsForbidden instead because this name is 'ugly' to me.
func MatchForbidden(err error) bool {
return Match(err, codeForbidden)
return IsForbidden(err)
}

// MatchNotFound matches err with not found code.
// Deprecated: Use IsNotFound instead because this name is 'ugly' to me.
func MatchNotFound(err error) bool {
return Match(err, codeNotFound)
}

// MatchRequireLogin matches err with require login code.
func MatchRequireLogin(err error) bool {
return Match(err, codeRequireLogin)
return IsNotFound(err)
}
82 changes: 12 additions & 70 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,48 +126,8 @@ func TestNotFound(t *testing.T) {
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestRequireLogin$
func TestRequireLogin(t *testing.T) {
testCases := []struct {
message string
args []any
wantCode int32
wantMessage string
}{
{
message: "xxx",
args: nil,
wantCode: codeRequireLogin,
wantMessage: "xxx",
},
{
message: "xxx %d%.2f",
args: nil,
wantCode: codeRequireLogin,
wantMessage: "xxx %d%.2f",
},
{
message: "xxx %d%s%+v",
args: []any{1, ".", true},
wantCode: codeRequireLogin,
wantMessage: "xxx 1.true",
},
}

for _, testCase := range testCases {
err := RequireLogin(testCase.message, testCase.args...)
if err.Code() != testCase.wantCode {
t.Errorf("err.Code() %d != testCase.wantCode %d", err.Code(), testCase.wantCode)
}

if err.Message() != testCase.wantMessage {
t.Errorf("err.Message() %s != testCase.wantMessage %s", err.Message(), testCase.wantMessage)
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchBadRequest$
func TestMatchBadRequest(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsBadRequest$
func TestIsBadRequest(t *testing.T) {
testCases := []struct {
message string
}{
Expand All @@ -178,14 +138,14 @@ func TestMatchBadRequest(t *testing.T) {

for _, testCase := range testCases {
err := BadRequest(testCase.message)
if !MatchBadRequest(err) {
t.Errorf("err %+v not match code %d", err, err.Code())
if !IsBadRequest(err) {
t.Errorf("err %+v not code %d", err, err.Code())
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchForbidden$
func TestMatchForbidden(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsForbidden$
func TestIsForbidden(t *testing.T) {
testCases := []struct {
message string
}{
Expand All @@ -196,14 +156,14 @@ func TestMatchForbidden(t *testing.T) {

for _, testCase := range testCases {
err := Forbidden(testCase.message)
if !MatchForbidden(err) {
t.Errorf("err %+v not match code %d", err, err.Code())
if !IsForbidden(err) {
t.Errorf("err %+v not code %d", err, err.Code())
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchNotFound$
func TestMatchNotFound(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsNotFound$
func TestIsNotFound(t *testing.T) {
testCases := []struct {
message string
}{
Expand All @@ -214,26 +174,8 @@ func TestMatchNotFound(t *testing.T) {

for _, testCase := range testCases {
err := NotFound(testCase.message)
if !MatchNotFound(err) {
t.Errorf("err %+v not match code %d", err, err.Code())
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatchRequireLogin$
func TestMatchRequireLogin(t *testing.T) {
testCases := []struct {
message string
}{
{
message: "xxx",
},
}

for _, testCase := range testCases {
err := RequireLogin(testCase.message)
if !MatchRequireLogin(err) {
t.Errorf("err %+v not match code %d", err, err.Code())
if !IsNotFound(err) {
t.Errorf("err %+v not code %d", err, err.Code())
}
}
}
10 changes: 8 additions & 2 deletions unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func CodeMessage(err error, defaultCode int32, defaultMessage string, args ...an
return code, message
}

// Match unwraps error and check if its code equals to code.
func Match(err error, code int32) bool {
// IsCode unwraps error and check if its code equals to code.
func IsCode(err error, code int32) bool {
if err == nil {
return code == 0
}
Expand All @@ -87,3 +87,9 @@ func Match(err error, code int32) bool {

return false
}

// Match unwraps error and check if its code equals to code.
// Deprecated: Use IsCode instead because this name is 'ugly' to me.
func Match(err error, code int32) bool {
return IsCode(err, code)
}
52 changes: 26 additions & 26 deletions unwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,51 +193,51 @@ func TestCodeMessage(t *testing.T) {
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestMatch$
func TestMatch(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIsCode$
func TestIsCode(t *testing.T) {
testErr := &testError{}

testCases := []struct {
err error
code int32
match bool
err error
code int32
is bool
}{
{
err: nil,
code: 0,
match: true,
err: nil,
code: 0,
is: true,
},
{
err: nil,
code: 999,
match: false,
err: nil,
code: 999,
is: false,
},
{
err: io.EOF,
code: 999,
match: false,
err: io.EOF,
code: 999,
is: false,
},
{
err: testErr,
code: testErr.Code(),
match: true,
err: testErr,
code: testErr.Code(),
is: true,
},
{
err: Wrap(1000, "wow"),
code: 1000,
match: true,
err: Wrap(1000, "wow"),
code: 1000,
is: true,
},
{
err: Wrap(1000, "eof").With(io.EOF),
code: 1000,
match: true,
err: Wrap(1000, "eof").With(io.EOF),
code: 1000,
is: true,
},
}

for _, testCase := range testCases {
match := Match(testCase.err, testCase.code)
if match != testCase.match {
t.Errorf("match %+v != testCase.match %+v", match, testCase.match)
is := IsCode(testCase.err, testCase.code)
if is != testCase.is {
t.Errorf("got %+v != want %+v", is, testCase.is)
}
}
}
Loading