Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Package errors provides a way to return errors if they are not nil
- // without `if err != nil { return err }` spam.
- package errors
- // handleError represents an error raised by ErrReturn.
- // it is here to provide a diference between normal panics and error panics
- type handleError struct {
- err error
- }
- // only used with Init to avoid repeating if err != nil
- // will panic if err != nil, should be caught by Init.
- func Return(err error) {
- if err != nil {
- panic(handleError{err})
- }
- }
- // if there is a panic and that panic is an error then
- // set err to it, since err is a named return value this effectively
- // changes our function to return this error.
- //
- // must be called inside defer.
- func Init(errp *error) {
- e := recover()
- if e == nil {
- return
- }
- // we only deal with handleError, everything else is a valid panic.
- if x, ok := e.(handleError); ok {
- *errp = x.err
- return
- }
- panic(e)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement