Documentation ¶
Index ¶
- func Details(err error) []any
- func Is(err error, target ErrCode) bool
- func IsErrCode(err error) bool
- type Code
- func (e *Code) AddDetails(details ...any) (code *Code)
- func (e *Code) Code() int32
- func (e *Code) Details() []any
- func (e *Code) Error() string
- func (e *Code) Format(f fmt.State, c rune)
- func (e *Code) Is(target error) bool
- func (e *Code) MarshalJSON() ([]byte, error)
- func (e *Code) Message() string
- func (e *Code) RemoveDetails() (code *Code)
- func (e *Code) String() string
- func (e *Code) UnmarshalJSON(data []byte) error
- func (e *Code) WithMessage(msg string) (code *Code)
- type ErrCode
- type Option
- type Registry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Details ¶ added in v2.9.2
Details returns the details attached to err if it is an ErrCode, it returns nil if err is not 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 ¶
AddDetails returns a copy of Code with new error details attached to the returned Code.
func (*Code) Details ¶
Details returns the error details attached to the Code. It returns nil if no details are attached.
func (*Code) Error ¶
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) Is ¶ added in v2.1.0
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 ¶
MarshalJSON implements json.Marshaler.
func (*Code) Message ¶
Message returns the error message associated with the error code. If message is not available, it returns an empty string "".
func (*Code) RemoveDetails ¶
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) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Code) WithMessage ¶
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 ¶
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 (*Registry) Register ¶
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 ¶
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 ¶
UpdateMessages updates error messages to the registry. This method copies the underlying message map, it's safe for concurrent use.