error

package
v0.0.0-...-4c22de1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package error provides rich functionalities to manipulate errors.

For maintainers, please note that this package is a basic package, which SHOULD NOT import extra packages except standard packages and internal packages, to avoid cycle imports.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the root cause error of `err`.

func Code

func Code(err error) code.Code

Code returns the error code of the current error. It returns `CodeNil` if it has no error code or it does not implement the Code interface.

func Current

func Current(err error) error

Current creates and returns the current level error. It returns nil if the current level error is nil.

func Equal

func Equal(err, target error) bool

Equal reports whether the current error `err` equals to the error `target`. Please note that, in default comparison logic for `Error`, the errors are considered the same if both the `code` and `text` of them are the same.

func HasCode

func HasCode(err error, c code.Code) bool

HasCode checks and reports whether `err` has `code` in its chaining errors.

func HasError

func HasError(err, target error) bool

HasError is an alias of Is, which has more easily understandable semantics.

func HasStack

func HasStack(err error) bool

HasStack checks and reports whether `err` implements the `ErrorStack` interface.

func Is

func Is(err, target error) bool

Is reports whether the current error `err` has error `target` in its chaining errors. It is just for implementation of stdlib errors.Is from Go version 1.17.

func New

func New(text string) error

New creates and returns an error which is formatted from the given text.

func NewCode

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

NewCode creates and returns an error that has error code and given text.

func NewCodeSkip

func NewCodeSkip(c code.Code, skip int, text ...string) error

NewCodeSkip creates and returns an error which has error code and is formatted from given text. The parameter `skip` specifies the stack callers skipped amount.

func NewCodeSkipf

func NewCodeSkipf(c code.Code, skip int, format string, args ...interface{}) error

NewCodeSkipf returns an error that has error code and formats as the given format and args. The parameter `skip` specifies the stack callers skipped amount.

func NewCodef

func NewCodef(c code.Code, format string, args ...interface{}) error

NewCodef returns an error that has error code and formats as the given format and args.

func NewOption

func NewOption(option Option) error

NewOption creates and returns a custom error with Option. Deprecated: use NewWithOption instead.

func NewSkip

func NewSkip(skip int, text string) error

NewSkip creates and returns an error which is formatted from the given text. The parameter `skip` specifies the stack callers skipped amount.

func NewSkipf

func NewSkipf(skip int, format string, args ...interface{}) error

NewSkipf returns an error that formats as the given format and args. The parameter `skip` specifies the stack callers skipped amount.

func NewWithOption

func NewWithOption(option Option) error

NewWithOption creates and returns a custom error with Option. It is the advanced usage for creating errors, which is often used internally in the framework.

func Newf

func Newf(format string, args ...interface{}) error

Newf returns an error that formats as the given format and args.

func Stack

func Stack(err error) string

Stack returns the stack callers as a string. It returns the error string directly if the `err` does not support stacks.

func Unwrap

func Unwrap(err error) error

Unwrap returns the next level error. It returns nil if the current level error or the next level error is nil.

func Wrap

func Wrap(err error, text string) error

Wrap wraps an error with text. It returns nil if the given error is nil. Note that it does not lose the error code of the wrapped error, as it inherits the error code from it.

func WrapCode

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

WrapCode wraps error with code and text. It returns nil if the given err is nil.

func WrapCodeSkip

func WrapCodeSkip(c code.Code, skip int, err error, text ...string) error

WrapCodeSkip wraps error with code and text. It returns nil if the given err is nil. The parameter `skip` specifies the stack callers skipped amount.

func WrapCodeSkipf

func WrapCodeSkipf(c code.Code, skip int, err error, format string, args ...interface{}) error

WrapCodeSkipf wraps error with code and text that is formatted with given format and args. It returns nil if the given err is nil. The parameter `skip` specifies the stack callers skipped amount.

func WrapCodef

func WrapCodef(c code.Code, err error, format string, args ...interface{}) error

WrapCodef wraps error with code and format specifier. It returns nil if the given `err` is nil.

func WrapSkip

func WrapSkip(skip int, err error, text string) error

WrapSkip wraps an error with text. It returns nil if the given error is nil. The parameter `skip` specifies the stack callers skipped amount. Note that it does not lose the error code of the wrapped error, as it inherits the error code from it.

func WrapSkipf

func WrapSkipf(skip int, err error, format string, args ...interface{}) error

WrapSkipf wraps an error with text that is formatted with the given format and args. It returns nil if the given error is nil. The parameter `skip` specifies the stack callers skipped amount. Note that it does not lose the error code of the wrapped error, as it inherits the error code from it.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. It returns nil if the given `err` is nil. Note that it does not lose the error code of the wrapped error, as it inherits the error code from it.

Types

type Error

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

Error is a custom error for additional features.

func (*Error) Cause

func (err *Error) Cause() error

Cause returns the root cause error.

func (*Error) Code

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

Code returns the error code. It returns CodeNil if it has no error code.

func (*Error) Current

func (err *Error) Current() error

Current creates and returns the current level error. It returns nil if the current level error is nil.

func (*Error) Equal

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

Equal reports whether the current error `err` equals to the error `target`. Please note that, in default comparison for `Error`, the errors are considered the same if both the `code` and `text` of them are the same.

func (*Error) Error

func (err *Error) Error() string

Error implements the interface of Error, it returns all the error as a string.

func (*Error) Format

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

Format formats the error according to the fmt.Formatter interface.

%v, %s : Print all the error string %-v, %-s : Print current level error string %+s : Print full stack error list %+v : Print the error string and full stack error list

func (*Error) Is

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

Is reports whether the current error `err` has error `target` in its chaining errors. It is just for implementation of stdlib errors.Is from Go version 1.17.

func (Error) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface for Error. Note that do not use a pointer as its receiver here.

func (*Error) SetCode

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

SetCode updates the internal code with the given code.

func (*Error) Stack

func (err *Error) Stack() string

Stack returns the error stack information as a string.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap is an alias of the function `Next`. It is just for implementation of stdlib errors.Unwrap from Go version 1.17.

type ErrorCause

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

ErrorCause is the interface for Cause feature.

type ErrorCode

type ErrorCode interface {
	Error() string
	Code() code.Code
}

ErrorCode is the interface for Code feature.

type ErrorCurrent

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

ErrorCurrent is the interface for Current feature.

type ErrorEqual

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

ErrorEqual is the interface for Equal feature.

type ErrorIs

type ErrorIs interface {
	Error() string
	Is(target error) bool
}

ErrorIs is the interface for Is feature.

type ErrorStack

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

ErrorStack is the interface for Stack feature.

type ErrorUnwrap

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

ErrorUnwrap is the interface for Unwrap feature.

type Option

type Option struct {
	Error error     // Wrapped error if any.
	Stack bool      // Whether to record stack information into the error.
	Text  string    // Error text, which is created by New* functions.
	Code  code.Code // Error code if necessary.
}

Option is an option for creating an error.

Jump to

Keyboard shortcuts

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