errors

package
v0.0.0-...-befce3c Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As 将错误转换为特定类型错误,当错误为链式错误时,错误链中任意错误可转换为目标错误则完成转换

func Cause

func Cause(err error) error

Cause 完整解包错误,得到最内层错误

func DecodeError

func DecodeError(ctx context.Context, enc errors.EncodedError) error

DecodeError 解码错误,还原错误类型

func EncodeError

func EncodeError(ctx context.Context, err error) errors.EncodedError

EncodeError 将err进行编码,编码后为一个基于protobuf生成的结构体,主要用于跨网络进行错误传输

func FlattenHints

func FlattenHints(err error) string

FlattenHints 将全部提示信息扁平化为一条提示信息返回

func GetAllHints

func GetAllHints(err error) []string

GetAllHints 获取错误中包含的全部提示信息

func GetCode

func GetCode(err error) int

GetCode 获取错误中包含的业务状态码

func GetGrpcCode

func GetGrpcCode(err error) codes.Code

GetGrpcCode 获取错误中的grpc状态码

func GetHint

func GetHint(err error) string

GetHint 获取错误中包含的最后一条提示信息

func GetHttpCode

func GetHttpCode(err error, defaultCode int) int

GetHttpCode 获取错误中的业务状态码

func GetStack

func GetStack(err error) []string

GetStack 获取错误中的堆栈信息,以易于日志阅读及解析的字符串表示

func GetStackTrace

func GetStackTrace(err error) errbase.StackTrace

GetStackTrace 获取错误中的堆栈信息,若错误为链式错误,则使用最外层错误的堆栈信息,以保证错误堆栈覆盖生效 errbase.StackTrace 实际上是 pkg/errors中的StackTrace,主流日志库均可友好打印堆栈信息

func Is

func Is(err, target error) bool

Is 判断错误是否为相同错误,当错误为链式错误时,错误链中任意错误和目标错误相等则视作相等

func Join

func Join(errs ...error) error

Join 将多个错误包装为一个错误

func New

func New(msg string) error

New 创建一个错误,此错误携带堆栈信息

func NewBiz

func NewBiz(code int, hint string, msg string) error

NewBiz 快速创建一个业务错误,业务错误一般包含错误信息、状态码及提示信息, 业务错误一般为前置判断异常,故http状态码默认为200,grpc状态码默认为 codes.FailedPrecondition

func NewBizf

func NewBizf(code int, hint string, format string, args ...any) error

NewBizf 使用格式化字符串创建一个业务错误,业务错误一般包含错误信息、状态码及提示信息, 业务错误一般为前置判断异常,故http状态码默认为200,grpc状态码默认为 codes.FailedPrecondition

func NewWithCode

func NewWithCode(code int, msg string) error

NewWithCode 创建带有业务状态码的错误

func NewWithCodef

func NewWithCodef(code int, format string, args ...any) error

NewWithCodef 使用格式化方式创建带有业务状态码的错误

func Newf

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

Newf 使用格式化方式创建一个错误,此错误携带堆栈信息

func Unwrap

func Unwrap(err error) error

Unwrap 解包一层错误

func WithCode

func WithCode(err error, code int) error

WithCode 为错误附加业务状态码,业务状态码相同时,则直接认为是同一个错误

func WithGrpcCode

func WithGrpcCode(err error, code codes.Code) error

WithGrpcCode 为错误附加grpc状态码

func WithHint

func WithHint(err error, hint string) error

WithHint 为错误附加提示信息,提示信息一般用于直接展示给用户

func WithHintf

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

WithHintf 为错误附加格式化提示信息,提示信息一般用于直接展示给用户

func WithHttpCode

func WithHttpCode(err error, code int) error

WithHttpCode 为错误附加http状态码

func WithMessage

func WithMessage(err error, msg string) error

WithMessage 为错误附加上下文信息

func WithMessagef

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

WithMessagef 使用格式化字符串为错误附加上下文信息

func WithStack

func WithStack(err error) error

WithStack 为错误附加堆栈信息,此堆栈信息会覆盖原本错误中的堆栈信息 若错误已经存在堆栈信息,则应当只在需要重新指定错误堆栈时才使用此函数

func Wrap

func Wrap(err error, msg string) error

Wrap 使用msg作为前缀包装错误,并附加堆栈信息,此堆栈信息会覆盖原本错误中的堆栈信息 若仅是为了给错误添加上下文信息,应当使用 WithMessage

func Wrapf

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

Wrapf 使用格式化字符串作为前缀包装错误,并附加堆栈信息,此堆栈信息会覆盖原本错误中的堆栈信息 若仅是为了给错误添加上下文信息,应当使用 WithMessagef

Types

type ErrorMarshal

type ErrorMarshal struct {
	Message string   `json:"message"`
	Stack   []string `json:"stack"`
}

func Marshal

func Marshal(err error) *ErrorMarshal

Marshal 获取错误的序列化信息,统一日志结构

func (ErrorMarshal) MarshalJSON

func (e ErrorMarshal) MarshalJSON() ([]byte, error)

func (ErrorMarshal) MarshalText

func (e ErrorMarshal) MarshalText() ([]byte, error)

Jump to

Keyboard shortcuts

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