error module design

This commit is contained in:
zchh 2021-01-08 22:38:35 +08:00
parent 7d2c5486be
commit c8a88914f9
2 changed files with 55 additions and 0 deletions

11
core/codes/codes.go Normal file
View File

@ -0,0 +1,11 @@
package codes
type Code uint32
const (
SessionSessionStartError Code = 5001001
)
var strToCode = map[string]Code{
`"SESSION_MODULE_SESSION_START_ERROR"`: SessionSessionStartError,
}

44
core/error/error.go Normal file
View File

@ -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 ""
}