Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. // Package errors provides a way to return errors if they are not nil
  2. // without `if err != nil { return err }` spam.
  3. package errors
  4.  
  5. // handleError represents an error raised by ErrReturn.
  6. // it is here to provide a diference between normal panics and error panics
  7. type handleError struct {
  8. err error
  9. }
  10.  
  11. // only used with Init to avoid repeating if err != nil
  12. // will panic if err != nil, should be caught by Init.
  13. func Return(err error) {
  14. if err != nil {
  15. panic(handleError{err})
  16. }
  17. }
  18.  
  19. // if there is a panic and that panic is an error then
  20. // set err to it, since err is a named return value this effectively
  21. // changes our function to return this error.
  22. //
  23. // must be called inside defer.
  24. func Init(errp *error) {
  25. e := recover()
  26. if e == nil {
  27. return
  28. }
  29.  
  30. // we only deal with handleError, everything else is a valid panic.
  31. if x, ok := e.(handleError); ok {
  32. *errp = x.err
  33. return
  34. }
  35.  
  36. panic(e)
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement