recovery

package module
v0.0.0-...-faf067e Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: MIT Imports: 7 Imported by: 0

README

go-recovery

A small library for panic handling in golang

Simple example

Code
package main

import (
	recovery "github.com/protogrammer/go-recovery"
	"log"
)

var rc = recovery.CreateConfig(recovery.PanicMessage.Log, nil)

func fun(a, b int) int {
	defer rc.Recur("fun")
	defer recovery.Commentf("a = %d, b = %d", a, b)
	return a / b
}

func main() {
	defer recovery.DoFinally() // not obligatory in this particular case
	defer rc.Block("main")
	x := fun(5, 0)
	log.Print(x)
}
Possible output
2022/09/21 07:07:24
Panic: runtime error: integer divide by zero
Type: runtime.errorString
Call stack:
    main
 -> fun
    Comment: a = 5, b = 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert(cond bool, msg string)

func AssertEq

func AssertEq[T comparable](a T, b T, msg string)

func AssertFalse

func AssertFalse(msg string)

func AssertNeq

func AssertNeq[T comparable](a T, b T, msg string)

func AssertNot

func AssertNot(cond bool, msg string)

func AssertNotNil

func AssertNotNil[T any](ptr *T, msg string)

func Assertf

func Assertf(cond bool, format string, a ...any)

func Capture

func Capture(value any)

func CaptureResult

func CaptureResult(f func() any)

func Comment

func Comment(values ...any)

func CommentResult

func CommentResult(f func() string)

func Commentf

func Commentf(format string, values ...any)

func Commentln

func Commentln(values ...any)

func Defer

func Defer(f func())

func DoFinally

func DoFinally()

func Finally

func Finally(finally func())

func IfNotPanic

func IfNotPanic(f func())

func IfPanic

func IfPanic(f func())

func Recover

func Recover()

func SafeCall

func SafeCall(process string, f func())

func Set

func Set[T any](ptr *T, val T)

func SetResult

func SetResult[T any](ptr *T, f func() T)

func Stop

func Stop()

Types

type AssertEqInfo

type AssertEqInfo[T comparable] struct {
	Message    string
	Arg1, Arg2 T
}

type AssertFalseInfo

type AssertFalseInfo struct {
	Message string
}

type AssertInfo

type AssertInfo struct {
	Message string
}

type AssertNeqInfo

type AssertNeqInfo[T comparable] struct {
	Message    string
	Arg1, Arg2 T
}

type AssertNotInfo

type AssertNotInfo struct {
	Message string
}

type AssertNotNilInfo

type AssertNotNilInfo[T any] struct {
	Message string
}

type CaptureType

type CaptureType any

type CommentType

type CommentType string

type Config

type Config struct {
	// contains filtered or unexported fields
}

func CreateConfig

func CreateConfig(handlePanic HandlePanicFunc, specifyBehavior SpecifyBehaviorFunc) Config

func (Config) Block

func (config Config) Block(process string)

func (Config) BlockCall

func (config Config) BlockCall(process string, function any, args ...any) []any

func (Config) BlockCallDefer

func (config Config) BlockCallDefer(process string, deferFunction func(), function any, args ...any) []any

func (Config) Recover

func (config Config) Recover(process string)

func (Config) RecoverCall

func (config Config) RecoverCall(process string, function any, args ...any) []any

func (Config) RecoverCallDefer

func (config Config) RecoverCallDefer(process string, deferFunction func(), function any, args ...any) []any

func (Config) Recur

func (config Config) Recur(process string)

func (Config) RecurCall

func (config Config) RecurCall(process string, function any, args ...any) []any

func (Config) RecurCallDefer

func (config Config) RecurCallDefer(process string, deferFunction func(), function any, args ...any) []any

func (Config) Stop

func (config Config) Stop(process string)

func (Config) StopCall

func (config Config) StopCall(process string, function any, args ...any) []any

func (Config) StopCallDefer

func (config Config) StopCallDefer(process string, deferFunction func(), function any, args ...any) []any

type FinallyFuncIsNilError

type FinallyFuncIsNilError struct{}

type HandlePanicFunc

type HandlePanicFunc func(PanicMessage)

type PanicHandlerEnum

type PanicHandlerEnum uint
const (
	BLOCK PanicHandlerEnum = iota
	STOP
	RECUR
	RECOVER
)

type PanicMessage

type PanicMessage struct {
	CallStack []StackFrame
	Err       any
}

func PanicMessageFromError

func PanicMessageFromError(err any) *PanicMessage

func (*PanicMessage) AddMetadata

func (msg *PanicMessage) AddMetadata(metadata StackMetadata)

func (*PanicMessage) AddProcess

func (msg *PanicMessage) AddProcess(process string)

func (PanicMessage) Log

func (msg PanicMessage) Log()

func (PanicMessage) String

func (msg PanicMessage) String() string

func (PanicMessage) StringIndent

func (msg PanicMessage) StringIndent(indent uint) string

type SpecifyBehaviorFunc

type SpecifyBehaviorFunc func(err any, panicHandler PanicHandlerEnum) PanicHandlerEnum

type StackFrame

type StackFrame struct {
	Process  *string
	Metadata []StackMetadata
}

type StackMetadata

type StackMetadata any // CommentType | CaptureType | PanicMessage

Jump to

Keyboard shortcuts

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