errorhandle

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package errorhandle handles error.

Example
ctx := context.Background()
ctx = SetHandlerToContext(ctx, func(ctx context.Context, err error) {
	fmt.Println("Error:", err)
})
Handle(ctx, errors.New("test"))
Output:

Error: test

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handle

func Handle(ctx context.Context, err error)

Handle calls the Handler returned by GetHandler.

func SetHandlerToContext added in v0.4.1

func SetHandlerToContext(ctx context.Context, h Handler) context.Context

SetHandlerToContext sets a Handler to a context.Context.

func StderrHandler

func StderrHandler(ctx context.Context, err error)

StderrHandler is a Handler that writes the error to os.Stderr.

Types

type FilterHandler

type FilterHandler struct {
	Handler
	Filter func(ctx context.Context, err error) bool
}

FilterHandler is a Handler that filters error.

If Filter returns true, the error is passed to Handler.

func (FilterHandler) Handle

func (f FilterHandler) Handle(ctx context.Context, err error)

Handle handles the error using the Filter and Handler.

type Handler

type Handler func(ctx context.Context, err error)

Handler is a function that handles error.

var DefaultHandler Handler = StderrHandler

DefaultHandler is the default Handler.

func GetHandler added in v0.4.1

func GetHandler(ctx context.Context) Handler

GetHandler gets a Handler from a context.Context or the DefaultHandler.

func GetHandlerFromContext added in v0.4.1

func GetHandlerFromContext(ctx context.Context) Handler

GetHandlerFromContext gets a Handler from a context.Context.

It returns nil if no Handler is set.

type Handlers

type Handlers []Handler

Handlers is a list of Handler.

They are called in order.

func (Handlers) Handle

func (hs Handlers) Handle(ctx context.Context, err error)

Handle handles the error using the list of Handler.

Jump to

Keyboard shortcuts

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