fserr

package
v0.2.4-alpha Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As 匹配最外层的与target类型相同的error,将其赋值给target

func ErrBasic

func ErrBasic() int

func ErrDb

func ErrDb() int

func Is

func Is(err, target error) bool

Is 判断err是否是target类型 相比==判断错误,该方式会进行不断地类似于 UmWrap 的操作, 将 UmWrap 后的错误进行比较

func IsCode

func IsCode(err error, code int) bool

func New

func New(format string, args ...any) error

New 创建新的错误,支持格式化占位符

func ParseCode

func ParseCode(err error) *withCode

ParseCode 将错误解析为错误码错误 若err不是错误码错误,则包裹传递错误,其他信息为默认错误码信息 若err为错误码错误,将其转换,将最外层错误信息作为最终错误信息返回 若想得到原始的错误码错误,可以使用As方法

func SetDefault

func SetDefault(httpCode, businessCode int, message string)

SetDefault 设置默认错误码 当错误码匹配失败时,提供的备选方案,已内置默认错误码, 它的HTTP码为200,业务码和信息均为零值

func SetServiceCode

func SetServiceCode(code int)

SetServiceCode 设置服务错误码

func UmWrap

func UmWrap(err error) error

UmWrap 获取包装过的error 若error1使用 Wrap 包装后产出错误error2 当使用 UmWrap 后,返回的是error1

func WithCode

func WithCode(err error, businessCode int, options ...Option) error

WithCode 创建带有错误码的error,支持格式化占位符 使用option可以替换其中信息

func Wrap

func Wrap(err error, format string, args ...any) error

Wrap 包装已有错误,支持格式化占位符

Types

type ErrCode

type ErrCode struct {
	// HttpCode 该错误码建议的HTTP响应码
	HttpCode int `json:"httpCode,omitempty"`
	// BusinessCode 该错误码对应的业务码
	// +optional
	BusinessCode int `json:"businessCode,omitempty"`
	// Message 该错误码对应的错误信息
	// +optional
	Message string `json:"message,omitempty"`
}

ErrCode 产生error所包含的错误码信息 支持http错误码、业务错误码 可通过下方快捷函数快速创建指定http码的错误码

func NewAlreadyExists

func NewAlreadyExists(businessCode int, message string) ErrCode

func NewBadRequest

func NewBadRequest(businessCode int, message string) ErrCode

func NewCode

func NewCode(httpCode, businessCode int, message string) ErrCode

NewCode 创建指定信息的错误码

func NewConflict

func NewConflict(businessCode int, message string) ErrCode

func NewForbidden

func NewForbidden(businessCode int, message string) ErrCode

func NewGenerateNameConflict

func NewGenerateNameConflict(businessCode int, message string) ErrCode

func NewGone

func NewGone(businessCode int, message string) ErrCode

func NewInternalError

func NewInternalError(businessCode int, message string) ErrCode

func NewMethodNotSupported

func NewMethodNotSupported(businessCode int, message string) ErrCode

func NewNotFound

func NewNotFound(businessCode int, message string) ErrCode

func NewOK

func NewOK(businessCode int, message string) ErrCode

func NewRequestEntityTooLargeError

func NewRequestEntityTooLargeError(businessCode int, message string) ErrCode

func NewServiceUnavailable

func NewServiceUnavailable(businessCode int, message string) ErrCode

func NewTimeoutError

func NewTimeoutError(businessCode int, message string) ErrCode

func NewTooManyRequests

func NewTooManyRequests(businessCode int, message string) ErrCode

func NewTooManyRequestsError

func NewTooManyRequestsError(businessCode int, message string) ErrCode

func NewUnauthorized

func NewUnauthorized(businessCode int, message string) ErrCode

type Option

type Option func(*withCode)

func MsgOption

func MsgOption(msg string) Option

Jump to

Keyboard shortcuts

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