Documentation ¶
Overview ¶
Package catcher provides utilites to gracefully handle crap software.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Catch ¶
func Catch(recv ...Receiver)
Catch must be used together with defer to catch panics from suspicious functions. All listed receivers and all the global notifiers will be invoked with the error itself, function name and a stack trace.
Example ¶
ExampleCatch shows how to treat suspicious funcs properly.
defer Catch( // if this example panics (oh sure it would), just output the // panic message and its stacktrace to the stderr. RecvWrite(os.Stderr, true), ) // SafeCall is an example of a function that uses two receivers. // First one will put the panic message into the error value; // second one will yield the message to the stderr without the stracktrace. SafeCall := func() (err error) { defer Catch( RecvError(&err), RecvWrite(os.Stderr), ) suspiciousFunc() return } // treat suspiciousFunc like a normal func that may return an error if err := SafeCall(); err != nil { log.Println("[ERR] SafeCall failed with:", err) } // don't be afraid to call this func anymore suspiciousFunc()
Output:
func CatchWithContext ¶
func CatchWithContext(context, meta interface{}, recv ...Receiver)
CatchWithContext must be used together with defer to catch panics from suspicious functions. All listed receivers and all the global notifiers will be invoked with the error itself, function name and a stack trace, along with the provided context and meta.
func RegisterNotifiers ¶
func RegisterNotifiers(fn ...NotifierFunc)
RegisterNotifiers adds all listed notifiers to a global list, each of them would be invoked upon an error occurs.
func SetCallerOffset ¶ added in v1.2.0
func SetCallerOffset(n int)
SetCallerOffset allows to customize stacktrace reporting in case the catcher is wrapped. Default is 0. Setting to positive or negative will shift caller's stacktrace window up and down.
Types ¶
type Error ¶
Error has additional context and stack that can be accessed outside receivers; by storing it to a variable using the RecvError receiver.
type NotifierFunc ¶
NotifierFunc is a func that may be used as a global notifier (via RegisterNotifiers()).
type Receiver ¶
type Receiver interface { RecvError(err error, context interface{}, caller string, stack []byte) RecvPanic(err error, context interface{}, caller string, stack []byte) }
Receiver handles panics and errors thrown with a panic.
func RecvDie ¶
RecvDie forces app to exit with the provided exit code. If onPanic is set to true, the app will die only on wild panics. N.B. panic(errors.New(...)) is not a wild panic.
func RecvError ¶
RecvError puts the error catched in the variable by pointer. If useSimple is set to true, the error would not contain anything except the message.