errors

package
v0.1.21 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NilValue is an error that is returned when a value is nil.
	NilValue error

	// NilReceiver is an error that is returned when a receiver is nil.
	NilReceiver error
)

Functions

func Fix added in v0.1.17

func Fix(name string, obj Fixer, allow_nil bool) error

Fix fixes the object.

Parameters:

  • name: The name of the object.
  • obj: The object to fix.
  • allow_nil: Whether to allow the object to be nil.

Returns:

  • error: An error that occurred while fixing the object.

func GetOrdinalSuffix added in v0.1.6

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 LimitErrorMsg added in v0.1.11

func LimitErrorMsg(err error, limit int) error

LimitErrorMsg is a function that limits the number of errors in an error chain.

Parameters:

  • err: The error to limit.
  • limit: The maximum number of errors to limit.

Returns:

  • error: The limited error.

If the error is nil or the limit is less than or equal to 0, the function returns nil.

Types

type ErrAt added in v0.1.17

type ErrAt struct {
	// Idx is the index of the error.
	Idx int

	// IdxType is the type of the index.
	IdxType string

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

ErrAt represents an error that occurs at a specific index.

func NewErrAt added in v0.1.17

func NewErrAt(idx int, idx_type string, reason error) *ErrAt

NewErrAt creates a new ErrAt error.

Parameters:

  • idx: The index of the error.
  • idx_type: The type of the index.
  • reason: The reason for the error.

Returns:

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

Empty name will default to "index".

func (*ErrAt) ChangeReason added in v0.1.17

func (e *ErrAt) ChangeReason(new_reason error)

ChangeReason implements the errors.Unwrapper interface.

func (ErrAt) Error added in v0.1.17

func (e ErrAt) Error() string

Error implements the error interface.

Message:

  • "something went wrong at the <ordinal> <idx_type>" if Reason is nil
  • "<ordinal> <idx_type> is invalid: <reason>" if Reason is not nil

func (ErrAt) Unwrap added in v0.1.17

func (e ErrAt) Unwrap() error

Unwrap implements the errors.Unwrapper interface.

type ErrEmpty

type ErrEmpty struct {
	// Type is the type of the empty value.
	Type any
}

ErrEmpty represents an error when a value is empty.

func NewErrEmpty

func NewErrEmpty(var_type any) *ErrEmpty

NewErrEmpty creates a new ErrEmpty error.

Parameters:

  • var_type: The type of the empty value.

Returns:

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

func (ErrEmpty) Error

func (e ErrEmpty) Error() string

Error implements the error interface.

Message: "{{ .Type }} must not be empty"

type ErrFix added in v0.1.17

type ErrFix struct {
	// Name is the name of the object.
	Name string

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

ErrFix is an error that is returned when an object cannot be fixed.

func NewErrFix added in v0.1.17

func NewErrFix(name string, reason error) *ErrFix

NewErrFix creates a new ErrFix error.

Parameters:

  • name: the name of the object.
  • reason: the reason for the error.

Returns:

  • *ErrFix: the new error. Never returns nil.

func (*ErrFix) ChangeReason added in v0.1.17

func (e *ErrFix) ChangeReason(new_reason error)

ChangeReason changes the reason for the error.

Parameters:

  • new_reason: the new reason for the error.

func (ErrFix) Error added in v0.1.17

func (e ErrFix) Error() string

Error implements the error interface.

Message:

"failed to fix <name>: <reason>"

func (ErrFix) Unwrap added in v0.1.17

func (e ErrFix) Unwrap() error

Unwrap implements the errors.Unwrap interface.

type ErrGT added in v0.1.2

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

ErrGT represents an error when a value is less than or equal to a specified value.

func NewErrGT added in v0.1.2

func NewErrGT[T cmp.Ordered](value T) *ErrGT[T]

NewErrGT creates a new ErrGT error with the specified value.

Parameters:

  • value: The minimum value that is not allowed.

Returns:

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

func (ErrGT[T]) Error added in v0.1.2

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

Error implements the error interface.

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

type ErrGTE added in v0.1.2

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

ErrGTE represents an error when a value is less than a specified value.

func NewErrGTE added in v0.1.2

func NewErrGTE[T cmp.Ordered](value T) *ErrGTE[T]

NewErrGTE creates a new ErrGTE error with the specified value.

Parameters:

  • value: The minimum value that is allowed.

Returns:

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

func (ErrGTE[T]) Error added in v0.1.2

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

Error implements the error interface.

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

type ErrInvalidParameter

type ErrInvalidParameter struct {
	// Parameter is the name of the invalid parameter.
	Parameter string

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

ErrInvalidParameter is an error that is returned when a parameter is invalid.

func NewErrInvalidParameter

func NewErrInvalidParameter(parameter string, reason error) *ErrInvalidParameter

NewErrInvalidParameter creates a new ErrInvalidParameter error.

Parameters:

  • parameter: the name of the invalid parameter.
  • reason: the reason for the error.

Returns:

  • *ErrInvalidParameter: the new error. Never returns nil.

func NewErrNilParameter

func NewErrNilParameter(parameter string) *ErrInvalidParameter

NewErrNilParameter creates a new ErrInvalidParameter error.

Parameters:

  • parameter: the name of the invalid parameter.

Returns:

  • *ErrInvalidParameter: the new error. Never returns nil.

func (*ErrInvalidParameter) ChangeReason

func (e *ErrInvalidParameter) ChangeReason(new_reason error)

ChangeReason changes the reason for the error.

Parameters:

  • new_reason: the new reason for the error.

func (ErrInvalidParameter) Error

func (e ErrInvalidParameter) Error() string

Error implements the error interface.

Message:

"parameter <parameter> is invalid: <reason>"

func (ErrInvalidParameter) Unwrap

func (e ErrInvalidParameter) Unwrap() error

Unwrap implements the errors.Unwrap interface.

type ErrInvalidUsage added in v0.1.2

type ErrInvalidUsage struct {
	// Reason is the reason for the invalid usage.
	Reason error

	// Usage is the usage of the function.
	Usage string
}

ErrInvalidUsage represents an error that occurs when a function is used incorrectly.

func NewErrInvalidUsage added in v0.1.2

func NewErrInvalidUsage(reason error, usage string) *ErrInvalidUsage

NewErrInvalidUsage creates a new ErrInvalidUsage error.

Parameters:

  • reason: The reason for the invalid usage.
  • usage: The usage of the function.

Returns:

  • *ErrInvalidUsage: A pointer to the new ErrInvalidUsage error.

func (*ErrInvalidUsage) ChangeReason added in v0.1.2

func (e *ErrInvalidUsage) ChangeReason(new_reason error)

ChangeReason implements the Unwrapper interface.

func (ErrInvalidUsage) Error added in v0.1.2

func (e ErrInvalidUsage) Error() string

Error is a method of the Unwrapper interface.

Message:

"{reason}. {usage}"

However, if the reason is nil, the message is "invalid usage. {usage}" instead.

If the usage is empty, no usage is added to the message.

func (ErrInvalidUsage) Unwrap added in v0.1.2

func (e ErrInvalidUsage) Unwrap() error

Unwrap implements the errors.Unwrap interface.

type ErrOutOfBounds added in v0.1.17

type ErrOutOfBounds struct {
	// LowerBound is the lower bound of the value.
	LowerBound int

	// UpperBound is the upper bound of the value.
	UpperBound int

	// LowerInclusive is true if the lower bound is inclusive.
	LowerInclusive bool

	// UpperInclusive is true if the upper bound is inclusive.
	UpperInclusive bool

	// Value is the value that is out of bounds.
	Value int
}

ErrOutOfBounds represents an error when a value is out of bounds.

func NewErrOutOfBounds added in v0.1.17

func NewErrOutOfBounds(value, lowerBound, upperBound int) *ErrOutOfBounds

NewErrOutOfBounds creates a new ErrOutOfBounds error.

Parameters:

  • value: The value that is out of bounds.
  • lowerBound: The lower bound of the value.
  • upperBound: The upper bound of the value.

Returns:

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

By default, the lower bound is inclusive and the upper bound is exclusive.

func (ErrOutOfBounds) Error added in v0.1.17

func (e ErrOutOfBounds) Error() string

Error implements the error interface.

Message: "value ( <value> ) not in range <lower_bound> , <upper_bound>"

func (*ErrOutOfBounds) WithLowerBound added in v0.1.17

func (e *ErrOutOfBounds) WithLowerBound(is_inclusive bool) *ErrOutOfBounds

WithLowerBound sets the lower bound of the value.

Parameters:

  • is_inclusive: True if the lower bound is inclusive. False if the lower bound is exclusive.

Returns:

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

Only when the receiver is nil, this function returns nil.

func (*ErrOutOfBounds) WithUpperBound added in v0.1.17

func (e *ErrOutOfBounds) WithUpperBound(is_inclusive bool) *ErrOutOfBounds

WithUpperBound sets the upper bound of the value.

Parameters:

  • is_inclusive: True if the upper bound is inclusive. False if the upper bound is exclusive.

Returns:

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

Only when the receiver is nil, this function returns nil.

type ErrValue added in v0.1.21

type ErrValue struct {
	// Kind is the name of the thing that was expected.
	Kind string

	// Expected is the value that was expected.
	Expected any

	// Got is the value that was received.
	Got any

	// ShouldQuote is true if the expected and got values should be quoted,
	// false otherwise.
	ShouldQuote bool
}

ErrValue represents an error when a value is not expected.

func NewErrValue added in v0.1.21

func NewErrValue(kind string, expected, got any, should_quote bool) *ErrValue

NewErrValue creates a new ErrValue error.

Parameters:

  • kind: The name of the thing that was expected.
  • expected: The value that was expected.
  • got: The value that was received.
  • should_quote: True if the expected and got values should be quoted, false otherwise.

Returns:

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

func NewErrValues added in v0.1.21

func NewErrValues[T any](kind string, expected []T, got any, should_quote bool) *ErrValue

NewErrValues creates a new ErrValues error.

Parameters:

  • kind: The name of the thing that was expected.
  • expected: The values that were expected.
  • got: The value that was received.
  • should_quote: True if the expected and got values should be quoted, false otherwise.

Returns:

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

func (ErrValue) Error added in v0.1.21

func (e ErrValue) Error() string

Error implements the error interface.

Message:

"expected <kind> to be <expected>, got <got> instead"

type ErrValues added in v0.1.21

type ErrValues[T any] struct {
	// Kind is the name of the thing that was expected.
	Kind string

	// Expecteds is the values that were expected.
	Expecteds []T

	// Got is the value that was received.
	Got any

	// ShouldQuote is true if the expected and got values should be quoted,
	// false otherwise.
	ShouldQuote bool
}

ErrValues represents an error when multiple value are not expected.

func (ErrValues[T]) Error added in v0.1.21

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

Error implements the error interface.

Message:

"expected <kind> to be <expected>, got <got> instead"

type Fixer added in v0.1.17

type Fixer interface {
	// Fix fixes the object.
	//
	// Returns:
	//   - error: An error that occurred while fixing the object.
	Fix() error
}

Fixer is defines the behavior of an object that can be fixed. This must not have a non-pointer receiver.

type Unwrapper added in v0.1.11

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.
	ChangeReason(new_reason error)
}

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
JackTT - Gopher 🇻🇳