6 Star 11 Fork 1

distill/distill-infra

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
errors.go 2.83 KB
一键复制 编辑 原始数据 按行查看 历史
Lyndon Hu 提交于 2020-05-23 20:12 . 主要增加错误封装
package distill_errors
import (
"errors"
"fmt"
"net/http"
"runtime"
"strings"
)
type MErr struct {
Message string // 保存自定义的错误信息
StatusCode int // 错误状态码
rawErr error // 保存原始错误信息
stackPC []uintptr // 保存函数调用栈指针
}
func (e *MErr) Error() string {
return e.Message
}
// RawErr the origin err
func (e MErr) RawErr() error {
return e.rawErr
}
// CallStack get function call stack
func (e MErr) CallStack() string {
frames := runtime.CallersFrames(e.stackPC)
var (
f runtime.Frame
more bool
result string
index int
)
for {
f, more = frames.Next()
if index = strings.Index(f.File, "src"); index != -1 {
// trim GOPATH or GOROOT prifix
f.File = string(f.File[index+4:])
}
result = fmt.Sprintf("%s%s\n\t%s:%d\n", result, f.Function, f.File, f.Line)
if !more {
break
}
}
return result
}
// maintain rawErr and update Message if fmtAndArgs is not empty
// update StatusCode to code if code is not 0
// notice: the returned value is used as error, so, should not return nil
func wrapErr(err error, code int, fmtAndArgs ...interface{}) *MErr {
msg := fmtErrMsg(fmtAndArgs...)
if err == nil {
err = errors.New(msg)
}
if e, ok := err.(*MErr); ok {
if msg != "" {
e.Message = msg
}
if code != 0 {
e.StatusCode = code
}
return e
}
pcs := make([]uintptr, 32)
// skip the first 3 invocations
count := runtime.Callers(3, pcs)
e := &MErr{
StatusCode: code,
Message: msg,
rawErr: err,
stackPC: pcs[:count],
}
if e.Message == "" {
e.Message = err.Error()
}
return e
}
// fmtErrMsg used to format error message
func fmtErrMsg(msgs ...interface{}) string {
if len(msgs) > 1 {
return fmt.Sprintf(msgs[0].(string), msgs[1:]...)
}
if len(msgs) == 1 {
if v, ok := msgs[0].(string); ok {
return v
}
if v, ok := msgs[0].(error); ok {
return v.Error()
}
}
return ""
}
// WrapErr equal to InternalErr(err)
// notice: be careful, the returned value is *MErr, not error
func WrapErr(err error, fmtAndArgs ...interface{}) *MErr {
return wrapErr(err, http.StatusInternalServerError, fmtAndArgs...)
}
// WrapErrWithCode if code is not 0, update StatusCode to code,
// if fmtAndArgs is not nil, update the Message according to fmtAndArgs
// notice: be careful, the returned value is *MErr, not error
func WrapErrWithCode(err error, code int, fmtAndArgs ...interface{}) *MErr {
return wrapErr(err, code, fmtAndArgs...)
}
// NotFoundErr use http.StatusNotFound as StatusCode to express not found err
// if fmtAndArgs is not nil, update the Message according to fmtAndArgs
func NotFoundErr(err error, fmtAndArgs ...interface{}) error {
return wrapErr(err, http.StatusNotFound, fmtAndArgs...)
}
func BadRequest(err error, fmtAndArgs ...interface{}) error {
return wrapErr(err, http.StatusBadRequest, fmtAndArgs...)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/banyanhouse/distill-infra.git
git@gitee.com:banyanhouse/distill-infra.git
banyanhouse
distill-infra
distill-infra
v0.0.15

搜索帮助