1 |
3a515b92
|
cagy
|
# 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 |
|
|
|
7 |
|
|
# Like iferr, but also catches errors thrown from `succ` and passes to `fail`
|
8 |
|
|
tiferr = (fail, succ) -> iferr fail, (a...) ->
|
9 |
|
|
try succ a...
|
10 |
|
|
catch err then fail err
|
11 |
|
|
|
12 |
|
|
# Delegate to the success function on success, or throw the error otherwise
|
13 |
|
|
# ex: Thing.load 123, throwerr (thing) -> ...
|
14 |
|
|
throwerr = iferr.bind null, (err) -> throw err
|
15 |
|
|
|
16 |
|
|
# Prints errors when one is passed, or does nothing otherwise
|
17 |
|
|
# ex: thing.save printerr
|
18 |
|
|
printerr = iferr (err) -> console.error err.stack or err
|
19 |
|
|
|
20 |
|
|
module.exports = exports = iferr
|
21 |
|
|
exports.iferr = iferr
|
22 |
|
|
exports.tiferr = tiferr
|
23 |
|
|
exports.throwerr = throwerr
|
24 |
|
|
exports.printerr = printerr
|