Documentation ¶
Index ¶
- Constants
- func Reserve(err interface{})
- type CodedError
- func (e CodedError) Cause() error
- func (e CodedError) Code() int64
- func (e CodedError) CodeMask() int64
- func (e CodedError) Error() string
- func (e CodedError) Is(target error) bool
- func (e CodedError) MarshalBinary() ([]byte, error)
- func (e CodedError) MarshalText() ([]byte, error)
- func (e CodedError) RootCause() error
- func (e *CodedError) UnmarshalBinary(data []byte) error
- func (e CodedError) WithCause(cause error, msg string, args ...interface{}) *CodedError
- func (e CodedError) WithMessage(msg string, args ...interface{}) *CodedError
- type ComparableError
- type ComparableErrorCoder
- type ErrorCoder
- type Hasher
- type NestedError
- type Unwrapper
- type WrappedError
Constants ¶
const ( ReservedOffset = 32 ReservedMask = ^int64(0) << ReservedOffset ErrorTypeOffset = 24 ErrorTypeMask = ^int64(0) << ErrorTypeOffset ErrorSubTypeOffset = 12 ErrorSubTypeMask = ^int64(0) << ErrorSubTypeOffset DefaultErrorCodeMask = ^int64(0) )
Variables ¶
This section is empty.
Functions ¶
func Reserve ¶
func Reserve(err interface{})
Reserve is used for error hierarchy defining packages to validate and reserve its error code range it's usually called during init() this funciton does following things:
- validate given err implements error, ErrorCoder and ComparableErrorCoder
- the mask equals ReservedMask (a category error created via NewErrorCategory)
- bits lower than ReservedOffset of the given error's code are all 0
- if the code is available (not registered by other packages)
- try to register the error's implementation with gob
Types ¶
type CodedError ¶
CodedError implements error, Code, CodeMask, NestedError, ComparableError, Unwrapper encoding.TextMarshaler, json.Marshaler, encoding.BinaryMarshaler, encoding.BinaryUnmarshaler
func NewCodedError ¶
func NewCodedError(code int64, e interface{}, causes ...interface{}) *CodedError
NewCodedError creates concrete error. it cannot be used as ErrorType or ErrorSubType comparison supported item are string, error, fmt.Stringer
func NewErrorCategory ¶
func NewErrorCategory(code int64, e interface{}) *CodedError
func NewErrorSubType ¶
func NewErrorSubType(code int64, e interface{}) *CodedError
func NewErrorType ¶
func NewErrorType(code int64, e interface{}) *CodedError
func (CodedError) Cause ¶
func (e CodedError) Cause() error
func (CodedError) Code ¶
func (e CodedError) Code() int64
func (CodedError) CodeMask ¶
func (e CodedError) CodeMask() int64
func (CodedError) Error ¶
func (e CodedError) Error() string
func (CodedError) Is ¶
func (e CodedError) Is(target error) bool
Is return true if
- target has same ErrCode, OR
- target is a type/sub-type error and the receiver error is in same type/sub-type
func (CodedError) MarshalBinary ¶
func (e CodedError) MarshalBinary() ([]byte, error)
MarshalBinary implements encoding.BinaryMarshaler interface ErrCode, ErrMask, error.Error() are written into byte array in the mentioned order ErrCode and ErrMask are written as 64 bits with binary.BigEndian Note: currently we don't serialize Cause() to avoid cyclic reference
func (CodedError) MarshalText ¶
func (e CodedError) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler
func (CodedError) RootCause ¶
func (e CodedError) RootCause() error
func (*CodedError) UnmarshalBinary ¶
func (e *CodedError) UnmarshalBinary(data []byte) error
UnmarshalBinary implements encoding.BinaryUnmarshaler interface
func (CodedError) WithCause ¶
func (e CodedError) WithCause(cause error, msg string, args ...interface{}) *CodedError
WithCause make a concrete error with given cause and error message
func (CodedError) WithMessage ¶
func (e CodedError) WithMessage(msg string, args ...interface{}) *CodedError
WithMessage make a concrete error with given error message
type ComparableError ¶
type ComparableErrorCoder ¶
type ComparableErrorCoder interface {
CodeMask() int64
}
type ErrorCoder ¶
type ErrorCoder interface {
Code() int64
}
type Hasher ¶
type Hasher interface {
Hash() error
}
Hasher is an interface for error implementations that not naturally hashable to be used as a map key e.g. if a struct containing Slice, Map, Array are not hashable. Therefore, use those errors as map key would panic For implementations that need use error as map key (e.g. map[error]interface{}) should use Hasher.Hash as key Note: CodedError doesn't implement this interface because itself is hashable
type NestedError ¶
type WrappedError ¶
type WrappedError struct { ErrIs error Type *CodedError ErrMsg string }
WrappedError is an embeddable struct that provide a convenient way to implement concrete error within certain error hierarchy without error code. This error implementation contains 3 components: - ErrIs is an anchor error used for comparison. Used for Is - Type is the parent error indicating its type, CodedError. Used for Unwrap - ErrMsg is the error's actual string value. Used for Error
func (WrappedError) Error ¶
func (e WrappedError) Error() string
func (WrappedError) Is ¶
func (e WrappedError) Is(target error) bool
func (WrappedError) MarshalText ¶
func (e WrappedError) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler
func (WrappedError) Unwrap ¶
func (e WrappedError) Unwrap() error
Unwrap returns type error, which makes sure that errors.Is(e, errorType) returns true when errors.Is(e.Type, errorType) is true
func (WrappedError) WithMessage ¶
func (e WrappedError) WithMessage(msg string, args ...interface{}) WrappedError