Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v0.0.3
As finds the first error in `err`'s tree that has type `T`, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.
The tree consists of `err` itself, followed by the errors obtained by repeatedly calling its `Unwrap() error` or `Unwrap() []error` method. When `err` wraps multiple errors, `As` examines `err` followed by a depth-first traversal of its children.
An error has the type `T` if the error's value is assignable to `T`, including cases where there are pointer-value mismatches (e.g., if `T` is `*MyError` but the found error is `MyError`, or vice versa), or if the error has a method `As(any) bool` that returns `true`. To accommodate pointer-value mismatches in `As` implementations, `As` tries different variations of the target type. In the latter case, the `As` method is responsible for setting `target`.
An error type might provide an `As` method, so it can be treated as if it were a different error type.
As panics if `target` is a nil pointer.
func AsError ¶ added in v0.0.3
AsError finds the first error in `err`'s tree that is of type `T`. If a matching error is found, sets target to that error value and returns `true`. Otherwise, it returns false.
This function provides a generic, type-safe alternative to the standard library's `errors.As`.
The error tree is traversed depth-first, starting with `err` itself. The tree is explored by repeatedly calling `Unwrap() error` or `Unwrap() []error`.
An error is considered to be of type `T` if:
- The error's concrete value is assignable to `T`.
- The error has a method `As(any) bool`, and calling `As` with `target` returns `true`. In this case, the `As` method is responsible for setting the value of `target`.
AsError panics if `target` is a nil pointer.
func DepthFirstErrorTree ¶
DepthFirstErrorTree traverses an error tree depth-first and returns a sequence of errors starting from the root error. It supports both single error unwrapping (`Unwrap() error`) and multi-error unwrapping (`Unwrap() []error`) mechanisms. Nil errors or nil results from unwrapping are skipped during traversal.
func Has ¶
Has finds the first error in `err`'s tree that has type `T`, and if one is found, returns that error and true. Otherwise, it returns the zero value for `T` (`nil` for pointer types) and false.
The tree consists of `err` itself, followed by the errors obtained by repeatedly calling its `Unwrap() error` or `Unwrap() []error` method. When `err` wraps multiple errors, `Has` examines `err` followed by a depth-first traversal of its children.
An error has the type `T` if the error's value is assignable to `T`, including cases where there are pointer-value mismatches (e.g., if `T` is `*MyError` but the found error is `MyError`, or vice versa), or if the error has a method `As(any) bool` that returns `true`. To accommodate pointer-value mismatches in `As` implementations, `Has` tries different variations of the target type. In the latter case, the `As` method is responsible for the result.
An error type might provide an `As` method, so it can be treated as if it were a different error type.
Example ¶
package main
import (
"crypto/aes"
"errors"
"fmt"
errorx "fillmore-labs.com/exp/errors"
)
func main() {
key := []byte("My kung fu is better than yours")
_, err := aes.NewCipher(key)
// With errors.As - this check fails silently.
var target *aes.KeySizeError
if errors.As(err, &target) {
fmt.Printf("Wrong AES key size: %d bytes.\n", *target)
}
// With Has - the check succeeds.
if kse, ok := errorx.Has[*aes.KeySizeError](err); ok {
fmt.Printf("AES keys must be 16, 24, or 32 bytes long, got %d bytes.\n", *kse)
}
}
Output: AES keys must be 16, 24, or 32 bytes long, got 31 bytes.
func HasError ¶
HasError finds the first error in `err`'s tree that is of type `T`. If a matching error is found, it is returned along with `true`. Otherwise, the zero value for `T` (`nil` in case of a pointer type) and `false` are returned.
This function provides a generic, type-safe alternative to the standard library's `errors.As`.
The error tree is traversed depth-first, starting with `err` itself. The tree is explored by repeatedly calling `Unwrap() error` or `Unwrap() []error`.
An error is considered to be of type `T` if:
- The error's concrete value is assignable to `T`.
- The error has a method `As(any) bool`, and calling `As` with a pointer to a value of type `T` returns `true`. In this case, the `As` method is responsible for the result.
Example ¶
package main
import (
"crypto/aes"
"fmt"
"fillmore-labs.com/exp/errors"
)
func main() {
key := []byte("My kung fu is better than yours")
_, err := aes.NewCipher(key)
if kse, ok := errors.Has[aes.KeySizeError](err); ok {
fmt.Printf("AES keys must be 16, 24, or 32 bytes long, got %d bytes.\n", kse)
}
}
Output: AES keys must be 16, 24, or 32 bytes long, got 31 bytes.
Types ¶
This section is empty.