recoverich

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

README

test coverage Go Report Card

Install

go get -u github.com/ildomm/recoverich

Recoverich Module

The recoverich library provides functions to recover from panics, log errors, and track values during a panic situation. This README provides an explanation of each public method along with examples of how to use them.

Recover()

This is the most basic version of the recovery function. It recovers from a panic, logs the error, and prints a stack trace.

Example:
package main

import (
	"github.com/ildomm/recoverich"
)

func main() {
    defer recoverich.Recover()

    // Your code that may panic
    panic("Something went wrong!")
}

RecoverWithTrackedValues(values ...interface{})

This function enhances the basic recovery by allowing you to log additional tracked values along with the error and stack trace.

Example:
package main

import (
	"github.com/ildommm/recoverich"
)

func main() {
    defer recoverich.RecoverWithTrackedValues("Track this value", 42)

    // Your code that may panic
    panic("Something went wrong!")
}

Results in the following log output:

ERROR 2024/01/30 08:53:42 Recovered from panic: Something went wrong!
ERROR 2024/01/30 08:53:42 Stacktrace ***
 *errors.errorString Something went wrong!
/recoverich/recover.go:11 (0x104bec81c)
        print: e := errors.Wrap(err, 0)
/recoverich/recover_with_tracked_values.go:18 (0x104bec98c)
        RecoverWithTrackedValues: print(err)
/golang/1.21.4/go/src/runtime/panic.go:920 (0x104b6fdc4)
        gopanic: d.fn()
/recoverich/cmd/main.go:14 (0x104becea8)
        main: panic("Something went wrong!")
/golang/1.21.4/go/src/runtime/proc.go:267 (0x104b72834)
        main: fn()
/golang/1.21.4/go/src/runtime/asm_arm64.s:1197 (0x104b9d544)
        goexit: MOVD    R0, R0  // NOP
 
*** 
ERROR 2024/01/30 08:53:42 Tracked values ***
ERROR 2024/01/30 08:53:42 Name: 0, Type: string, Value: Track this value 
ERROR 2024/01/30 08:53:42 Name: 1, Type: int, Value: 42 
ERROR 2024/01/30 08:53:42 ***

RecoverWithContextValues(ctx context.Context)

This function is designed for use with the context package. It recovers from a panic, logs the error and stack trace, and also logs values associated with the context.

Example:
package main

import (
    "context"
    "fmt"
    "github.com/ildomm/recoverich"
)

func main() {
    ctx := context.WithValue(context.Background(), "exampleKey", "exampleValue")

    defer recoverich.RecoverWithContextValues(ctx)

    // Your code that may panic
    panic("Something went wrong!")
}

Note: Ensure that your context values are compatible with reflection, and the Key and Value fields are accessible. This example assumes a simple case; you may need to adapt it for more complex scenarios.

Results in the following log output:

ERROR 2024/01/30 09:26:26 Recovered from panic: Something went wrong!
ERROR 2024/01/30 09:26:26 Stacktrace ***
 *errors.errorString Something went wrong!
/recoverich/recover.go:11 (0x1045f957c)
        print: e := errors.Wrap(err, 0)
/recoverich/recover_with_context_values.go:16 (0x1045f9844)
        RecoverWithContextValues: print(err)
/golang/1.21.4/go/src/runtime/panic.go:920 (0x1045782b4)
        gopanic: d.fn()
/recoverich/cmd/main.go:24 (0x1045fa4fc)
        main: panic("Something went wrong!")
/golang/1.21.4/go/src/runtime/proc.go:267 (0x10457ad24)
        main: fn()
/golang/1.21.4/go/src/runtime/asm_arm64.s:1197 (0x1045a5cc4)
        goexit: MOVD    R0, R0  // NOP
 
*** 
ERROR 2024/01/30 09:26:26 Context values ***
ERROR 2024/01/30 09:26:26 Name: exampleKey, Type: string, Value: exampleValue 
ERROR 2024/01/30 09:26:26 ***

Feel free to explore and integrate these functions into your projects to enhance error recovery and debugging capabilities.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Recover

func Recover()

Recover recovers from a panic and logs the error and stack trace. This is the most basic version of Recover.

func RecoverWithContextValues

func RecoverWithContextValues(ctx context.Context)

RecoverWithContextValues recovers from a panic and logs the error and stack trace. It also logs the context values.

func RecoverWithTrackedValues

func RecoverWithTrackedValues(values ...any)

RecoverWithTrackedValues recovers from a panic and logs the error and stack trace. It also logs the tracked values.

The tracked values are any values that you want to log when a panic occurs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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