package/package.json000644 001750 001750 0000001127 12437337741013033 0ustar00000000 000000 { "name": "iferr", "version": "0.1.5", "description": "Higher-order functions for easier error handling", "main": "index.js", "scripts": { "test": "mocha", "prepublish": "coffee -c index.coffee" }, "repository": { "type": "git", "url": "https://github.com/shesek/iferr" }, "keywords": [ "error", "errors" ], "author": "Nadav Ivgi", "license": "MIT", "bugs": { "url": "https://github.com/shesek/iferr/issues" }, "homepage": "https://github.com/shesek/iferr", "devDependencies": { "coffee-script": "^1.7.1", "mocha": "^1.18.2" } } package/.npmignore000644 001750 001750 0000000015 12322011447012520 0ustar00000000 000000 node_modules package/README.md000644 001750 001750 0000001243 12437032673012017 0ustar00000000 000000 # iferr Higher-order functions for easier error handling. `if (err) return cb(err);` be gone! ## Install ```bash npm install iferr ``` ## Use ### JavaScript example ```js var iferr = require('iferr'); function get_friends_count(id, cb) { User.load_user(id, iferr(cb, function(user) { user.load_friends(iferr(cb, function(friends) { cb(null, friends.length); })); })); } ``` ### CoffeeScript example ```coffee iferr = require 'iferr' get_friends_count = (id, cb) -> User.load_user id, iferr cb, (user) -> user.load_friends iferr cb, (friends) -> cb null, friends.length ``` (TODO: document tiferr, throwerr and printerr) ## License MIT package/LICENSE000644 001750 001750 0000002064 12322011521011525 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2014 Nadav Ivgi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.package/index.js000644 001750 001750 0000002055 12437337773012220 0ustar00000000 000000 // Generated by CoffeeScript 1.7.1 (function() { var exports, iferr, printerr, throwerr, tiferr, __slice = [].slice; iferr = function(fail, succ) { return function() { var a, err; err = arguments[0], a = 2 <= arguments.length ? __slice.call(arguments, 1) : []; if (err != null) { return fail(err); } else { return typeof succ === "function" ? succ.apply(null, a) : void 0; } }; }; tiferr = function(fail, succ) { return iferr(fail, function() { var a, err; a = 1 <= arguments.length ? __slice.call(arguments, 0) : []; try { return succ.apply(null, a); } catch (_error) { err = _error; return fail(err); } }); }; throwerr = iferr.bind(null, function(err) { throw err; }); printerr = iferr(function(err) { return console.error(err.stack || err); }); module.exports = exports = iferr; exports.iferr = iferr; exports.tiferr = tiferr; exports.throwerr = throwerr; exports.printerr = printerr; }).call(this); package/index.coffee000644 001750 001750 0000001443 12437337674013033 0ustar00000000 000000 # Delegates to `succ` on sucecss or to `fail` on error # ex: Thing.load 123, iferr cb, (thing) -> ... iferr = (fail, succ) -> (err, a...) -> if err? then fail err else succ? a... # Like iferr, but also catches errors thrown from `succ` and passes to `fail` tiferr = (fail, succ) -> iferr fail, (a...) -> try succ a... catch err then fail err # Delegate to the success function on success, or throw the error otherwise # ex: Thing.load 123, throwerr (thing) -> ... throwerr = iferr.bind null, (err) -> throw err # Prints errors when one is passed, or does nothing otherwise # ex: thing.save printerr printerr = iferr (err) -> console.error err.stack or err module.exports = exports = iferr exports.iferr = iferr exports.tiferr = tiferr exports.throwerr = throwerr exports.printerr = printerr package/test/index.coffee000644 001750 001750 0000002164 12437033204013771 0ustar00000000 000000 { iferr, tiferr, throwerr } = require '../index.coffee' { equal: eq, throws } = require 'assert' invoke_fail = (cb) -> cb new Error 'callback error' invoke_succ = (cb) -> cb null throw_error = -> throw new Error 'thrown' describe 'iferr', -> it 'calls the error callback on errors', (done) -> invoke_fail iferr( (err) -> eq err.message, 'callback error' do done -> done new Error 'shouldn\'t call the success callback' ) it 'calls the success callback on success', (done) -> invoke_succ iferr( -> done new Error 'shouldn\'t call the error callback' done ) describe 'tiferr', -> it 'catches errors in the success callback', (done) -> invoke_succ tiferr( (err) -> eq err.message, 'thrown' do done throw_error ) describe 'throwerr', -> it 'throws errors passed to the callback', (done)-> try invoke_fail throwerr -> done 'shouldn\'t call the success callback' catch err eq err.message, 'callback error' do done it 'delegates to the success callback otherwise', (done) -> invoke_succ throwerr done package/test/mocha.opts000644 001750 001750 0000000072 12322010355013476 0ustar00000000 000000 --compilers coffee:coffee-script/register --reporter spec