Documentation
¶
Overview ¶
Package errors provides functional utilities for working with Go errors. It includes functions for error creation, transformation, and type conversion that integrate well with functional programming patterns.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Identity = F.Identity[error]
Identity is the identity function specialized for error types. It returns the error unchanged, useful in functional composition where an error needs to be passed through without modification.
Example:
err := errors.New("something went wrong")
same := Identity(err) // returns the same error
Functions ¶
func As ¶
As tries to extract an error of the desired type from the given error. It returns an Option containing the extracted error if successful, or None if the error cannot be converted to the target type.
This function wraps Go's standard errors.As in a functional style, making it composable with other functional operations.
Example:
type MyError struct{ msg string }
func (e *MyError) Error() string { return e.msg }
rootErr := &MyError{msg: "custom error"}
wrappedErr := fmt.Errorf("wrapped: %w", rootErr)
// Extract MyError from the wrapped error
extractMyError := As[*MyError]()
result := extractMyError(wrappedErr)
// result is Some(*MyError) containing the original error
// Try to extract a different error type
extractOther := As[*os.PathError]()
result2 := extractOther(wrappedErr)
// result2 is None since wrappedErr doesn't contain *os.PathError
func OnError ¶
func OnError(msg string, args ...any) endomorphism.Endomorphism[error]
OnError generates a unary function that produces a formatted error with error wrapping. The argument to that function is the root cause of the error and the message will be augmented with a format string containing %w for proper error wrapping.
This is useful for adding context to errors while preserving the error chain, allowing errors.Is and errors.As to work correctly.
Example:
wrapError := OnError("failed to load configuration from %s", "config.json")
rootErr := errors.New("file not found")
wrappedErr := wrapError(rootErr)
// returns error: "failed to load configuration from config.json, Caused By: file not found"
// errors.Is(wrappedErr, rootErr) returns true
func OnNone ¶
OnNone generates a nullary function that produces a formatted error. This is useful when you need to create an error lazily, such as when handling the None case in an Option type.
Example:
getError := OnNone("value not found")
err := getError() // returns error: "value not found"
getErrorWithArgs := OnNone("failed to load %s", "config.json")
err2 := getErrorWithArgs() // returns error: "failed to load config.json"
func OnSome ¶
OnSome generates a unary function that produces a formatted error. The function takes a value of type T and includes it in the error message. If no additional args are provided, the value is used as the only format argument. If additional args are provided, the value becomes the first format argument.
This is useful when you need to create an error that includes information about a value, such as when handling the Some case in an Option type.
Example:
// Without additional args - value is the only format argument
makeError := OnSome[int]("invalid value: %d")
err := makeError(42) // returns error: "invalid value: 42"
// With additional args - value is the first format argument
makeError2 := OnSome[string]("failed to process %s in file %s", "data.txt")
err2 := makeError2("record123") // returns error: "failed to process record123 in file data.txt"
Types ¶
This section is empty.