merror

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: MIT Imports: 7 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

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 Cause

func Cause(err error) error

Cause returns the root cause of `err`.

func Code

func Code(err error) mcode.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

func Current(err error) error

Current creates and returns the current level error. If the current level error is nil, it returns nil.

func Equal

func Equal(err, target error) bool

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 HasStack

func HasStack(err error) bool

HasStack checks and reports whether `err` implements the `gerror.IStack` interface.

func Is

func Is(err, target error) bool

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 New

func New(text string) error

New creates a new error. Example: err := merror.New("username cannot be empty")

func NewCode

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

NewCode creates a new error with the specified error code. Example: err := merror.NewCode(mcode.ValidationError)

func NewCodef

func NewCodef(code mcode.Code, format string, args ...any) error

NewCodef creates a new error with the specified error code. Example: err := merror.NewCodef(mcode.ValidationError, "username %s cannot be empty", admin)

func Newf

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

Newf creates a new error. Example: err := merror.Newf("username %s cannot be empty", admin)

func Stack

func Stack(err error) string

Stack returns the string of the stack caller information. If `err` does not support stack, it will return the error string directly.

func Unwrap

func Unwrap(err error) error

Unwrap returns the next level error. If the current level error or the next level error is nil, it returns nil.

func Wrap

func Wrap(err error, text string) error

Wrap wraps an error. Example: err := merror.Wrap(err, "username cannot be empty")

func WrapCode

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

WrapCode wraps an error and appends the specified error code. Example: err := merror.WrapCode(err, mcode.ValidationError)

func WrapCodef

func WrapCodef(err error, code mcode.Code, format string, args ...any) error

WrapCodef wraps an error and appends the specified error code and formatted text. Example: err := merror.WrapCodef(err, mcode.ValidationError, "username %s cannot be empty", admin)

func Wrapf

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

Wrapf wraps an error. Example: err := merror.Wrapf(err, "username %s cannot be empty", admin)

Types

type Error

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

Error is the error structure.

func (*Error) Cause

func (err *Error) Cause() error

Cause returns the root error.

func (*Error) Code

func (err *Error) Code() mcode.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

func (err *Error) Current() error

Current creates and returns the current error. If the current error is nil, it returns nil.

func (*Error) Equal

func (err *Error) Equal(target error) bool

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) Error

func (err *Error) Error() string

Error implements the Error interface, it returns all error information.

func (*Error) Format

func (err *Error) Format(s fmt.State, verb rune)

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

func (err Error) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Error) SetCode

func (err *Error) SetCode(code mcode.Code)

SetCode sets the error code.

func (*Error) Stack

func (err *Error) Stack() string

Stack returns the stack information of the error.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap is an alias function for `Next`. It is only for implementing the stdlib errors.Unwrap interface after Go version 1.17.

type ICause

type ICause interface {
	Error() string
	Cause() error
}

ICause defines the interface for the Cause functionality.

type ICode

type ICode interface {
	Error() string
	Code() mcode.Code
}

ICode defines the interface for the Code functionality.

type ICurrent

type ICurrent interface {
	Error() string
	Current() error
}

ICurrent defines the interface for the Current functionality.

type IEqual

type IEqual interface {
	Error() string
	Equal(target error) bool
}

IEqual defines the interface for comparing two errors for equality.

type IStack

type IStack interface {
	Error() string
	Stack() string
}

IStack defines the interface for the Stack functionality.

type IUnwrap

type IUnwrap interface {
	Error() string
	Unwrap() error
}

IUnwrap defines the interface for the Unwrap functionality.

Jump to

Keyboard shortcuts

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