Merge branch 'develop' of https://github.com/zchh/beego into error
This commit is contained in:
commit
05f8058be1
14
core/codes/codes.go
Normal file
14
core/codes/codes.go
Normal file
@ -0,0 +1,14 @@
|
||||
package codes
|
||||
|
||||
// A Code is an unsigned 32-bit error code as defined in the beego spec.
|
||||
type Code uint32
|
||||
|
||||
const (
|
||||
// SessionSessionStartError means func SessionStart error in session module.
|
||||
SessionSessionStartError Code = 5001001
|
||||
)
|
||||
|
||||
// CodeToStr is a map about Code and Code's message
|
||||
var CodeToStr = map[Code]string{
|
||||
SessionSessionStartError : `"SESSION_MODULE_SESSION_START_ERROR"`,
|
||||
}
|
||||
53
core/error/error.go
Normal file
53
core/error/error.go
Normal file
@ -0,0 +1,53 @@
|
||||
package error
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/beego/beego/v2/core/codes"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// New returns a Error representing c and msg.
|
||||
func New(c codes.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 {
|
||||
return New(c, msg)
|
||||
}
|
||||
|
||||
// Errorf returns Error(c, fmt.Sprintf(format, a...)).
|
||||
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())
|
||||
}
|
||||
|
||||
// GetCode returns Error's Code.
|
||||
func (e *Error) GetCode() codes.Code {
|
||||
if e != nil {
|
||||
return e.Code
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetMessage returns Error's Msg.
|
||||
func (e *Error) GetMessage() string {
|
||||
if e != nil {
|
||||
return e.Msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
152
core/error/error_test.go
Normal file
152
core/error/error_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
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]}}, wantErr: true},
|
||||
}
|
||||
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.
|
||||
{name: "1", args: args{codes.SessionSessionStartError, codes.CodeToStr[codes.SessionSessionStartError]}, want: &Error{Code:codes.SessionSessionStartError, Msg:codes.CodeToStr[codes.SessionSessionStartError]}},
|
||||
}
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user