paniccatcher

package
v0.0.0-...-add0f8d Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 1 Imported by: 37

Documentation

Overview

Package paniccatcher package exposes a set of utility structures and methods that support standardized panic catching and handling.

Example

Example is a very simple example of how to use Catch to recover from a panic and log its stack trace.

Do(func() {
	fmt.Println("Doing something...")
	panic("Something wrong happened!")
}, func(p *Panic) {
	fmt.Println("Caught a panic:", p.Reason)
})
Output:

Doing something...
Caught a panic: Something wrong happened!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Catch

func Catch(cb func(p *Panic))

Catch recovers from panic. It should be used as a deferred call.

If the supplied panic callback is nil, the panic will be silently discarded. Otherwise, the callback will be invoked with the panic's information.

func Do

func Do(f func(), cb func(p *Panic))

Do executes f. If a panic occurs during execution, the supplied callback will be called with the panic's information.

If the panic callback is nil, the panic will be caught and discarded silently.

Types

type Panic

type Panic struct {
	// Reason is the value supplied to the recover function.
	Reason any
	// Stack is a stack dump at the time of the panic.
	Stack string
}

Panic is a snapshot of a panic, containing both the panic's reason and the system stack.

func PCall

func PCall(fn func()) (ret *Panic)

PCall calls a nullary function and returns the panic if there was one.

Jump to

Keyboard shortcuts

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