Documentation
¶
Overview ¶
Package calmly implements convenient runtime panic recovery and handling
When a panic condition needs to be handled by the program (rather than crashing it), wrap the code that can trigger such condition in a `Try`, which allows you to `Catch` the panic for further processing.
The `Outcome` of a `Try`ed code also offers convenience methods to: - `KeepCalm` downgrading a panic to an error condition; - `Escalate` upgrading a panic to a fatal error; - `Log` the error, panic or fatal condition, using the appropriate logger method - presumably triggering a new panic or exiting the program.
Index ¶
- Constants
- type Logger
- type Outcome
- func (o *Outcome) AddInfo(s ...string) *Outcome
- func (o *Outcome) Catch(f func(*Outcome)) *Outcome
- func (o *Outcome) Code() int
- func (o *Outcome) Err() error
- func (o *Outcome) Error() string
- func (o *Outcome) Escalate() *Outcome
- func (o *Outcome) Info() []string
- func (o *Outcome) KeepCalm() *Outcome
- func (o *Outcome) Level() int8
- func (o *Outcome) Log(log Logger) *Outcome
- func (o *Outcome) Result() (interface{}, error)
- func (o *Outcome) SetCode(c int) *Outcome
- func (o *Outcome) SetLevel(l int8) *Outcome
- func (o *Outcome) SetText(t string) *Outcome
- func (o *Outcome) Text() string
- func (o *Outcome) Value() interface{}
Constants ¶
const ( OK int8 = 0 ERROR int8 = iota + 3 PANIC FATAL )
Outcome levels match logging levels in agext/log
const ( ERR_TRY_ARG int = iota ERR_TRY_PANIC )
Predefined error codes
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger interface {
Fatal(...interface{})
Panic(...interface{})
Print(...interface{})
}
Logger defines the interface expected by the Log method of Outcome
type Outcome ¶
type Outcome struct {
// contains filtered or unexported fields
}
Outcome represents the state of a `Try`ed call, including information about any panic it may have triggered, as well as the returned value and error, if applicable.
func Try ¶
func Try(f interface{}) (o *Outcome)
Try calls the function it receives as argument, recovering from any panic it may cause
func (*Outcome) Catch ¶
Catch calls the provided function passing the receiver Outcome as argument, only if the Outcome is at PANIC level.
func (*Outcome) Error ¶
Error returns a string representation of the Outcome if it is in an error condition, or an empty string if no error or panic occurred. Note that the Try-ed function returning a non-nil error does not constitute an error condition for the Outcome. That error value can be retrieved via Err or Result. This is also useful for satisfying the `error` interface.
func (*Outcome) Escalate ¶
Escalate converts a PANIC into a FATAL condition, to trigger program termination upon logging the outcome.
func (*Outcome) KeepCalm ¶
KeepCalm downgrades a PANIC to ERROR level, to avoid triggering a panic upon logging the outcome.
func (*Outcome) Log ¶
Log sends the error-condition Outcome to the provided log, using the appropriate logging function: FATAL conditions are logged using Fatal(), PANIC using Panic(), and ERROR using Print(). Non-error conditions are not logged because there is no information stored in the Outcome, beside what the Try-ed function returned (and is better suited to log itself).
func (*Outcome) Result ¶
Result provides the value and error returned by the Try-ed function, if any.