diff --git a/core/codes/codes.go b/core/codes/codes.go new file mode 100644 index 00000000..9be5bb9c --- /dev/null +++ b/core/codes/codes.go @@ -0,0 +1,11 @@ +package codes + +type Code uint32 + +const ( + SessionSessionStartError Code = 5001001 +) + +var strToCode = map[string]Code{ + `"SESSION_MODULE_SESSION_START_ERROR"`: SessionSessionStartError, +} diff --git a/core/error/error.go b/core/error/error.go new file mode 100644 index 00000000..9e257313 --- /dev/null +++ b/core/error/error.go @@ -0,0 +1,44 @@ +package error + +import "fmt" +import "github.com/beego/beego/v2/core/codes" + +type Code int32 + +type Error struct { + Code Code + Msg string +} + +// New returns a Error representing c and msg. +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 { + 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...)) +} + +func (e *Error) Error() string { + return fmt.Sprintf("beego error: code = %s desc = %s", e.GetCode(), e.GetMessage()) +} + +func (x *Error) GetCode() Code { + if x != nil { + return x.Code + } + return 0 +} + +func (x *Error) GetMessage() string { + if x != nil { + return x.Msg + } + return "" +} \ No newline at end of file