Documentation
¶
Index ¶
- func DisplayError(w io.Writer, to_display error) error
- func Is[T ErrorCoder](err error, code T) bool
- func Merge(outer, inner *internal.Info) *internal.Info
- func MergeErrors(outer, inner error) error
- func Panic(w io.Writer, to_display error)
- func Value[C ErrorCoder, T any](e *Err, key string) (T, error)
- type Err
- func As(err error) (*Err, bool)
- func AsWithCode[T ErrorCoder](err error, code T) (*Err, bool)
- func New[C ErrorCoder](code C, message string) *Err
- func NewErrAfter(before string, reason error) *Err
- func NewErrAt(at string, reason error) *Err
- func NewErrBefore(after string, reason error) *Err
- func NewErrInvalidParameter(frame, message string) *Err
- func NewErrInvalidUsage(frame, message, usage string) *Err
- func NewErrNilParameter(frame, parameter string) *Err
- func NewErrNilReceiver(frame string) *Err
- func NewErrNoSuchKey(frame, key string) *Err
- func NewFromError[C ErrorCoder](code C, err error) *Err
- func NewWithSeverity[C ErrorCoder](severity SeverityLevel, code C, message string) *Err
- func (e *Err) AddContext(key string, value any)
- func (e *Err) AddFrame(frame string)
- func (e *Err) AddSuggestion(suggestion string)
- func (e *Err) ChangeSeverity(new_severity SeverityLevel)
- func (e Err) Error() string
- func (e *Err) IsNil() bool
- func (e *Err) SetInner(inner error)
- func (e Err) Value(key string) (any, bool)
- type ErrorCode
- type ErrorCoder
- type Pointer
- type SeverityLevel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisplayError ¶ added in v0.1.3
DisplayError displays the complete error to the writer.
Parameters:
- w: The writer to write to.
- to_display: The error to display.
Returns:
- error: The error that occurred while displaying the error.
func Is ¶
func Is[T ErrorCoder](err error, code T) bool
Is is function that checks if an error is of type T.
Parameters:
- err: The error to check.
- code: The error code to check.
Returns:
- bool: true if the error is of type T, false otherwise (including if the error is nil).
func Merge ¶ added in v0.1.4
Merge merges the inner Info into the outer Info.
Parameters:
- outer: The outer Info to merge.
- inner: The inner Info to merge.
Returns:
- *Info: A pointer to the new Info. Never returns nil.
Note:
- The other Info is the inner info of the current Info and, as such, when conflicts occur, the outer Info takes precedence.
func MergeErrors ¶ added in v0.1.4
func Panic ¶ added in v0.1.4
Panic is like DisplayError but panics afterwards.
Parameters:
- w: The writer to write to.
- to_display: The error to display.
func Value ¶
func Value[C ErrorCoder, T any](e *Err, key string) (T, error)
Value is a function that returns the value of the context with the given key.
Parameters:
- e: The error to get the value from.
- key: The key of the context.
Returns:
- T: The value of the context with the given key.
- error: The error that occurred while getting the value.
Types ¶
type Err ¶ added in v0.1.4
type Err struct { // Severity is the severity level of the error. Severity SeverityLevel // Code is the error code. Code ErrorCoder // Message is the error message. Message string *internal.Info }
Err represents a generalized error.
func As ¶
As returns the error if it is of type T.
Parameters:
- err: The error to check.
- code: The error code to check.
Returns:
- *Err: The error if it is of type T, nil otherwise.
- bool: true if the error is of type T, false otherwise (including if the error is nil).
func AsWithCode ¶
func AsWithCode[T ErrorCoder](err error, code T) (*Err, bool)
AsWithCode returns the error if it is of type T.
Parameters:
- err: The error to check.
- code: The error code to check.
Returns:
- *Err: The error if it is of type T, nil otherwise.
- bool: true if the error is of type T, false otherwise (including if the error is nil).
func New ¶ added in v0.1.4
func New[C ErrorCoder](code C, message string) *Err
New creates a new error.
Parameters:
- code: The error code.
- message: The error message.
Returns:
- *Err: A pointer to the new error. Never returns nil.
func NewErrAfter ¶
NewErrAfter creates a new error.Err error.
Parameters:
- before: The operation after which the error occurred.
- reason: The reason for the error.
Returns:
- *error.Err: The new error. Never returns nil.
func NewErrAt ¶
NewErrAt creates a new error.Err error.
Parameters:
- at: The operation at which the error occurred.
- reason: The reason for the error.
Returns:
- *error.Err: The new error. Never returns nil.
func NewErrBefore ¶
NewErrBefore creates a new error.Err error.
Parameters:
- after: The operation before which the error occurred.
- reason: The reason for the error.
Returns:
- *error.Err: The new error. Never returns nil.
func NewErrInvalidParameter ¶
NewErrInvalidParameter creates a new error.Err error.
Parameters:
- frame: The frame of the error.
- message: The message of the error.
Returns:
- *error.Err: The new error. Never returns nil.
This function is mostly useless since it just wraps BadParameter.
func NewErrInvalidUsage ¶
NewErrInvalidUsage creates a new error.Err error.
Parameters:
- frame: The frame of the error.
- message: The message of the error.
- usage: The usage/suggestion to solve the problem.
Returns:
- *error.Err: The new error. Never returns nil.
func NewErrNilParameter ¶
NewErrNilParameter creates a new error.Err error.
Parameters:
- frame: The frame of the error.
- parameter: the name of the invalid parameter.
Returns:
- *error.Err: The new error. Never returns nil.
func NewErrNilReceiver ¶ added in v0.1.4
NewErrNilReceiver creates a new error.Err error with the code OperationFail.
Parameters:
- frame: The frame of the error.
Returns:
- *error.Err: The new error. Never returns nil.
func NewErrNoSuchKey ¶
NewErrNoSuchKey creates a new error.Err error.
Parameters:
- frame: The frame of the error.
- key: The key that does not exist.
Returns:
- *error.Err: The new error. Never returns nil.
func NewFromError ¶ added in v0.1.4
func NewFromError[C ErrorCoder](code C, err error) *Err
NewFromError creates a new error from an error.
Parameters:
- code: The error code.
- err: The error to wrap.
Returns:
- *Err: A pointer to the new error. Never returns nil.
func NewWithSeverity ¶ added in v0.1.4
func NewWithSeverity[C ErrorCoder](severity SeverityLevel, code C, message string) *Err
NewWithSeverity creates a new error.
Parameters:
- severity: The severity level of the error.
- code: The error code.
- message: The error message.
Returns:
- *Err: A pointer to the new error. Never returns nil.
func (*Err) AddContext ¶ added in v0.1.4
AddContext adds a context to the error. Does nothing if the receiver is nil.
Parameters:
- key: The key of the context.
- value: The value of the context.
func (*Err) AddFrame ¶ added in v0.1.4
AddFrame prepends a frame to the stack trace. Does nothing if the receiver is nil or the trace is empty.
Parameters:
- frame: The frame to add.
If prefix is empty, the call is used as the frame. Otherwise a dot is added between the prefix and the call.
func (*Err) AddSuggestion ¶ added in v0.1.4
AddSuggestion adds a suggestion to the error. Does nothing if the receiver is nil.
Parameters:
- suggestion: The suggestion to add.
func (*Err) ChangeSeverity ¶ added in v0.1.4
func (e *Err) ChangeSeverity(new_severity SeverityLevel)
ChangeSeverity changes the severity level of the error. Does nothing if the receiver is nil.
Parameters:
- new_severity: The new severity level of the error.
type ErrorCode ¶
type ErrorCode int
ErrorCode is the type of the error code.
const ( // BadParameter occurs when a parameter is invalid or is not // valid for some reason. For example, a nil pointer when nil // pointers are not allowed. BadParameter ErrorCode = iota // InvalidUsage occurs when users call a function without // proper setups or preconditions. InvalidUsage // NoSuchKey occurs when a context key is requested but does // not exist. NoSuchKey // OperationFail occurs when an operation cannot be completed // due to an internal error. OperationFail )
type ErrorCoder ¶ added in v0.1.4
type ErrorCoder interface { // Int returns the integer value of the error code. // // Returns: // - int: The integer value of the error code. Int() int fmt.Stringer }
ErrorCoder is an interface that all error codes must implement.
type Pointer ¶ added in v0.1.4
type Pointer interface { // IsNil checks whether the pointer is nil. // // Returns: // - bool: True if the pointer is nil, false otherwise. IsNil() bool }
Pointer is an interface that checks whether a pointer is nil.
type SeverityLevel ¶ added in v0.1.4
type SeverityLevel int
SeverityLevel represents the severity level of an error.
const ( // INFO is the severity level for informational messages. // (i.e., errors that are not critical nor fatal) // // Mostly used for message passing. INFO SeverityLevel = iota // WARNING is the severity level for warning messages. // (i.e., errors that are not critical nor fatal, yet worthy of attention). WARNING // ERROR is the severity level for error messages. // (i.e., the standard error level). ERROR // FATAL is the severity level for fatal errors. // (i.e., the highest severity level). // // These are usually panic-level of errors. FATAL )
func (SeverityLevel) String ¶ added in v0.1.4
func (i SeverityLevel) String() string