errcode

package
v2.12.5 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Details added in v2.9.2

func Details(err error) []any

Details returns the details attached to err if it is an ErrCode, it returns nil if err is not an ErrCode.

func Is

func Is(err error, target ErrCode) bool

Is reports whether any error in err's chain matches the target ErrCode.

func IsErrCode

func IsErrCode(err error) bool

IsErrCode reports whether any error in err's chain is an ErrCode.

Types

type Code

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

Code represents an error code. It can be created by calling Registry.Register or Registry.RegisterReserved. Code implements the ErrCode interface.

func (*Code) AddDetails

func (e *Code) AddDetails(details ...any) (code *Code)

AddDetails returns a copy of Code with new error details attached to the returned Code.

func (*Code) Code

func (e *Code) Code() int32

Code returns the integer error code.

func (*Code) Details

func (e *Code) Details() []any

Details returns the error details attached to the Code. It returns nil if no details are attached.

func (*Code) Error

func (e *Code) Error() string

Error returns the error message, it implements the error interface. If message is not registered for the error code, it uses "unknown" as a default message.

func (*Code) Format added in v2.9.2

func (e *Code) Format(f fmt.State, c rune)

func (*Code) Is added in v2.1.0

func (e *Code) Is(target error) bool

Is reports whether an error is ErrCode and the code is same.

This method allows Code to be tested using errors.Is.

func (*Code) MarshalJSON

func (e *Code) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Code) Message

func (e *Code) Message() string

Message returns the error message associated with the error code. If message is not available, it returns an empty string "".

func (*Code) RemoveDetails

func (e *Code) RemoveDetails() (code *Code)

RemoveDetails returns a copy of Code without the error details. If the Code does not have error details, it returns the Code directly instead of a copy. When returning an error code to end-users, you may want to remove the error details which generally should not be exposed to them.

func (*Code) String

func (e *Code) String() string

func (*Code) UnmarshalJSON

func (e *Code) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Code) WithMessage

func (e *Code) WithMessage(msg string) (code *Code)

WithMessage returns a copy of Code with the given message.

type ErrCode

type ErrCode interface {

	// Error returns the error message, it implements the error interface.
	Error() string

	// Code returns the integer error code.
	Code() int32

	// Message returns the registered message for the error code.
	// If message is not available, it returns an empty string "".
	Message() string

	// Details returns the error details attached to the error.
	// It returns nil if no details are attached.
	Details() []any
}

ErrCode is the interface implemented by an error code.

type Option

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

An Option customizes the behavior of Registry.

func WithReserved

func WithReserved(fn func(code int32) bool) Option

WithReserved returns an option to make a Registry to reserve some codes. Calling Register with a reserved code causes a panic. Reserved code can be registered by calling RegisterReserved.

type Registry

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

Registry represents an error code registry.

func New

func New(options ...Option) *Registry

New creates a new error code registry.

func (*Registry) Dump

func (p *Registry) Dump() []*Code

Dump returns all error codes registered with the registry.

func (*Registry) Register

func (p *Registry) Register(code int32, msg string) *Code

Register registers an error code to the registry. If the registry is created by NewWithReserved, it checks the code with the reserve function and panics if the code is reserved.

func (*Registry) RegisterReserved

func (p *Registry) RegisterReserved(code int32, msg string) *Code

RegisterReserved registers an error code to the registry. It does not check the reserve function, but simply adds the code to the register.

func (*Registry) UpdateMessages

func (p *Registry) UpdateMessages(messages map[int32]string)

UpdateMessages updates error messages to the registry. This method copies the underlying message map, it's safe for concurrent use.

Jump to

Keyboard shortcuts

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