errors

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 3 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package errors provides helpers extending the standard errors package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsAny

func AsAny(err error, targets ...any) bool

AsAny reports whether err matches any of the targets via errors.As.

Example
package main

import (
	"fmt"
	"io/fs"
	"os"
	"strconv"

	goerrors "github.com/foomo/go/errors"
)

func main() {
	var (
		pathErr *fs.PathError
		numErr  *strconv.NumError
	)

	_, err := os.Open("/nonexistent/path/for/example")
	fmt.Println(goerrors.AsAny(err, &numErr, &pathErr))
}
Output:
true

func AsAnyType

func AsAnyType(err error, targets ...any) bool

AsAnyType reports whether err matches the concrete type of any of the targets via errors.As. Each target must be a pointer to the error type to match (e.g. &fs.PathError{}); unlike AsAny, the caller need not declare target variables and the matched value is discarded. nil targets are skipped.

Example
package main

import (
	"fmt"
	"io/fs"
	"os"
	"strconv"

	goerrors "github.com/foomo/go/errors"
)

func main() {
	_, err := os.Open("/nonexistent/path/for/example")
	fmt.Println(goerrors.AsAnyType(err, &fs.PathError{}, &strconv.NumError{}))

}
Output:
true

func Cause added in v0.17.0

func Cause(err error) error

Cause unwraps err through successive types.Causer implementations and returns the root cause. It stops if an error is not a Causer, nil or if Cause returns the same error instance (which would otherwise loop forever). ref: https://github.com/pkg/errors/pull/143

func IsAny

func IsAny(err error, targets ...error) bool

IsAny reports whether err matches any of the targets via errors.Is.

Example
package main

import (
	"context"
	"fmt"
	"io"

	goerrors "github.com/foomo/go/errors"
)

func main() {
	err := fmt.Errorf("wrapped: %w", io.EOF)
	fmt.Println(goerrors.IsAny(err, context.Canceled, io.EOF))
}
Output:
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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