errors

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const AssertionFailed string = "[ASSERTION FAILED]: "

AssertionFailed is the message that is shown when an assertion fails.

Variables

This section is empty.

Functions

func Error

func Error(err error) string

Error returns the error message of an error.

Parameters:

  • err: The error to get the message of.

Returns:

  • string: The error message of the error.

If the error is nil, the function returns "something went wrong" as the error message.

func GetOrdinalSuffix

func GetOrdinalSuffix(number int) string

GetOrdinalSuffix returns the ordinal suffix for a given integer.

Parameters:

  • number: The integer for which to get the ordinal suffix. Negative numbers are treated as positive.

Returns:

  • string: The ordinal suffix for the number.

Example:

  • GetOrdinalSuffix(1) returns "1st"
  • GetOrdinalSuffix(2) returns "2nd"

func Got

func Got(quote bool, got any) string

Got returns the string representation of a value.

Parameters:

  • quote: A flag indicating whether the value should be quoted.
  • got: The value to get the string representation of.

Returns:

  • string: The string "got <value> instead"

If the value is nil, the function returns "got nothing instead" regardless of the flag.

func Is

func Is[T error](err error) bool

Is is function that checks if an error is of type T.

Parameters:

  • err: The error to check.

Returns:

  • bool: true if the error is of type T, false otherwise (including if the error is nil).

func LimitErrorMsg

func LimitErrorMsg(err error, limit int) error

LimitErrorMsg limits the error message to a certain number of unwraps. It returns the top level error for allowing to print the error message with the limit of unwraps applied.

If the error is nil or the limit is less than 0, the function does nothing.

Parameters:

  • err: The error to limit.
  • limit: The limit of unwraps.

Returns:

  • error: The top level error with the limit of unwraps applied.

func StringOfSlice

func StringOfSlice[T any](quoted bool, elems []T) []string

StringOfSlice returns the string representation of a slice of values.

Parameters:

  • quoted: A flag indicating whether the values should be quoted.
  • elems: The slice of values to get the string representation of.

Returns:

  • []string: The string representation of the slice of values.

Types

type Enumer

type Enumer interface {
	~int

	// String returns the literal name of the enum.
	//
	// Returns:
	//   - string: The name of the enum.
	String() string
}

Enumer is an enum type.

type Err

type Err[T Enumer] struct {
	// Code is the error code.
	Code T

	// Message is the error message.
	Message error

	// Suggestions are the error suggestions.
	Suggestions []string
}

Err is the base error type.

func NewErr

func NewErr[T Enumer](code T, message error) *Err[T]

NewErr creates a new error.

Parameters:

  • code: The error code.
  • message: The error message.

Returns:

  • *Err: The new error. Never returns nil.

func (*Err[T]) AddSuggestion

func (e *Err[T]) AddSuggestion(suggestions ...string) *Err[T]

AddSuggestion adds a suggestion of the error.

Parameters:

  • suggestions: The suggestion of the error.

Returns:

  • *Err: The error. Never returns nil.

Each element in the suggestion is separated by a space but each call to this function adds each suggestion on a new line.

func (*Err[T]) ChangeReason

func (e *Err[T]) ChangeReason(reason error) bool

ChangeReason implements the Unwrapper interface.

func (Err[T]) Error

func (e Err[T]) Error() string

Error implements the error interface.

Message: "{code}: {message}"

func (Err[T]) Unwrap

func (e Err[T]) Unwrap() error

Unwrap implements errors.Unwrap interface.

type ErrAfter

type ErrAfter struct {
	// After is the element that was processed before the error occurred.
	After string

	// Reason is the reason for the error.
	Reason error
}

ErrAfter is an error that is returned when something goes wrong after a certain element in a stream of data.

func NewErrAfter

func NewErrAfter(after string, reason error) *ErrAfter

NewErrAfter creates a new ErrAfter error.

Parameters:

  • after: The element that was processed before the error occurred.
  • reason: The reason for the error.

Returns:

  • *ErrAfter: A pointer to the new ErrAfter error. Never returns nil.

func (*ErrAfter) ChangeReason

func (e *ErrAfter) ChangeReason(reason error) bool

ChangeReason implements the Unwrapper interface.

func (ErrAfter) Error

func (e ErrAfter) Error() string

Error implements the Unwrapper interface.

Message: "after {after}: {reason}".

However, if the reason is nil, the message is "something went wrong after {after}" instead.

func (ErrAfter) Unwrap

func (e ErrAfter) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrAssertFailed

type ErrAssertFailed struct {
	// Msg is the message that is shown when the assertion fails.
	Msg string
}

ErrAssertFailed is the error that is shown when an assertion fails.

func NewErrAssertFailed

func NewErrAssertFailed(msg string) *ErrAssertFailed

NewErrAssertFailed is a constructor for ErrAssertFailed.

Parameters:

  • msg: the message that is shown when the assertion fails.

Returns:

  • *ErrAssertFailed: the error. Never returns nil.

func (ErrAssertFailed) Error

func (e ErrAssertFailed) Error() string

Error implements the error interface.

Message: "[ASSERTION FAILED]: <msg>"

type ErrBefore

type ErrBefore struct {
	// Before is the element that was processed before the error occurred.
	Before string

	// Reason is the reason for the error.
	Reason error
}

ErrBefore is an error that is returned when something goes wrong before a certain element in a stream of data.

func NewErrBefore

func NewErrBefore(before string, reason error) *ErrBefore

NewErrBefore creates a new ErrBefore error.

Parameters:

  • before: The element that was processed before the error occurred.
  • reason: The reason for the error.

Returns:

  • *ErrBefore: A pointer to the new ErrBefore error. Never returns nil.

func (*ErrBefore) ChangeReason

func (e *ErrBefore) ChangeReason(reason error) bool

ChangeReason implements the Unwrapper interface.

func (ErrBefore) Error

func (e ErrBefore) Error() string

Error implements the Unwrapper interface.

Message: "before {before}: {reason}".

However, if the reason is nil, the message is "something went wrong before {before}" instead.

func (*ErrBefore) Unwrap

func (e *ErrBefore) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrInvalidCall

type ErrInvalidCall struct {
	// FnName is the name of the function.
	FnName string

	// Signature is the Signature of the function.
	Signature reflect.Type

	// Reason is the Reason for the failure.
	Reason error
}

ErrInvalidCall represents an error that occurs when a function is not called correctly.

func NewErrInvalidCall

func NewErrInvalidCall(functionName string, function any, reason error) *ErrInvalidCall

NewErrInvalidCall creates a new ErrInvalidCall.

Parameters:

  • functionName: The name of the function.
  • function: The function that failed.
  • reason: The reason for the failure.

Returns:

  • *ErrInvalidCall: A pointer to the new ErrInvalidCall. Never returns nil.

func (*ErrInvalidCall) ChangeReason

func (e *ErrInvalidCall) ChangeReason(reason error) bool

ChangeReason implements the Unwrapper interface.

func (ErrInvalidCall) Error

func (e ErrInvalidCall) Error() string

Error implements the Unwrapper interface.

Message: "call to {function}({signature}) failed: {reason}".

However, if the reason is nil, the message is "call to {function}({signature}) failed" instead.

func (*ErrInvalidCall) Unwrap

func (e *ErrInvalidCall) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrLT

type ErrLT[T cmp.Ordered] struct {
	// Value is the value that caused the error.
	Value T
}

ErrLT represents an error when a value is greater than or equal to a specified value.

func NewErrLT

func NewErrLT[T cmp.Ordered](value T) *ErrLT[T]

NewErrLT creates a new ErrLT error with the specified value.

Parameters:

  • value: The maximum value that is not allowed.

Returns:

  • *ErrLT: A pointer to the newly created ErrLT. Never returns nil.

func (ErrLT[T]) Error

func (e ErrLT[T]) Error() string

Error implements the error interface.

Message: "value must be less than <value>"

type ErrLTE

type ErrLTE[T cmp.Ordered] struct {
	// Value is the value that caused the error.
	Value T
}

ErrLTE represents an error when a value is greater than a specified value.

func NewErrLTE

func NewErrLTE[T cmp.Ordered](value T) *ErrLTE[T]

NewErrLTE creates a new ErrLTE error with the specified value.

Parameters:

  • value: The maximum value that is allowed.

Returns:

  • *ErrLTE: A pointer to the newly created ErrLTE. Never returns nil.

func (ErrLTE[T]) Error

func (e ErrLTE[T]) Error() string

Error implements the error interface.

Message: "value must be less than or equal to <value>"

type ErrOrSol

type ErrOrSol[T any] struct {
	// contains filtered or unexported fields
}

ErrOrSol is a struct that holds a list of errors and a list of solutions.

func (*ErrOrSol[T]) AddAny

func (e *ErrOrSol[T]) AddAny(elem any, level int) bool

AddAny adds an element to the list of errors or solutions if the level is greater or equal to the current level.

Parameters:

  • elem: The element to add.
  • level: The level of the element.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

Behaviors:

  • If an error has been added with a level greater than the current level, the error list is reset and the new level is updated.
  • If a solution has been added with a level greater than the current level, the solution list is reset and the new level is updated.

func (*ErrOrSol[T]) AddErr

func (e *ErrOrSol[T]) AddErr(err error, level int) bool

AddErr adds an error to the list of errors if the level is greater or equal to the current level. Nil errors are ignored.

Parameters:

  • err: The error to add.
  • level: The level of the error.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

Behaviors:

  • If an error has been added with a level greater than the current level, the error list is reset and the new level is updated.
  • If the error is nil, the ignoreErr flag is set to true and the error list is reset.

func (*ErrOrSol[T]) AddSol

func (e *ErrOrSol[T]) AddSol(sol T, level int) bool

AddSol adds a solution to the list of solutions if the level is greater or equal to the current level.

Parameters:

  • sol: The solution to add.
  • level: The level of the solution.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

Behaviors:

  • If a solution has been added with a level greater than the current level, the solution list is reset and the new level is updated.
  • This function sets the ignoreErr flag to true and resets the error list.

func (ErrOrSol[T]) Errors

func (e ErrOrSol[T]) Errors() []error

Errors returns the list of errors. It is a copy of the error list.

Returns:

  • []error: The list of errors.

func (ErrOrSol[T]) HasError

func (e ErrOrSol[T]) HasError() bool

HasError checks if errors are not ignored and if the error list is not empty.

Returns:

  • bool: True if errors are not ignored and the error list is not empty, otherwise false.

func (*ErrOrSol[T]) Reset

func (e *ErrOrSol[T]) Reset()

Reset resets the ErrOrSol struct to allow for reuse.

func (ErrOrSol[T]) Solutions

func (e ErrOrSol[T]) Solutions() []T

Solutions returns the list of solutions. It is a copy of the solution list.

Returns:

  • []T: The list of solutions.

type ErrPanic

type ErrPanic struct {
	// Value is the value that caused the panic.
	Value any
}

ErrPanic represents an error when a panic occurs.

func NewErrPanic

func NewErrPanic(value any) *ErrPanic

NewErrPanic creates a new ErrPanic error.

Parameters:

  • value: The value that caused the panic.

Returns:

  • *ErrPanic: A pointer to the newly created ErrPanic. Never returns nil.

func (ErrPanic) Error

func (e ErrPanic) Error() string

Error implements the error interface.

Message: "panic: {value}"

type ErrUnexpected

type ErrUnexpected[T any] struct {
	// Expecteds is the list of expected values.
	Expecteds []T

	// Got is the unexpected value.
	Got any

	// Quoted is a flag indicating whether values should be quoted.
	Quoted bool

	// Kind is the kind of the unexpected value.
	Kind string
}

ErrUnexpected represents an error when an unexpected value is encountered.

func NewErrUnexpected

func NewErrUnexpected[T any](quoted bool, expecteds []T, kind string, got any) *ErrUnexpected[T]

NewErrUnexpected creates a new ErrUnexpected error with the specified values.

Parameters:

  • quoted: A flag indicating whether values should be quoted.
  • expecteds: The list of expected values.
  • kind: The kind of the unexpected value.
  • got: The unexpected value.

Returns:

  • *ErrUnexpected: A pointer to the newly created ErrUnexpected. Never returns nil.

func (ErrUnexpected[T]) Error

func (e ErrUnexpected[T]) Error() string

Error implements the error interface.

Message: "expected <expecteds> <kind>, got <got> instead"

type ErrUnexpectedType

type ErrUnexpectedType[T any] struct {
	// Elem is the element that caused the error.
	Elem T

	// Kind is the category of the type that was expected.
	Kind string
}

ErrUnexpectedType represents an error when a value has an invalid type.

func NewErrUnexpectedType

func NewErrUnexpectedType[T any](kind string, elem T) *ErrUnexpectedType[T]

NewErrUnexpectedType creates a new ErrUnexpectedType error.

Parameters:

  • kind: The name of the type that was expected.
  • elem: The element that caused the error.

Returns:

  • *ErrUnexpectedType: A pointer to the newly created ErrUnexpectedType. Never returns nil.

func (ErrUnexpectedType[T]) Error

func (e ErrUnexpectedType[T]) Error() string

Error implements the error interface.

Message: "type <type> is not a valid <kind> type"

type ErrUnexpectedValue

type ErrUnexpectedValue[T fmt.Stringer] struct {
	// Elem is the element that caused the error.
	Elem T

	// Kind is the category of the type that was expected.
	Kind string
}

ErrUnexpectedValue represents an error when an unexpected value is encountered. This is mostly used in the 'default' case of switch statements.

func NewErrUnexpectedValue

func NewErrUnexpectedValue[T fmt.Stringer](elem T, kind string) *ErrUnexpectedValue[T]

NewErrUnexpectedValue creates a new ErrUnexpectedValue error.

Parameters:

  • elem: The element that caused the error.
  • kind: The name of the type that was expected.

Returns:

  • *ErrUnexpectedValue: A pointer to the newly created ErrUnexpectedValue. Never returns nil.

func (ErrUnexpectedValue[T]) Error

func (e ErrUnexpectedValue[T]) Error() string

Error implements the error interface.

Message: "<type> (<elem>) is not a supported <kind> type"

type ErrWhile

type ErrWhile struct {
	// Operation is the operation that was being performed.
	Operation string

	// Reason is the reason for the error.
	Reason error
}

ErrWhile represents an error that occurs while performing an operation.

func NewErrWhile

func NewErrWhile(operation string, reason error) *ErrWhile

NewErrWhile creates a new ErrWhile error.

Parameters:

  • operation: The operation that was being performed.
  • reason: The reason for the error.

Returns:

  • *ErrWhile: A pointer to the newly created ErrWhile.

func (*ErrWhile) ChangeReason

func (e *ErrWhile) ChangeReason(reason error) bool

ChangeReason implements the Unwrapper interface.

func (ErrWhile) Error

func (e ErrWhile) Error() string

Error implements the Unwrapper interface.

Message: "error while {operation}: {reason}"

However, if the reason is nil, the message is "an error occurred while {operation}" instead.

func (ErrWhile) Unwrap

func (e ErrWhile) Unwrap() error

Unwrap implements the Unwrapper interface.

type Unwrapper

type Unwrapper interface {
	// Unwrap returns the error that this error wraps.
	//
	// Returns:
	//   - error: The error that this error wraps.
	Unwrap() error

	// ChangeReason changes the reason of the error.
	//
	// Parameters:
	//   - reason: The new reason of the error.
	//
	// Returns:
	// 	- bool: True if the receiver is not nil, false otherwise.
	ChangeReason(reason error) bool
}

Unwrapper is an interface that defines a method to unwrap an error.

Jump to

Keyboard shortcuts

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