panichandle

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: 1 Imported by: 0

Documentation

Overview

Package panichandle handles panic.

Example
ctx := context.Background()
ctx = SetHandlerToContext(ctx, func(ctx context.Context, r any) {
	fmt.Println("Panic:", r)
})
defer Recover(ctx)
panic("test")
Output:

Panic: test

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Recover

func Recover(ctx context.Context)

Recover recovers panic and calls the Handler returned by GetHandler.

If there is no Handler, it doesn't recover.

It should be called in defer.

func SetHandlerToContext added in v0.4.1

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

SetHandlerToContext sets a Handler to a context.Context.

Types

type ErrorHandler added in v0.3.0

type ErrorHandler struct {
	Handler func(ctx context.Context, err error)
	Convert func(r any) error
}

ErrorHandler is a Handler that converts the recovered value to error with Convert, and calls Handler.

If the recovered value is already an error, Convert is not called.

func (ErrorHandler) Handle added in v0.3.0

func (h ErrorHandler) Handle(ctx context.Context, r any)

Handle handles the panic recovered value.

type Handler

type Handler func(ctx context.Context, r any)

Handler is a function that handles panic.

var DefaultHandler Handler

DefaultHandler is the default Handler.

By default there is no 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.

It may return nil.

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.

Jump to

Keyboard shortcuts

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