Documentation
¶
Index ¶
- Constants
- func Bomb(err error, code int, msg, chMsg string)
- func ErrorMessage(code int) string
- func ErrorMessageCh(code int) string
- func Recover() any
- func StatusCode(code int) int
- type Error
- func New(err error, code int, msg, chMsg string) *Error
- func Wrap(err error, code int) *Error
- func Wrap404Error(err error) *Error
- func WrapForBiddenError(err error) *Error
- func WrapInternalServerError(err error) *Error
- func WrapInvalidParamsError(err error) *Error
- func WrapUnauthorizedError(err error) *Error
Constants ¶
View Source
const ( CodeOK = 0 // 200 成功ok CodeInternalServerError = 10000 // 500 服务内部失败 CodeForbidden = 10002 // 403 鉴权失败,如token无效或者过期 CodeInvalidParams = 10003 // 400 参数错误 CodeResourcesNotFount = 10004 // 404 资源未找到 CodeResourcesHasExist = 10005 // 409 资源已存在 CodeResourcesConflict = 10006 // 409 状态冲突 CodeUnknownError = 10007 // 500 未知异常 CodeNoRight2Modify = 10008 // 403 用户没权限修改相关资源 )
自定义错误类型与http status code是多对一的关系,所以要自定义错误码
Variables ¶
This section is empty.
Functions ¶
func Bomb ¶
Bomb 直接panic,like a bomb! 注意:调用panic()会比 return err 慢,所以使用Bomb的error都应是不被容忍的错误,是须后续修正的错误。
func ErrorMessage ¶
func ErrorMessageCh ¶
Types ¶
type Error ¶
type Error struct { Cause error `json:"error"` // 用来装入原始error,不影响对原始错误的判断(如if err == sql.ErrNoRows的情况),可以为nil Code int `json:"code"` // 自定义的错误码 Message string `json:"message"` // 自定义的错误信息 ChMsg string `json:"ch_message"` // 中文错误信息 }
Error 自定义错误,替代go原生的error,避免层层返回层层判断,发生error时直接Bomb,写入code与message方便recover时解析
func Wrap404Error ¶
func WrapForBiddenError ¶
func WrapInternalServerError ¶
func WrapInvalidParamsError ¶
func WrapUnauthorizedError ¶
Click to show internal directories.
Click to hide internal directories.