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
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.6.1-alpha

> 此版本发布于 2024-08-10

* 增加常用错误快捷方式

### v0.6.0-alpha

> 此版本发布于 2024-08-09
Expand Down
8 changes: 8 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func main() {
// Also, we provide some useful information carrier for you.
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
fmt.Println(err)

// What's more, we provide some shortcuts for you.
// All these ways are returning a *Error and you are free to use all methods on *Error.
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)
}

```
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func main() {
// Also, we provide some useful information carrier for you.
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
fmt.Println(err)

// What's more, we provide some shortcuts for you.
// All these ways are returning a *Error and you are free to use all methods on *Error.
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)
}

```
Expand Down
8 changes: 8 additions & 0 deletions _examples/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ func main() {
// Also, we provide some useful information carrier for you.
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
fmt.Println(err)

// What's more, we provide some shortcuts for you.
// All these ways are returning a *Error and you are free to use all methods on *Error.
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)
}
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.
28 changes: 28 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package errors

const (
CodeBadRequest = 400
CodeForbidden = 403
CodeNotFound = 404
CodeRequireLogin = 1000
)

func BadRequest(message string) *Error {
return Wrap(CodeBadRequest, message)
}

func Forbidden(message string) *Error {
return Wrap(CodeForbidden, message)
}

func NotFound(message string) *Error {
return Wrap(CodeNotFound, message)
}

func RequireLogin(message string) *Error {
return Wrap(CodeRequireLogin, message)
}
87 changes: 87 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package errors

import "testing"

// go test -v -cover -count=1 -test.cpu=1 -run=^TestBadRequest$
func TestBadRequest(t *testing.T) {
testCases := []struct {
message string
code int32
}{
{
message: "id is nil",
code: CodeBadRequest,
},
}

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

// go test -v -cover -count=1 -test.cpu=1 -run=^TestForbidden$
func TestForbidden(t *testing.T) {
testCases := []struct {
message string
code int32
}{
{
message: "id is nil",
code: CodeForbidden,
},
}

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

// go test -v -cover -count=1 -test.cpu=1 -run=^TestNotFound$
func TestNotFound(t *testing.T) {
testCases := []struct {
message string
code int32
}{
{
message: "id is nil",
code: CodeNotFound,
},
}

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

// go test -v -cover -count=1 -test.cpu=1 -run=^TestRequireLogin$
func TestRequireLogin(t *testing.T) {
testCases := []struct {
message string
code int32
}{
{
message: "id is nil",
code: CodeRequireLogin,
},
}

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