Documentation
¶
Index ¶
Constants ¶
const ( // ErrCodeUnknownError is the errorex code for when the errorex code is unknown ErrCodeUnknownError = "errorex.000" // ErrCodeNotRegistered is the errorex code for when the errorex code is not registered ErrCodeNotRegistered = "errorex.001" // ErrCodeAlreadyRegistered is the errorex code for when the errorex code is already registered ErrCodeAlreadyRegistered = "errorex.002" // ErrDetailTypeMismatch is the errorex code for when the errorex detail type does not match the registered type ErrDetailTypeMismatch = "errorex.003" )
Variables ¶
This section is empty.
Functions ¶
func RegisterErrorCode ¶
RegisterErrorCode registers errorex codes to prevent repeats
Types ¶
type BaseErrorConverter ¶
type BaseErrorConverter struct {
// contains filtered or unexported fields
}
BaseErrorConverter provides a basic implementation of the ErrorConverter interface, holding a reference to the next handler in the chain.
func (*BaseErrorConverter) ConvertError ¶
func (b *BaseErrorConverter) ConvertError(err error) EX
ConvertError in BaseErrorConverter should be overridden by concrete handlers. This default implementation delegates the conversion task to the next handler.
func (*BaseErrorConverter) SetNext ¶
func (b *BaseErrorConverter) SetNext(next ErrorConverter)
SetNext sets the next handler in the chain.
type EX ¶
type EX interface {
error
// Code is the errorex code
Code() string
// Detail returns the detail of the error
Detail() any
}
EX is a custom errorex type with additional information
type ErrorConstructor ¶
ErrorConstructor is a function that creates an errorEX
type ErrorConverter ¶
type ErrorConverter interface {
// ConvertError tries to convert an error into another error compatible with errorex.EX.
// If it cannot handle the conversion, it attempts to delegate the task to the next handler in the chain.
// It returns an error which will be nil if conversion is not possible by any handler.
ConvertError(err error) EX
// SetNext sets the next handler in the chain.
SetNext(next ErrorConverter)
}
ErrorConverter defines the interface for handlers in the chain of responsibility that attempt to convert an error into an error compatible with errorex.EX. If the handler cannot convert the error, it delegates the task to the next handler in the chain. A large number of handlers in the chain of responsibilities can cause delays in the process, so I do not recommend having a single chain for the entire application. Instead, have multiple chains with handlers that make sense in their contexts.
func BuildErrorConverterChain ¶ added in v1.3.0
func BuildErrorConverterChain(converters ...ErrorConverter) ErrorConverter
BuildErrorConverterChain creates a chain of error converters. The chain starts ExErrorConverter, then the provided converters, and ends with UnknownErrorConverter.
func NewEXErrorConverter ¶ added in v1.2.0
func NewEXErrorConverter(next ErrorConverter) ErrorConverter
NewEXErrorConverter creates a new exErrorConverter this converter will check if the error passed implements EX, and if so, returns the error itself, otherwise it attempts to delegate the conversion to the next handler in the chain. This converter should be used as the first handler in the chain.
func NewUnknownErrorConverter ¶
func NewUnknownErrorConverter() ErrorConverter
NewUnknownErrorConverter creates a new unknownErrorConverter this converter will convert any error into an unknown error with the message of the error as the detail. This converter should be used as the last handler in the chain.
type ErrorEXDetail ¶
type ErrorEXDetail struct {
Code string `json:"code"`
}
ErrorEXDetail is the type of the detail of an errorex
type ErrorEXDetailTypeMismatch ¶
type ErrorEXDetailTypeMismatch struct {
ExpectedType string `json:"expectedType"`
ActualType string `json:"actualType"`
}
ErrorEXDetailTypeMismatch is the type of the detail of an errorex
type UnknownErrorDetail ¶
type UnknownErrorDetail struct {
Detail string `json:"detail"`
}
UnknownErrorDetail is the type of the detail of an unknown errorex