From e7073fe81a3aa8ad4dcf105cafcfc146400e982b Mon Sep 17 00:00:00 2001 From: FishGoddess <1149062639@qq.com> Date: Sat, 10 Aug 2024 14:38:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B8=B8=E7=94=A8=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=BF=AB=E6=8D=B7=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HISTORY.md | 6 ++++ README.en.md | 8 +++++ README.md | 8 +++++ _examples/basic.go | 8 +++++ _icons/coverage.svg | 4 +-- types.go | 28 +++++++++++++++ types_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 types.go create mode 100644 types_test.go diff --git a/HISTORY.md b/HISTORY.md index 7dc09f2..22ffbde 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,11 @@ ## ✒ 历史版本的特性介绍 (Features in old versions) +### v0.6.1-alpha + +> 此版本发布于 2024-08-10 + +* 增加常用错误快捷方式 + ### v0.6.0-alpha > 此版本发布于 2024-08-09 diff --git a/README.en.md b/README.en.md index 00cadbc..eb16587 100644 --- a/README.en.md +++ b/README.en.md @@ -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) } ``` diff --git a/README.md b/README.md index ec2122e..8b94969 100644 --- a/README.md +++ b/README.md @@ -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) } ``` diff --git a/_examples/basic.go b/_examples/basic.go index c4f0f78..db92cf0 100644 --- a/_examples/basic.go +++ b/_examples/basic.go @@ -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) } diff --git a/_icons/coverage.svg b/_icons/coverage.svg index 64d796b..73b8d11 100644 --- a/_icons/coverage.svg +++ b/_icons/coverage.svg @@ -10,7 +10,7 @@ coverage coverage - 79% - 79% + 80% + 80% \ No newline at end of file diff --git a/types.go b/types.go new file mode 100644 index 0000000..4c213d8 --- /dev/null +++ b/types.go @@ -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) +} diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..b7f6b3f --- /dev/null +++ b/types_test.go @@ -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) + } + } +}