代码拉取完成,页面将自动刷新
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...)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。