errors

package
v1.2.115 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 6 Imported by: 7

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

This section is empty.

Jump to

Keyboard shortcuts

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