Documentation
¶
Overview ¶
Package errors provides lightweight error handling and classification primitives.
The package defines blazing fast classification system. A class is composed of the major, minor and index sub classifications. Each sub classification has different bitwise length with total of 32 bits. Thus a Class is a wrapper over uint32. A major is composed of 8, minor 10 and index of 14 bits.
Example: Class with decimal value of 44205263, in a binary form equals to
00000010101000101000010011001111 which decomposes into: 00000010 - major (8 bit) 1010001010 - minor (10 bit) 00010011001111 - index (14 bit)
The class concept was inspired by the need of multiple errors with the same logic but different messages.
The package provides simple error handling interfaces and functions. It allows to create simple and detailed classified errors.
Index ¶
- Variables
- func IsClass(err error, class Class) bool
- type Class
- func MustNewClass(mjr Major, mnr Minor, index Index) Class
- func MustNewClassWIndex(mjr Major, mnr Minor) Class
- func MustNewMajorClass(mjr Major) Class
- func MustNewMinorClass(mjr Major, mnr Minor) Class
- func NewClass(mjr Major, mnr Minor, index Index) (Class, error)
- func NewClassWIndex(mjr Major, mnr Minor) (Class, error)
- func NewMajorClass(mjr Major) (Class, error)
- func NewMinorClass(mjr Major, mnr Minor) (Class, error)
- type ClassError
- type DetailedError
- type Index
- type Major
- type Minor
- type MultiError
Constants ¶
This section is empty.
Variables ¶
var ( // ClInvalidMajor defines the invalid major error classification. ClInvalidMajor Class // ClInvalidMinor defines the invalid minor error classification. ClInvalidMinor Class // ClInvalidIndex defines the invalid index error classification. ClInvalidIndex Class // MjrInternal is the major internal error class. MjrInternal Major // ClassInternal is the general internal error. ClassInternal Class )
Functions ¶
Types ¶
type Class ¶ added in v0.16.0
type Class uint32
Class is the error classification model. It is composed of the major, minor and index sub classifications. Each sub classification is a different length number, where major is composed of 8, minor 10 and index of 14 bits. Example:
44205263 in a binary form is: 00000010101000101000010011001111 which decomposes into: 00000010 - major (8 bit) - 2 1010001010 - minor (10 bit) - 650 00010011001111 - index (14 bit) - 1231
Major should be a global scope division like 'Repository', 'Marshaler', 'Controller' etc. Minor should divide the 'major' into subclasses like the Repository filter builders, marshaler - invalid field etc. Index is the most precise classification - i.e. Repository - filter builder - unsupported operator.
func MustNewClass ¶ added in v0.16.0
MustNewClass gets new class from the provided 'minor' and 'index'. Panics if any of the arguments is not valid or out of bands.
func MustNewClassWIndex ¶ added in v0.16.0
MustNewClassWIndex creates new 'mjr' Major, 'mnr' Minor 'index' and then a new Class for provided triplet. Panics on error.
func MustNewMajorClass ¶ added in v0.16.0
MustNewMajorClass creates Class from the provided 'mjr' Major. This class contains zero valued 'Minor' and 'Index'. Panics if provided 'mjr' is invalid.
func MustNewMinorClass ¶ added in v0.16.0
MustNewMinorClass creates a class from the provided 'mjr' Major and 'mnr' Minor. Panics when any of the provided arguments is invalid.
func NewClass ¶ added in v0.16.0
NewClass gets new class from the provided 'minor' and 'index'. If any of the arguments is not valid or out of bands the function returns an error.
func NewClassWIndex ¶ added in v0.16.0
NewClassWIndex creates new index and class for provided 'mjr' Major and 'mnr' Minor. Returns error if any of the input values are not valid.
func NewMajorClass ¶ added in v0.16.0
NewMajorClass creates Class from the provided 'mjr' Major. This class contains zero valued 'Minor' and 'Index'. Returns error if the 'mjr' is invalid.
func NewMinorClass ¶ added in v0.16.0
NewMinorClass gets the class from provided 'minor'. The function gets minor's major and gets the major/minor class.
func (Class) Index ¶ added in v0.16.0
Index is a four digit number unique within given minor and major.
type ClassError ¶ added in v0.16.0
ClassError is the interface used for all errors that uses classification system.
func New ¶ added in v0.16.0
func New(c Class, msg string) ClassError
New creates simple ClassError for provided 'c' Class and 'msg' message.
func Newf ¶ added in v0.16.0
func Newf(c Class, format string, args ...interface{}) ClassError
Newf creates simple formatted ClassError for provided 'c' Class, 'format' and arguments 'args'.
type DetailedError ¶ added in v0.16.0
type DetailedError struct { // ID is a unique error instance identification number. ID uuid.UUID // Classification defines the error classification. Classification Class // details contains the detailed information. Details string // message is a message used as a string for the // golang error interface implementation. Message string // Operation is the operation name when the error occurred. Operation string }
DetailedError is the class based error definition. Each instance has it's own traceable ID. It contains also a Class variable that might be comparable in logic.
func NewDet ¶ added in v0.16.0
func NewDet(c Class, message string) *DetailedError
NewDet creates DetailedError with given 'class' and message 'message'.
func NewDetf ¶ added in v0.16.0
func NewDetf(c Class, format string, args ...interface{}) *DetailedError
NewDetf creates DetailedError instance with provided 'class' with formatted message. DetailedError implements ClassError interface.
func (*DetailedError) Class ¶ added in v0.16.0
func (e *DetailedError) Class() Class
Class implements ClassError.
func (*DetailedError) Error ¶ added in v0.16.0
func (e *DetailedError) Error() string
DetailedError implements error interface.
func (*DetailedError) WithDetail ¶ added in v0.16.0
func (e *DetailedError) WithDetail(detail string) *DetailedError
WithDetail sets error detail.
func (*DetailedError) WithDetailf ¶ added in v0.16.0
func (e *DetailedError) WithDetailf(format string, values ...interface{}) *DetailedError
WithDetailf sets error detail.
type Index ¶ added in v0.16.0
type Index uint16
Index is a 14 bit length lowest level error classification. It defines the most accurate class division. It's maximum size gives 2^14 - 16384 - index combinations for each minor.
func MustNewIndex ¶ added in v0.16.0
MustNewIndex creates new Index for the 'mjr' Major and 'mnr' Minor. Panics if 'mjr' or 'mnr' are not valid.
type Major ¶ added in v0.16.0
type Major uint8
Major is the highest level sub classification. It is of maximum 8 bit size, which gives 2^8 - 256 combinations.
func MustNewMajor ¶ added in v0.16.0
func MustNewMajor() Major
MustNewMajor creates new major error classification. Panics if reached maximum number of possible majors.
type Minor ¶ added in v0.16.0
type Minor uint16
Minor is mid level error sub classification. It is a 10 bit long value, which give 2^10 - 1024 - combinations for each major.
func MustNewMinor ¶ added in v0.16.0
MustNewMinor creates new Minor error classification for provided 'mjr' Major. Panics if the 'mjr' is not valid.
type MultiError ¶ added in v0.16.0
type MultiError []ClassError
MultiError is the slice of errors parsable into a single error.
func (MultiError) Error ¶ added in v0.16.0
func (m MultiError) Error() string
Error implements error interface.
func (MultiError) HasMajor ¶ added in v0.16.0
func (m MultiError) HasMajor(mjr Major) bool
HasMajor checks if provided 'mjr' occurs in given multi error slice.