Errors

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package Errors provides advanced error handling mechanisms.

Package errors provides a custom error type for out-of-bound errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckFunc added in v0.2.9

func CheckFunc[O any, I any](f func(I) (O, error), param I) O

CheckFunc is a generic function that accepts a function and a builder. It executes the provided function with the target value of the builder and returns a new builder with the result of the function as its target value.

If the provided function returns an error, CheckFunc triggers a panic with the error. The panic can be recovered using the RecoverFromPanic function.

This function is useful for chaining operations on a value while handling errors in a consistent manner.

Parameters:

  • f: The function to execute. It should accept a value of type I and return a value of type O and an error.
  • b: The builder whose target value should be passed to the function.

Returns:

  • *builder[O]: A new builder with the result of the function as its target value.

Example:

 CheckFunc(func(n int) (int, error) {
	if n <= 0 {
		return 0, NewErrInvalidParameter("n", fmt.Errorf("value (%d) must be positive", n))
	}

	return 60 / n, nil
 }, On(42))

func ErrorOf added in v0.2.9

func ErrorOf[T any](f func(T), param T) (err error)

ErrorOf is a generic function that accepts a function and a parameter of any type. It executes the provided function with the given parameter and returns any error that might occur during the execution of the function.

The function uses a deferred function to recover from any panics that might occur during the execution of the provided function. If the recovered value is an error, it sets the returned error to that error. If the recovered value is not an error, it panics again with a new error that contains the recovered value.

This function is useful for checking if a function can handle certain inputs without causing a panic.

Parameters:

  • f: The function to execute.
  • param: The parameter to pass to the function.

Returns:

  • err: The error that occurred during the execution of the function, or nil if no error occurred.

Example:

	err := ErrorOf(func(n int) { panic("something went wrong") }, 42)
	if err != nil {
    	fmt.Println(err)
	}

func PropagatePanic added in v0.2.9

func PropagatePanic[T interface{ WithReason(error) T }](err T)

PropagatePanic is a function that recovers from a panic and propagates it with the provided error. If the recovered value is an error, it panics with the provided error and the recovered error as its reason. If the recovered value is not an error, it panics with a new error that contains the recovered value.

This function should be called using the defer statement to recover from panics.

Parameters:

  • err: The error to propagate with the recovered value.

func RecoverFromPanic added in v0.2.9

func RecoverFromPanic(err *error)

RecoverFromPanic is a function that recovers from a panic and sets the provided error pointer to the recovered value. If the recovered value is an error, it sets the error pointer to that error. Otherwise, it creates a new error with the recovered value and sets the error pointer to that.

This function should be called using the defer statement to recover from panics.

Parameters:

  • err: The error pointer to set.

Example:

func MyFunction(n int) (result int, err error) {
    defer RecoverFromPanic(&err)
    // ...
    panic("something went wrong")
}

Types

type ErrCallFailed added in v0.2.9

type ErrCallFailed struct {
	// contains filtered or unexported fields
}

ErrCallFailed represents an error that occurs when a function call fails.

func NewErrCallFailed added in v0.2.9

func NewErrCallFailed(functionName string, function any) *ErrCallFailed

NewCallFailed creates a new ErrCallFailed. If the reason is not provided (nil), the reason is set to "an error occurred while calling the function" by default.

Parameters:

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

Returns:

  • error: A pointer to the new ErrCallFailed.

func (*ErrCallFailed) Error added in v0.2.9

func (e *ErrCallFailed) Error() string

Error generates a string representation of an ErrCallFailed, including the function name, signature, and the reason for the failure.

Returns:

  • string: The error message.

func (*ErrCallFailed) Unwrap added in v0.2.9

func (e *ErrCallFailed) Unwrap() error

Unwrap returns the underlying error that caused the ErrCallFailed. It is used for error unwrapping.

Returns:

  • error: The reason for the failure.

func (*ErrCallFailed) WithReason added in v0.2.9

func (e *ErrCallFailed) WithReason(reason error) *ErrCallFailed

WithReason sets the reason for the failure. If the reason is not provided (nil), the reason is set to "an error occurred while calling the function" by default.

Parameters:

  • reason: The reason for the failure.

Returns:

  • *ErrCallFailed: The error instance for chaining.

type ErrInvalidParameter

type ErrInvalidParameter struct {
	// contains filtered or unexported fields
}

ErrInvalidParameter represents an error when a parameter is invalid.

func NewErrInvalidParameter

func NewErrInvalidParameter(parameter string) *ErrInvalidParameter

NewErrInvalidParameter creates a new ErrInvalidParameter error. If the reason is not provided (nil), the reason is set to "parameter is invalid" by default.

Parameters:

  • parameter: The name of the parameter.
  • reason: The reason for the invalidity.

Returns:

  • error: A pointer to the newly created ErrInvalidParameter.

func (*ErrInvalidParameter) Error

func (e *ErrInvalidParameter) Error() string

Error generates the error message for the ErrInvalidParameter error, including the parameter name and the reason for its invalidity.

Returns:

  • string: The error message.

func (*ErrInvalidParameter) Unwrap

func (e *ErrInvalidParameter) Unwrap() error

Unwrap returns the reason for the invalidity of the parameter. It is used for error unwrapping.

Returns:

  • error: The reason for the invalidity of the parameter.

func (*ErrInvalidParameter) WithReason

func (e *ErrInvalidParameter) WithReason(reason error) *ErrInvalidParameter

WithReason sets the reason for the invalidity of the parameter. If the reason is not provided (nil), the reason is set to "parameter is invalid" by default.

type ErrOutOfBound

type ErrOutOfBound struct {
	// contains filtered or unexported fields
}

ErrOutOfBound represents an error when a value is out of a specified range.

func NewErrOutOfBound

func NewErrOutOfBound(value int, lowerBound, upperBound int) *ErrOutOfBound

NewOutOfBound creates a new ErrOutOfBound error. If no inclusivity flags are provided, the lower bound is inclusive and the upper bound is exclusive.

Parameters:

  • lowerBound, upperbound: The lower and upper bounds of the range, respectively.
  • value: The value that caused the error.

Returns:

  • error: A pointer to the newly created ErrOutOfBound.

func (*ErrOutOfBound) Error

func (e *ErrOutOfBound) Error() string

Error generates the error message for the ErrOutOfBound error, including the value, the range, and whether the bounds are inclusive. The message includes the range, and whether the bounds are inclusive.

Returns:

  • string: The error message.

func (*ErrOutOfBound) WithLowerBound

func (e *ErrOutOfBound) WithLowerBound(isInclusive bool) *ErrOutOfBound

WithLowerBound sets the inclusivity of the lower bound.

Parameters:

  • isInclusive: A boolean indicating whether the lower bound is inclusive.

Returns:

  • *ErrOutOfBound: The error instance for chaining.

func (*ErrOutOfBound) WithUpperBound

func (e *ErrOutOfBound) WithUpperBound(isInclusive bool) *ErrOutOfBound

WithUpperBound sets the inclusivity of the upper bound.

Parameters:

  • isInclusive: A boolean indicating whether the upper bound is inclusive.

Returns:

  • *ErrOutOfBound: The error instance for chaining.

Jump to

Keyboard shortcuts

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