index.coffee 803 B

12345678910111213141516171819202122232425
  1. # Delegates to `succ` on sucecss or to `fail` on error
  2. # ex: Thing.load 123, iferr cb, (thing) -> ...
  3. iferr = (fail, succ) -> (err, a...) ->
  4. if err? then fail err
  5. else succ? a...
  6. # Like iferr, but also catches errors thrown from `succ` and passes to `fail`
  7. tiferr = (fail, succ) -> iferr fail, (a...) ->
  8. try succ a...
  9. catch err then fail err
  10. # Delegate to the success function on success, or throw the error otherwise
  11. # ex: Thing.load 123, throwerr (thing) -> ...
  12. throwerr = iferr.bind null, (err) -> throw err
  13. # Prints errors when one is passed, or does nothing otherwise
  14. # ex: thing.save printerr
  15. printerr = iferr (err) -> console.error err.stack or err
  16. module.exports = exports = iferr
  17. exports.iferr = iferr
  18. exports.tiferr = tiferr
  19. exports.throwerr = throwerr
  20. exports.printerr = printerr