完善错误处理逻辑,统一使用 BizErr 包装业务错误,提供打印源码跳转并返回合适的 http 状态码
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"platform/pkg/env"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// region page
|
||||
|
||||
type PageReq struct {
|
||||
@@ -40,3 +46,56 @@ type PageResp struct {
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region error
|
||||
|
||||
type BizErr struct {
|
||||
msg string
|
||||
err error
|
||||
|
||||
errFile string
|
||||
errLine int
|
||||
errFunc string
|
||||
}
|
||||
|
||||
func (e *BizErr) Error() string {
|
||||
if e.err != nil {
|
||||
return e.msg + ":" + e.err.Error()
|
||||
}
|
||||
return e.msg
|
||||
}
|
||||
|
||||
func (e *BizErr) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e *BizErr) Source() *slog.Source {
|
||||
return &slog.Source{
|
||||
Function: e.errFunc,
|
||||
File: e.errFile,
|
||||
Line: e.errLine,
|
||||
}
|
||||
}
|
||||
|
||||
func NewBizErr(msg string, err ...error) (biz *BizErr) {
|
||||
biz = &BizErr{
|
||||
msg: msg,
|
||||
}
|
||||
|
||||
if len(err) > 0 {
|
||||
biz.err = err[0]
|
||||
}
|
||||
|
||||
if env.RunMode == env.RunModeDev {
|
||||
pc, file, line, ok := runtime.Caller(1)
|
||||
if ok {
|
||||
biz.errFile = file
|
||||
biz.errLine = line
|
||||
biz.errFunc = runtime.FuncForPC(pc).Name()
|
||||
}
|
||||
}
|
||||
|
||||
return biz
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
Reference in New Issue
Block a user