errors

package
v0.0.0-...-ff37333 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeOK                  = 0     // 200 成功ok
	CodeInternalServerError = 10000 // 500 服务内部失败
	CodeUnauthorized        = 10001 // 401 用户未传token
	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

func Bomb(err error, code int, msg, chMsg string)

Bomb 直接panic,like a bomb! 注意:调用panic()会比 return err 慢,所以使用Bomb的error都应是不被容忍的错误,是须后续修正的错误。

func ErrorMessage

func ErrorMessage(code int) string

func ErrorMessageCh

func ErrorMessageCh(code int) string

func Recover

func Recover() any

Recover 捕获错误并recover,须使用defer调用,打印黄色错误信息到stdin

func StatusCode

func StatusCode(code int) int

StatusCode 使用自定义code取得http status code

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 New

func New(err error, code int, msg, chMsg string) *Error

New 创建自定义Error

func Wrap

func Wrap(err error, code int) *Error

Wrap code为自定义错误码

func Wrap404Error

func Wrap404Error(err error) *Error

func WrapForBiddenError

func WrapForBiddenError(err error) *Error

func WrapInternalServerError

func WrapInternalServerError(err error) *Error

func WrapInvalidParamsError

func WrapInvalidParamsError(err error) *Error

func WrapUnauthorizedError

func WrapUnauthorizedError(err error) *Error

func (Error) Error

func (err Error) Error() string

Error 满足go原生error interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL