merror

package
v0.0.0-...-9e15026 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CodeFromError

func CodeFromError(err error) mcode.Code

CodeFromError 从错误中获取错误码

func New

func New(text string, a ...any) error

New 创建一个新的基础错误 适用于创建一个全新的错误,默认使用 InternalError 错误码 示例:err := merror.New("数据库连接失败")

func NewCode

func NewCode(code mcode.Code, text string, a ...any) error

NewCode 创建一个带指定错误码的新错误 适用于创建一个全新的错误,并指定特定的错误码 示例:err := merror.NewCode(mcode.ValidationError, "用户名不能为空")

func Wrap

func Wrap(err error, text string) error

Wrap 包装已有错误,保持原错误码 适用于包装底层错误,添加上下文信息,同时保留原始错误的错误码 示例:return merror.Wrap(err, "查询用户信息失败")

func WrapCode

func WrapCode(err error, code mcode.Code, text string) error

WrapCode 包装已有错误,并指定新的错误码 适用于包装底层错误,但需要改变错误码的场景 示例:return merror.WrapCode(err, mcode.ValidationError, "用户验证失败")

func WrapMsg

func WrapMsg(err error, text string, a ...any) error

WrapMsg 包装错误,保留原错误码但替换提示信息 适用于需要修改错误提示但保持原始错误码的场景 示例:return merror.WrapMsg(err, "用户注册失败,请稍后重试")

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error 错误结构体

func (*Error) Code

func (e *Error) Code() mcode.Code

Code 获取错误码

func (*Error) Error

func (e *Error) Error() string

func (*Error) Stack

func (e *Error) Stack() string

Stack 获取错误堆栈信息

Jump to

Keyboard shortcuts

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