Documentation
¶
Index ¶
- func As(err error, target any) bool
- func Cause(err error) error
- func Code(err error) mcode.Code
- func Current(err error) error
- func Equal(err, target error) bool
- func HasStack(err error) bool
- func Is(err, target error) bool
- func New(text string) error
- func NewCode(code mcode.Code, text ...string) error
- func NewCodef(code mcode.Code, format string, args ...any) error
- func Newf(format string, a ...any) error
- func Stack(err error) string
- func Unwrap(err error) error
- func Wrap(err error, text string) error
- func WrapCode(err error, code mcode.Code, text ...string) error
- func WrapCodef(err error, code mcode.Code, format string, args ...any) error
- func Wrapf(err error, format string, a ...any) error
- type Error
- func (err *Error) Cause() error
- func (err *Error) Code() mcode.Code
- func (err *Error) Current() error
- func (err *Error) Equal(target error) bool
- func (err *Error) Error() string
- func (err *Error) Format(s fmt.State, verb rune)
- func (err Error) MarshalJSON() ([]byte, error)
- func (err *Error) SetCode(code mcode.Code)
- func (err *Error) Stack() string
- func (err *Error) Unwrap() error
- type ICause
- type ICode
- type ICurrent
- type IEqual
- type IStack
- type IUnwrap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As searches for the first error in the chain of errors that matches `target`. If found, it sets `target` to the error value and returns true.
The error chain consists of `err` itself, followed by the error sequence obtained by repeatedly calling `Unwrap`.
If the specific value of the error can be assigned to the value pointed to by `target`, or the error has a method `As(interface{}) bool` so that `As(target)` returns true, then the error matches target. In the latter case, the As method is responsible for setting target.
If target is not a pointer to a type that implements the error interface or any interface type, As will panic. If err is nil, As returns false.
func Code ¶
Code gets the error code of the error. If there is no error code, and the error does not implement the ICode interface, it returns CodeNil.
func Current ¶
Current creates and returns the current level error. If the current level error is nil, it returns nil.
func Equal ¶
Equal reports whether `err` is equal to `target`. Note that in the default comparison logic of `Error`, if the `code` and `text` of the two errors are the same, it is considered that they are the same.
func Is ¶
Is reports whether `err` is in the chain of errors. There is a similar function `HasError`, it is designed and implemented before the `errors.Is` function of the go standard library. Now it is an alias of the `errors.Is` function of the go standard library, to ensure the same performance as the go standard library.
func NewCode ¶
NewCode creates a new error with the specified error code. Example: err := merror.NewCode(mcode.ValidationError)
func NewCodef ¶
NewCodef creates a new error with the specified error code. Example: err := merror.NewCodef(mcode.ValidationError, "username %s cannot be empty", admin)
func Newf ¶
Newf creates a new error. Example: err := merror.Newf("username %s cannot be empty", admin)
func Stack ¶
Stack returns the string of the stack caller information. If `err` does not support stack, it will return the error string directly.
func Unwrap ¶
Unwrap returns the next level error. If the current level error or the next level error is nil, it returns nil.
func WrapCode ¶
WrapCode wraps an error and appends the specified error code. Example: err := merror.WrapCode(err, mcode.ValidationError)
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the error structure.
func (*Error) Code ¶
Code returns the error code. If the error does not have a set error code, it will return the error code of the error returned by the `Unwrap` method.
func (*Error) Current ¶
Current creates and returns the current error. If the current error is nil, it returns nil.
func (*Error) Equal ¶
Equal compares two errors for equality. Note that in the default error comparison, only when their `code` and `text` are the same, the error is considered the same.
func (*Error) Format ¶
Format implements the fmt.Formatter interface, it can format the error information.
Format specifiers:
%s: error information +v: error information and stack information
func (Error) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.