From a91a5d01d1672401376ce9c60ac8f51ecbb9a35a Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Fri, 22 Jan 2021 16:15:01 +0800 Subject: [PATCH] Move codes to error package --- ERROR_SPECIFICATION.md | 1 + client/httplib/error.go | 17 +++++++++++++++++ core/{codes => error}/codes.go | 4 ++-- core/error/error.go | 13 ++++++------- core/error/error_test.go | 27 +++++++++++++-------------- 5 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 ERROR_SPECIFICATION.md create mode 100644 client/httplib/error.go rename core/{codes => error}/codes.go (78%) diff --git a/ERROR_SPECIFICATION.md b/ERROR_SPECIFICATION.md new file mode 100644 index 00000000..cf2f94b2 --- /dev/null +++ b/ERROR_SPECIFICATION.md @@ -0,0 +1 @@ +# Error Module diff --git a/client/httplib/error.go b/client/httplib/error.go new file mode 100644 index 00000000..974fa5bb --- /dev/null +++ b/client/httplib/error.go @@ -0,0 +1,17 @@ +// Copyright 2020 beego +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package httplib + + diff --git a/core/codes/codes.go b/core/error/codes.go similarity index 78% rename from core/codes/codes.go rename to core/error/codes.go index 77f59220..929714e8 100644 --- a/core/codes/codes.go +++ b/core/error/codes.go @@ -1,4 +1,4 @@ -package codes +package error // A Code is an unsigned 32-bit error code as defined in the beego spec. type Code uint32 @@ -10,5 +10,5 @@ const ( // CodeToStr is a map about Code and Code's message var CodeToStr = map[Code]string{ - SessionSessionStartError : `"SESSION_MODULE_SESSION_START_ERROR"`, + SessionSessionStartError: `"SESSION_MODULE_SESSION_START_ERROR"`, } \ No newline at end of file diff --git a/core/error/error.go b/core/error/error.go index 6caf9839..5844325b 100644 --- a/core/error/error.go +++ b/core/error/error.go @@ -2,7 +2,6 @@ package error import ( "fmt" - "github.com/beego/beego/v2/core/codes" "strconv" ) @@ -11,22 +10,22 @@ import ( // error message. // More docs http://beego.me/docs/module/error.md. type Error struct { - Code codes.Code - Msg string + Code Code + Msg string } // New returns a Error representing c and msg. -func New(c codes.Code, msg string) *Error { +func New(c Code, msg string) *Error { return &Error{Code: c, Msg: msg} } // Err returns an error representing c and msg. If c is OK, returns nil. -func Err(c codes.Code, msg string) error { +func Err(c Code, msg string) error { return New(c, msg) } // Errorf returns Error(c, fmt.Sprintf(format, a...)). -func Errorf(c codes.Code, format string, a ...interface{}) error { +func Errorf(c Code, format string, a ...interface{}) error { return Err(c, fmt.Sprintf(format, a...)) } @@ -37,7 +36,7 @@ func (e *Error) Error() string { } // GetCode returns Error's Code. -func (e *Error) GetCode() codes.Code { +func (e *Error) GetCode() Code { if e != nil { return e.Code } diff --git a/core/error/error_test.go b/core/error/error_test.go index 630b277c..85974623 100644 --- a/core/error/error_test.go +++ b/core/error/error_test.go @@ -1,14 +1,13 @@ package error import ( - "github.com/beego/beego/v2/core/codes" "reflect" "testing" ) func TestErr(t *testing.T) { type args struct { - c codes.Code + c Code msg string } tests := []struct { @@ -17,7 +16,7 @@ func TestErr(t *testing.T) { wantErr bool }{ // TODO: Add test cases. - {name: "1", args: args{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, wantErr: true}, + {name: "1", args: args{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, wantErr: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -30,7 +29,7 @@ func TestErr(t *testing.T) { func TestError_Error(t *testing.T) { type fields struct { - Code codes.Code + Code Code Msg string } tests := []struct { @@ -39,7 +38,7 @@ func TestError_Error(t *testing.T) { want string }{ // TODO: Add test cases. - {name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: "beego error: code = 5001001 desc = \"SESSION_MODULE_SESSION_START_ERROR\""}, + {name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: "beego error: code = 5001001 desc = \"SESSION_MODULE_SESSION_START_ERROR\""}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -56,16 +55,16 @@ func TestError_Error(t *testing.T) { func TestError_GetCode(t *testing.T) { type fields struct { - Code codes.Code + Code Code Msg string } tests := []struct { name string fields fields - want codes.Code + want Code }{ // TODO: Add test cases. - {name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: codes.SessionSessionStartError}, + {name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: SessionSessionStartError}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -82,7 +81,7 @@ func TestError_GetCode(t *testing.T) { func TestError_GetMessage(t *testing.T) { type fields struct { - Code codes.Code + Code Code Msg string } tests := []struct { @@ -91,7 +90,7 @@ func TestError_GetMessage(t *testing.T) { want string }{ // TODO: Add test cases. - {name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: codes.CodeToStr[codes.SessionSessionStartError]}, + {name: "1", fields: fields{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: CodeToStr[SessionSessionStartError]}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -108,7 +107,7 @@ func TestError_GetMessage(t *testing.T) { func TestErrorf(t *testing.T) { type args struct { - c codes.Code + c Code format string a []interface{} } @@ -118,7 +117,7 @@ func TestErrorf(t *testing.T) { wantErr bool }{ // TODO: Add test cases. - {name: "1", args: args{codes.SessionSessionStartError, "%s", []interface{}{codes.CodeToStr[codes.SessionSessionStartError]}}, wantErr: true}, + {name: "1", args: args{SessionSessionStartError, "%s", []interface{}{CodeToStr[SessionSessionStartError]}}, wantErr: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -131,7 +130,7 @@ func TestErrorf(t *testing.T) { func TestNew(t *testing.T) { type args struct { - c codes.Code + c Code msg string } tests := []struct { @@ -140,7 +139,7 @@ func TestNew(t *testing.T) { want *Error }{ // TODO: Add test cases. - {name: "1", args: args{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: &Error{Code:codes.SessionSessionStartError, Msg:codes.CodeToStr[codes.SessionSessionStartError]}}, + {name: "1", args: args{SessionSessionStartError, CodeToStr[SessionSessionStartError]}, want: &Error{Code: SessionSessionStartError, Msg: CodeToStr[SessionSessionStartError]}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {