Documentation ¶
Index ¶
- func Is(err error, target ErrCode) bool
- func IsErrCode(err error) bool
- type Code
- func (e *Code) AddDetails(details ...interface{}) (code *Code)
- func (e *Code) Code() int32
- func (e *Code) Details() []interface{}
- func (e *Code) Error() string
- 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 Registry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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 ¶
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 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 NewWithReserved ¶
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) 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.