From c8a88914f925b8d3b6776aa72aa40d8fb92e8e1b Mon Sep 17 00:00:00 2001 From: zchh < zc123zhangchi@gmail.com> Date: Fri, 8 Jan 2021 22:38:35 +0800 Subject: [PATCH] error module design --- core/codes/codes.go | 11 +++++++++++ core/error/error.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 core/codes/codes.go create mode 100644 core/error/error.go 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