From d47a95df8d40218009962240cc8193e399935450 Mon Sep 17 00:00:00 2001 From: zchh < zc123zhangchi@gmail.com> Date: Sun, 17 Jan 2021 22:42:49 +0800 Subject: [PATCH] =?UTF-8?q?error=E6=A8=A1=E5=9D=97=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/codes/codes.go | 4 +- core/error/error.go | 18 +++-- core/error/error_test.go | 151 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 core/error/error_test.go diff --git a/core/codes/codes.go b/core/codes/codes.go index 9be5bb9c..7938fea7 100644 --- a/core/codes/codes.go +++ b/core/codes/codes.go @@ -6,6 +6,6 @@ const ( SessionSessionStartError Code = 5001001 ) -var strToCode = map[string]Code{ - `"SESSION_MODULE_SESSION_START_ERROR"`: SessionSessionStartError, +var CodeToStr = map[Code]string{ + SessionSessionStartError : `"SESSION_MODULE_SESSION_START_ERROR"`, } diff --git a/core/error/error.go b/core/error/error.go index 1f5506a5..50cce244 100644 --- a/core/error/error.go +++ b/core/error/error.go @@ -6,6 +6,9 @@ import ( "strconv" ) +// The `Error`type defines custom error for Beego. It is used by every module +// in Beego. Each `Error` message contains three pieces of data: error code, +// error message. More docs http://beego.me/docs/module/error.md type Error struct { Code codes.Code Msg string @@ -26,21 +29,24 @@ func Errorf(c codes.Code, format string, a ...interface{}) error { return Err(c, fmt.Sprintf(format, a...)) } +// Error returns formatted message for user. func (e *Error) Error() string { codeSrt := strconv.FormatUint(uint64(e.GetCode()), 10) return fmt.Sprintf("beego error: code = %s desc = %s", codeSrt, e.GetMessage()) } -func (x *Error) GetCode() codes.Code { - if x != nil { - return x.Code +// GetCode returns Error's Code +func (e *Error) GetCode() codes.Code { + if e != nil { + return e.Code } return 0 } -func (x *Error) GetMessage() string { - if x != nil { - return x.Msg +// GetMessage returns Error's Msg. +func (e *Error) GetMessage() string { + if e != nil { + return e.Msg } return "" } \ No newline at end of file diff --git a/core/error/error_test.go b/core/error/error_test.go new file mode 100644 index 00000000..30b61164 --- /dev/null +++ b/core/error/error_test.go @@ -0,0 +1,151 @@ +package error + +import ( + "github.com/beego/beego/v2/core/codes" + "reflect" + "testing" +) + +func TestErr(t *testing.T) { + type args struct { + c codes.Code + msg string + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + {name: "1", args: args{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := Err(tt.args.c, tt.args.msg); (err != nil) != tt.wantErr { + t.Errorf("Err() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestError_Error(t *testing.T) { + type fields struct { + Code codes.Code + Msg string + } + tests := []struct { + name string + fields fields + 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\""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &Error{ + Code: tt.fields.Code, + Msg: tt.fields.Msg, + } + if got := e.Error(); got != tt.want { + t.Errorf("Error() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestError_GetCode(t *testing.T) { + type fields struct { + Code codes.Code + Msg string + } + tests := []struct { + name string + fields fields + want codes.Code + }{ + // TODO: Add test cases. + {name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: codes.SessionSessionStartError}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &Error{ + Code: tt.fields.Code, + Msg: tt.fields.Msg, + } + if got := e.GetCode(); got != tt.want { + t.Errorf("GetCode() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestError_GetMessage(t *testing.T) { + type fields struct { + Code codes.Code + Msg string + } + tests := []struct { + name string + fields fields + want string + }{ + // TODO: Add test cases. + {name: "1", fields: fields{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: codes.CodeToStr[codes.SessionSessionStartError]}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &Error{ + Code: tt.fields.Code, + Msg: tt.fields.Msg, + } + if got := e.GetMessage(); got != tt.want { + t.Errorf("GetMessage() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestErrorf(t *testing.T) { + type args struct { + c codes.Code + format string + a []interface{} + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + {name: "1", args: args{codes.SessionSessionStartError, "%s", []interface{}{codes.CodeToStr[codes.SessionSessionStartError]}}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := Errorf(tt.args.c, tt.args.format, tt.args.a...); (err != nil) != tt.wantErr { + t.Errorf("Errorf() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestNew(t *testing.T) { + type args struct { + c codes.Code + msg string + } + tests := []struct { + name string + args args + want *Error + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.c, tt.args.msg); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() = %v, want %v", got, tt.want) + } + }) + } +}