nopanic

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2021 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package nopanic provides a utility to wrap function to prevent any panics from escaping. If a panic occurs in the callback, the value of the panic will be converted to an error, and returned normally.

This packages has the opinion that you should not panic with arbitrary values. The argument to panic should either be a string, or a value that implements the error interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unwrap

func Unwrap(err error) error

Unwrap will panic if err is an instance of PanicError, otherwise it will return the error unmodified.

Example
package main

import (
	"fmt"

	"bitbucket.org/rj/goey/internal/nopanic"
)

func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered...")
			fmt.Println(r)
		}
	}()

	// Wrap the callback to prevent the escape of any panics.
	err := nopanic.Unwrap(nopanic.Wrap(func() error {
		// Bad things sometimes happen to good people...
		panic("No luck!")
	}))
	// Check for errors.
	if err != nil {
		// Otherwise, continue with normal error handling.
		fmt.Println("Normal error...")
		fmt.Println(err)
	}

	// Output (not reproducible because of stack trace):
	// Recovered...
	// No luck!
	//
	// goroutine 1 [running]:
	// runtime/debug.Stack(0x0, 0x0, 0xc04202b9c8)
	//         C:/Go/src/runtime/debug/stack.go:24 +0x80
	// bitbucket.org/rj/goey/internal/nopanic.New(0x5190c0, 0xc04200cf30, 0xc04200cf30, 0xc04202ba78, 0x40ef6c, 0xc04200cf30)
	//         bitbucket.org/rj/goey/internal/nopanic/_test/_obj_test/nopanic.go:35 +0x33
	// bitbucket.org/rj/goey/internal/nopanic.Wrap.func1(0xc04202bb58)
	//         bitbucket.org/rj/goey/internal/nopanic/_test/_obj_test/nopanic.go:45 +0x71
	// panic(0x5190c0, 0xc04200cf30)
	//         C:/Go/src/runtime/panic.go:489 +0x2dd
	//     ... several frame omitted ...
	// main.main()
	//         bitbucket.org/rj/goey/internal/nopanic/_test/_testmain.go:98 +0x201
}
Output:

func Wrap

func Wrap(action func() error) (err error)

Wrap ensures that no panics escape. Action will be called, and if it returns normally, its return value will be returned. However, if action panics, the panic will be converted to an error, and will be returned.

Example
package main

import (
	"errors"
	"fmt"

	"bitbucket.org/rj/goey/internal/nopanic"
)

func main() {
	myerr := errors.New("No luck!")

	// Wrap the callback to prevent the escape of any panics.
	err := nopanic.Wrap(func() error {
		return myerr
	})
	// Check for errors.
	if err != nil {
		// Print the
		fmt.Println(err)
	}

}
Output:

No luck!
Example (Panic)
package main

import (
	"fmt"

	"bitbucket.org/rj/goey/internal/nopanic"
)

func main() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered...")
			fmt.Println(r)
		}
	}()

	// Wrap the callback to prevent the escape of any panics.
	err := nopanic.Wrap(func() error {
		// Bad things sometimes happen to good people...
		panic("No luck!")
	})
	// Check for errors.
	if err != nil {
		// If this is a PanicError, we rethrow.
		if value, ok := err.(nopanic.PanicError); ok {
			panic(value.Value())
		}
		// Otherwise, continue with normal error handling.
		fmt.Println("Normal error...")
		fmt.Println(err)
	}

}
Output:

Recovered...
No luck!

Types

type PanicError

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

PanicError represents a panic that occurred.

func New

func New(value interface{}) PanicError

New wraps the value returned by recover into a PanicError.

func (PanicError) Error

func (pe PanicError) Error() string

Error returns a description of the error.

func (PanicError) Stack

func (pe PanicError) Stack() string

Stack returns a formatted stack trace of the goroutine that originally paniced.

func (PanicError) Value

func (pe PanicError) Value() interface{}

Value returns the value returned by recover after a panic.

Jump to

Keyboard shortcuts

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