go-agent: github.com/sqreen/go-agent/agent/sqlib/sqsafe Index | Files

package sqsafe

import "github.com/sqreen/go-agent/agent/sqlib/sqsafe"

This package provides functions making sure panics are safely caught and do not break the running program. Since panics can stop the program execution if they are not recovered, we need ways to safely recover and handle them.

Therefore, this package provides simple function call wrappers to call a function that may panic and its goroutine equivalent. That way, any function can be safely called and any panic will be returned as a regular function error.

Index

Package Files

call.go doc.go go.go

func Call Uses

func Call(f func() error) (err error)

Call calls function `f` and recovers from any panic occurring while it executes, returning it in a `PanicError` object type.

func Go Uses

func Go(f func() error) <-chan error

Go mimics the `go` goroutine built-in to execute function `f` in a goroutine but with the ability to safely recover from any panic occurring while it executes. To do so, it uses `Call()` and returns an error channel in order to retrieve any panic occurring during the execution of `f()` or the error it returns otherwise. An error is sent into the channel only in case of error or panic, and is closed in any case before returning from the goroutine.

Usage example:

		errChan := safe.Go(f)
   // ...
		select {
			case err := <-errChan:
				var panicErr *sqlib.PanicError
				if xerrors.As(err, &panicErr) {
					// A panic occurred while executing f()
				} else {
					// A regular error was returned by f()
				}
			// ...
		}

type PanicError Uses

type PanicError struct {
    // The function that was given to `Call()`.
    In  func() error
    // The recovered panic value while executing `In()`.
    Err error
}

PanicError is an error type wrapping a recovered panic value that happened during a function call.

func NewPanicError Uses

func NewPanicError(in func() error, err error) *PanicError

func (*PanicError) Cause Uses

func (e *PanicError) Cause() error

func (*PanicError) Error Uses

func (e *PanicError) Error() string

func (*PanicError) Unwrap Uses

func (e *PanicError) Unwrap() error

Package sqsafe imports 5 packages (graph) and is imported by 2 packages. Updated 2019-06-15. Refresh now. Tools for package owners.