Documentation
¶
Index ¶
- Variables
- func HandleError(err error)
- func IsAll(err error, targets ...error) bool
- func IsAny(err error, targets ...error) bool
- func Mark(err error, marks ...error) error
- func Multi(errs ...error) errordeprecated
- func Must(err error)
- type HandlerDecorator
- type HandlerDecoratorFunc
- type HandlerDecorators
- type HandlerFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrIgnore = errors.New("error can be ignored")
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
IsAll reports whether any error in err's tree matches all target in targets.
func IsAny ¶ added in v1.2.72
IsAny reports whether any error in err's tree matches any target in targets.
func Mark ¶
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
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
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) 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
HandlerFunc defines an error processing function that takes an error and returns a processed error (or nil).