errors

package
v1.2.137 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: MIT Imports: 7 Imported by: 8

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrIgnore = errors.New("error can be ignored")
View Source
var ErrorHandlers = []func(error){
	func() func(err error) {
		limiter := rate.NewLimiter(rate.Every(time.Millisecond), 1)
		return func(err error) {

			_ = limiter.Wait(context.Background())
		}
	}(),
}

ErrorHandlers is a list of functions which will be invoked when a nonreturnable error occurs. should be packaged up into a testable and reusable object.

Functions

func HandleError

func HandleError(err error)

HandleError is a method to invoke when a non-user facing piece of code cannot return an error and needs to indicate it has been ignored. Invoking this method is preferable to logging the error - the default behavior is to log but the errors may be sent to a remote server for analysis.

func IsAll added in v1.2.72

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

IsAll reports whether any error in err's tree matches all target in targets.

func IsAny added in v1.2.72

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

IsAny reports whether any error in err's tree matches any target in targets.

func Mark

func Mark(err error, marks ...error) error

Mark returns an error with the supplied errors as marks. If err is nil, return nil. marks take effects only when Is and '%v' in fmt. Is returns true if err or any marks match the target.

Example
package main

import (
	"errors"
	"fmt"

	errors_ "github.com/searKing/golang/go/errors"
)

func main() {
	err := errors.New("err")
	mark1 := errors.New("mark1")
	mark2 := errors.New("mark2")
	mark3 := errors.New("mark3")
	me := errors_.Mark(err, mark1, mark2)
	fmt.Println(me)
	if errors.Is(me, err) {
		fmt.Println("err is err")
	}
	if errors.Is(me, mark1) {
		fmt.Println("err is mark1")
	}
	if errors.Is(me, mark2) {
		fmt.Println("err is mark2")
	}
	if errors.Is(me, mark3) {
		fmt.Println("err is mark3")
	}
	if errors.Is(me, nil) {
		fmt.Println("err is nil")
	}
}
Output:

err
err is err
err is mark1
err is mark2

func Multi deprecated

func Multi(errs ...error) error

Deprecated: Use errors.Join instead since go1.20.

Example
package main

import (
	"errors"
	"fmt"
)

func main() {
	err1 := errors.New("err1")
	err2 := errors.New("err2")
	err3 := errors.New("err3")
	err := errors.Join(err1, err2)
	fmt.Println(err)
	if errors.Is(err, err1) {
		fmt.Println("err is err1")
	}
	if errors.Is(err, err2) {
		fmt.Println("err is err2")
	}
	if errors.Is(err, nil) {
		fmt.Println("err is nil")
	}
	if errors.Is(err, err3) {
		fmt.Println("err is err3")
	}
}
Output:

err1
err2
err is err1
err is err2

func Must

func Must(err error)

Must panics if err != nil

Types

type HandlerDecorator added in v1.2.124

type HandlerDecorator interface {
	// WrapHandler returns a new error that wraps the provided error.
	WrapHandler(h HandlerFunc) HandlerFunc
}

HandlerDecorator is an interface for decorating errors. It provides a WrapHandler method that takes a HandlerFunc and returns a new HandlerFunc. This allows the decorator to wrap the error processing function.

type HandlerDecoratorFunc added in v1.2.124

type HandlerDecoratorFunc func(HandlerFunc) HandlerFunc

HandlerDecoratorFunc is a function type that implements the HandlerDecorator interface. This allows a simple function to be used as a HandlerDecorator.

func (HandlerDecoratorFunc) WrapHandler added in v1.2.124

func (f HandlerDecoratorFunc) WrapHandler(h HandlerFunc) HandlerFunc

WrapHandler implements the HandlerDecorator interface by calling the underlying function.

type HandlerDecorators added in v1.2.124

type HandlerDecorators []HandlerDecorator

HandlerDecorators is a composite decorator that applies a slice of decorators to an error. The decorators are applied in reverse order to simulate a typical wrapping chain (e.g., the last added decorator is the outermost).

func (HandlerDecorators) ErrorOr added in v1.2.137

func (hds HandlerDecorators) ErrorOr(err error, defaultErr error) error

ErrorOr applies all decorators in the slice, in reverse order, to the error. If the error is not handled, returns the default error.

func (HandlerDecorators) WrapError added in v1.2.124

func (hds HandlerDecorators) WrapError(err error) error

WrapError applies all decorators in the slice, in reverse order, to the error.

func (HandlerDecorators) WrapHandler added in v1.2.124

func (hds HandlerDecorators) WrapHandler(next HandlerFunc) HandlerFunc

WrapHandler applies all decorators in the slice, in reverse order, to the error.

type HandlerFunc added in v1.2.124

type HandlerFunc func(err error) error

HandlerFunc defines an error processing function that takes an error and returns a processed error (or nil).

Jump to

Keyboard shortcuts

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