panic_recovery

package
v1.67.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 2 Imported by: 0

README

Package panic_recovery

Пакет panic_recovery предоставляет инструменты восстановления и обработки ошибки после паники.

Functions

Recover(formatRes func(err error))

Функция Recover(formatRes func(err error)) выполняет восстановление после паники и вызывает функцию formatRes() последующей обработки ошибки.

Usage

Default usage flow
package main

import (
	"fmt"

	"github.com/pkg/errors"
	"github.com/txix-open/isp-kit/panic_recovery"
)

func main() {
	testPanic()
	fmt.Println("panic was recovered")
}

func testPanic() {
	defer panic_recovery.Recover(func(err error) {
		fmt.Printf("\ncatch panic err: %v\n", err)
	})

	panic(errors.New("fatal error"))
	return
}

Documentation

Overview

Package panic_recovery provides utilities for recovering from panics in Go applications. It captures the panic value, formats it with a stack trace, and passes the result to a provided callback function for handling.

The primary function, Recover, is intended to be used in a defer statement to catch panics that occur in the calling goroutine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Recover

func Recover(formatRes func(err error))

Recover captures a panic, formats it with a stack trace, and passes the resulting error to the provided callback function. It should typically be called via defer to handle panics in the current goroutine.

The formatRes function is invoked with a wrapped error containing the panic value and the current goroutine's stack trace. If no panic has occurred, Recover returns immediately without calling formatRes.

Example usage:

defer Recover(func(err error) {
    log.Printf("Recovered from panic: %v", err)
})

Types

This section is empty.

Jump to

Keyboard shortcuts

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