errorutils

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
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:

  1. validate given err implements error, ErrorCoder and ComparableErrorCoder
  2. the mask equals ReservedMask (a category error created via NewErrorCategory)
  3. bits lower than ReservedOffset of the given error's code are all 0
  4. if the code is available (not registered by other packages)
  5. try to register the error's implementation with gob

Types

type CodedError

type CodedError struct {
	ErrMsg  string
	ErrCode int64
	ErrMask int64
	Nested  error
}

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

  1. target has same ErrCode, OR
  2. 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 ComparableError interface {
	Is(target error) bool
}

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 NestedError interface {
	// Cause returns directly nested error
	Cause() error

	// RootCause returns the root cause of error, equivalent to calling Cause repeatedly
	RootCause() error
}

type Unwrapper

type Unwrapper interface {
	Unwrap() error
}

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

Jump to

Keyboard shortcuts

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