errcode

package
v2.0.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 ...interface{}) (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() []interface{}

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 "(no message)" as a default message.

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 Code.
	// It returns nil if no details are attached.
	Details() []interface{}
}

ErrCode is the interface implemented by an error code.

type Registry

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

Registry represents an error code registry.

func New

func New() *Registry

New creates a new error code registry.

func NewWithReserved

func NewWithReserved(reserveFunc func(code int32) bool) *Registry

NewWithReserved creates a new error code registry with reserved codes, calling Register with a reserved code causes a panic. Reserved code can be registered by calling RegisterReserved.

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