1 Star 0 Fork 0

后端组 / mvc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
error.go 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
heweiosc 提交于 2020-04-18 15:13 . update env
package components
import (
"fmt"
"gitee.com/nuokwan_backend_group/mvc/Base"
"runtime"
"strings"
)
// 自定义
type CustomError struct {
code int
message string
ErrType string
Parent error
}
func NewCustomError(msg string, code int, tyName string, parent ...error) Base.ErrorInstance {
var parentError error
if len(parent) > 0 {
parentError = parent[0]
}
return &CustomError{
code: code,
message: msg,
ErrType: tyName,
Parent: parentError,
}
}
func (c *CustomError) Error() string {
return fmt.Sprintf("message:%s,code: %d", c.Message(), c.Code())
}
func (c *CustomError) Code() int {
return c.code
}
func (c *CustomError) ErrorType() string {
return c.ErrType
}
func (c *CustomError) Message() string {
return c.message
}
func (c *CustomError) Traces() []string {
var stack []string
if c.code != 0 && c.message != "" {
for i := 4; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
f := runtime.FuncForPC(pc)
if f.Name() != "runtime.main" && f.Name() != "runtime.goexit" {
str := fmt.Sprintf("file:%s, at dline:%d ,funcName:%s, error:%s", file, line, f.Name(), c.Error())
stack = append(stack, str)
}
}
}
return stack
}
func (c *CustomError) TraceString() string {
return strings.Join(c.Traces(), "\n")
}
func (c *CustomError) Set(key string, value interface{}) Base.ErrorInstance {
switch key {
case "code":
c.code = value.(int)
case "message":
if c.message != "" {
c.message = value.(string)
}
case "errorType":
if c.ErrType == "" {
c.ErrType = value.(string)
}
case "parent":
if c.Parent == nil {
switch value.(type) {
case Base.ErrorInstance:
c.Parent = value.(Base.ErrorInstance)
case error:
err := value.(error)
c.Parent = NewError(0, err.Error())
}
}
}
return c
}
func NewError(code int, msg string, parent ...error) Base.ErrorInstance {
return NewCustomError(msg, code, "error", parent...)
}
func (c *CustomError) New(msg string, code int, parent ...error) error {
return NewCustomError(msg, code, c.ErrType).Set("parent", parent)
}
1
https://gitee.com/nuokwan_backend_group/mvc.git
git@gitee.com:nuokwan_backend_group/mvc.git
nuokwan_backend_group
mvc
mvc
1bf86c47ef7b

搜索帮助

53164aa7 5694891 3bd8fe86 5694891