完善代码

This commit is contained in:
zchh 2021-01-18 19:56:07 +08:00
parent d47a95df8d
commit 17d6795921
3 changed files with 10 additions and 5 deletions

View File

@ -1,11 +1,14 @@
package codes package codes
// A Code is an unsigned 32-bit error code as defined in the beego spec.
type Code uint32 type Code uint32
const ( const (
// SessionSessionStartError means func SessionStart error in session module.
SessionSessionStartError Code = 5001001 SessionSessionStartError Code = 5001001
) )
// CodeToStr is a map about Code and Code's message
var CodeToStr = map[Code]string{ var CodeToStr = map[Code]string{
SessionSessionStartError : `"SESSION_MODULE_SESSION_START_ERROR"`, SessionSessionStartError : `"SESSION_MODULE_SESSION_START_ERROR"`,
} }

View File

@ -6,9 +6,10 @@ import (
"strconv" "strconv"
) )
// The `Error`type defines custom error for Beego. It is used by every module // 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, // in Beego. Each `Error` message contains three pieces of data: error code,
// error message. More docs http://beego.me/docs/module/error.md // error message.
// More docs http://beego.me/docs/module/error.md.
type Error struct { type Error struct {
Code codes.Code Code codes.Code
Msg string Msg string
@ -35,7 +36,7 @@ func (e *Error) Error() string {
return fmt.Sprintf("beego error: code = %s desc = %s", codeSrt, e.GetMessage()) return fmt.Sprintf("beego error: code = %s desc = %s", codeSrt, e.GetMessage())
} }
// GetCode returns Error's Code // GetCode returns Error's Code.
func (e *Error) GetCode() codes.Code { func (e *Error) GetCode() codes.Code {
if e != nil { if e != nil {
return e.Code return e.Code

View File

@ -118,7 +118,7 @@ func TestErrorf(t *testing.T) {
wantErr bool wantErr bool
}{ }{
// TODO: Add test cases. // TODO: Add test cases.
{name: "1", args: args{codes.SessionSessionStartError, "%s", []interface{}{codes.CodeToStr[codes.SessionSessionStartError]}}}, {name: "1", args: args{codes.SessionSessionStartError, "%s", []interface{}{codes.CodeToStr[codes.SessionSessionStartError]}}, wantErr: true},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -140,6 +140,7 @@ func TestNew(t *testing.T) {
want *Error want *Error
}{ }{
// TODO: Add test cases. // TODO: Add test cases.
{name: "1", args: args{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: &Error{Code:codes.SessionSessionStartError, Msg:codes.CodeToStr[codes.SessionSessionStartError]}},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {