Comparing version 1.1.0 to 2.0.0
@@ -1,11 +0,15 @@ | ||
var onError = require('..') | ||
var EventEmitter = require('events').EventEmitter, | ||
onError = require('..') | ||
function fail (cb) { | ||
cb(new Error('failed')) | ||
// fake something that works | ||
function doSomething (cb) { | ||
cb(null, 'success') | ||
} | ||
function succeed (cb) { | ||
cb(null, 'success') | ||
// fake something that breaks | ||
function failToDoSomething (cb) { | ||
cb(new Error('failed')) | ||
} | ||
// error handler | ||
function handleIt (err) { | ||
@@ -15,14 +19,25 @@ console.error(err) | ||
fail(onError(handleIt, function (status) { | ||
console.log('will not see this') | ||
// if error handleIt, otherwise... | ||
doSomething(onError(handleIt).otherwise(function (message) { | ||
console.log('will see this: %s', message) | ||
})) | ||
fail(onError(handleIt, {alwaysCall: true}, function (err, status) { | ||
// When always call specified, err is passed | ||
console.log('will see this') | ||
failToDoSomething(onError(handleIt).otherwise(function (message) { | ||
console.log('will NOT see this: %s', message) | ||
})) | ||
succeed(onError(handleIt, function (status) { | ||
// Gets called with status of success | ||
console.log(status) | ||
// if error handleIt, and also... | ||
failToDoSomething(onError(handleIt).also(function (message) { | ||
console.log('will see this too: %s', message) | ||
})) | ||
// if error handleIt, and also (include error for also func)... | ||
failToDoSomething(onError(handleIt).alsoWithError(function (err, message) { | ||
console.log('will see this too (with the error): %s, %s', err, message) | ||
})) | ||
// maybe we want to emit the error instead... | ||
var emitter = new EventEmitter().on('error', console.log) | ||
failToDoSomething(onError.emit(emitter).otherwise(function (message) { | ||
// will not get here | ||
})) |
56
index.js
@@ -1,16 +0,48 @@ | ||
module.exports = function onError (errHandler, options, cb) { | ||
if (arguments.length === 2) | ||
cb = options, options = {} | ||
var assert = require('assert') | ||
return function () { | ||
if (arguments[0]) | ||
errHandler(arguments[0]) | ||
if (!cb) return | ||
if (options.alwaysCall) | ||
return cb.apply(cb, arguments) | ||
if (arguments[0]) return | ||
return cb.apply(cb, Array.prototype.slice.call(arguments, 1)) | ||
module.exports = onError | ||
function onError (errCb) { | ||
assert(typeof errCb === 'function', 'on-error requires a callback') | ||
function errHandler () { | ||
if (arguments[0]) errCb.call(this, arguments[0]) | ||
} | ||
return decorated(errHandler) | ||
} | ||
onError.emit = function (emitter) { | ||
assert(typeof emitter.emit === 'function', 'on-error.emit requires an EventEmitter') | ||
module.exports.emit = require('emit-error') | ||
function errHandler () { | ||
if (arguments[0]) emitter.emit('error', arguments[0]) | ||
} | ||
return decorated(errHandler) | ||
} | ||
function decorated (errHandler) { | ||
errHandler.otherwise = function (otherwiseCb) { | ||
assert(typeof otherwiseCb === 'function', 'on-error.otherwise requires a callback') | ||
return function otherwiseHandler () { | ||
if (arguments[0]) return errHandler(arguments[0]) | ||
return otherwiseCb.apply(this, Array.prototype.slice.call(arguments, 1)) | ||
} | ||
} | ||
errHandler.always = function (alwaysCb) { | ||
assert(typeof alwaysCb === 'function', 'on-error.always requires a callback') | ||
return function alwaysHandler () { | ||
if (arguments[0]) errHandler(arguments[0]) | ||
return alwaysCb.apply(this, Array.prototype.slice.call(arguments, 1)) | ||
} | ||
} | ||
errHandler.alwaysWithError = function (alwaysCb) { | ||
assert(typeof alwaysCb === 'function', 'on-error.alwaysWithError requires a callback') | ||
return function alwaysHandler () { | ||
if (arguments[0]) errHandler(arguments[0]) | ||
return alwaysCb.apply(this, arguments) | ||
} | ||
} | ||
return errHandler | ||
} |
{ | ||
"name": "on-error", | ||
"version": "1.1.0", | ||
"description": "Execute error handler instead of, or in addition to callback, when 1st callback argument is truthy", | ||
"version": "2.0.0", | ||
"description": "Handle callback errors without the if blocks.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test", | ||
"benchmark": "make benchmark", | ||
"travis-test": "make test && ((cat coverage/lcov.info | coveralls) || exit 0)", | ||
@@ -20,3 +19,6 @@ "view-cover": "open coverage/index.html", | ||
"error", | ||
"callback" | ||
"callback", | ||
"emit", | ||
"event", | ||
"emitter" | ||
], | ||
@@ -41,18 +43,2 @@ "license": "MIT", | ||
}, | ||
"testling": { | ||
"files": "test/*.js", | ||
"browsers": [ | ||
"ie/8..latest", | ||
"firefox/16..latest", | ||
"firefox/nightly", | ||
"chrome/22..latest", | ||
"chrome/canary", | ||
"opera/12..latest", | ||
"opera/next", | ||
"safari/5.1..latest", | ||
"ipad/6.0..latest", | ||
"iphone/6.0..latest", | ||
"android-browser/4.2..latest" | ||
] | ||
}, | ||
"dependencies": { | ||
@@ -59,0 +45,0 @@ "emit-error": "^2.0.0" |
121
README.md
# on-error | ||
[![NPM version](https://badge.fury.io/js/on-error.png)](http://badge.fury.io/js/on-error) | ||
[![Build Status](https://travis-ci.org/jasonpincin/on-error.svg?branch=master)](https://travis-ci.org/jasonpincin/on-error) | ||
[![Coverage Status](https://coveralls.io/repos/jasonpincin/on-error/badge.png?branch=master)](https://coveralls.io/r/jasonpincin/on-error?branch=master) | ||
[![NPM version](https://badge.fury.io/js/on-error.png)](http://badge.fury.io/js/on-error) | ||
[![Davis Dependency Status](https://david-dm.org/jasonpincin/on-error.png)](https://david-dm.org/jasonpincin/on-error) | ||
Handle callback errors by executing an error handler function, or emitting the error, instead of or in | ||
addition to executing the callback. | ||
Handle callback errors without the `if` blocks. | ||
On error, execute separate error handler function, or emit it. On no error, execute regular callback | ||
(with or without error stripped). | ||
## example | ||
``` | ||
var onError = require('on-error') | ||
```javascript | ||
// if error handleIt, otherwise... | ||
doSomething(onError(handleIt).otherwise(function (message) { | ||
console.log('will see this: %s', message) | ||
})) | ||
function fail (cb) { | ||
cb(new Error('failed')) | ||
} | ||
function succeed (cb) { | ||
cb(null, 'success') | ||
} | ||
``` | ||
Using error handler function: | ||
``` | ||
function handleIt (err) { | ||
console.error(err) | ||
} | ||
fail(onError(handleIt, function (status) { | ||
console.log('will not see this') | ||
failToDoSomething(onError(handleIt).otherwise(function (message) { | ||
console.log('will NOT see this: %s', message) | ||
})) | ||
fail(onError(handleIt, {alwaysCall: true}, function (err, status) { | ||
// When always call specified, err is passed | ||
console.log('will see this') | ||
// if error handleIt, and always... | ||
failToDoSomething(onError(handleIt).always(function (message) { | ||
console.log('will see this too: %s', message) | ||
})) | ||
succeed(onError(handleIt, function (status) { | ||
// Gets called with status of success | ||
console.log(status) | ||
// if error handleIt, and always (include error for always func)... | ||
failToDoSomething(onError(handleIt).alwaysWithError(function (err, message) { | ||
console.log('will see this too (with the error): %s, %s', err, message) | ||
})) | ||
``` | ||
Using emitter: | ||
``` | ||
var EventEmitter = require('events').EventEmitter | ||
var emitter = new EventEmitter().on('error', function (err) { | ||
console.error(err) | ||
}) | ||
fail(onError.emit(emitter, function (status) { | ||
console.log('will not see this') | ||
// maybe we want to emit the error instead... | ||
var emitter = new EventEmitter().on('error', console.log) | ||
failToDoSomething(onError.emit(emitter).otherwise(function (message) { | ||
// will not get here | ||
})) | ||
@@ -60,52 +44,45 @@ ``` | ||
### onError | ||
```javascript | ||
var onError = require('on-error') | ||
``` | ||
`var onError = require('on-error')` | ||
### onError(cb) | ||
### var wrappedCb = onError(errHandler [, options, cb]) | ||
Returns a function that when called with a truthy first argument, will execute `cb` with said | ||
argument. | ||
Returns a function that when called with a truthy first argument, will execute the errHandler | ||
with the 1st (error) argument supplied to the wrappedCb. If the option `alwaysCall` is define, | ||
the provided `cb` will be executed in all cases with all arguments supplied to `wrappedCb`, otherwise | ||
if the 1st argument to `wrappedCb` is falsey, the supplied `cb` will be executed with all but the | ||
1st argument supplied to `wrappedCb`. | ||
### onError.emit(emitter) | ||
If no callback `cb` is provided, then the generated callback will simply execute `errHandler` when | ||
called with a non-falsey 1st argument. | ||
Returns a function that when called with a truthy first argument, will emit `error` on the provided | ||
event `emitter` with said argument. | ||
*options:* | ||
- alwaysCall: `true` or `false` - if true, the provided callback `cb` will always be called (and include | ||
the 1st argument), otherwise it will only be called when a the first argument is falsey (and without the | ||
1st argument) | ||
### onError(cb1).otherwise(cb2) | ||
### var wrappedCb = onError.emit(emitter [, options, cb]) | ||
Returns a function that when called with a truthy first argument, will execute `cb1` with said | ||
argument. When executed with a non-truthy 1st argument, `cb2` will instead be executed with | ||
the error argument stripped. | ||
Same behaviour as above, except errors will be emitted to `emitter` instead of passed to an error handler | ||
function. All other options and arguments are the same. | ||
`.otherwise` may always be chained from `onError.emit()` | ||
### onError(cb1).always(cb2) | ||
## testing | ||
Returns a function that when called with a truthy first argument, will execute `cb1` with said | ||
argument. In addition, `cb2` will always be executed with the error argument stripped. | ||
`npm test [--dot | --spec] [--coverage]` | ||
`.always` may always be chained from `onError.emit()` | ||
### options | ||
### onError(cb1).alwaysWithError(cb2) | ||
* `--dot` - output test results as dots instead of tap | ||
* `--spec` - output test results as spec instead of tap | ||
* `--coverage` - display text cover report | ||
* `--testling` - run tests in browser via testling (cannot be used with --coverage and | ||
expects both browserify and testling to be installed globally) | ||
Returns a function that when called with a truthy first argument, will execute `cb1` with said | ||
argument. In addition, `cb2` will always be executed with the error argument in-tact. | ||
### patterns | ||
`.alwaysWithError` may always be chained from `onError.emit()` | ||
Only run test files matching a certain pattern by prefixing the | ||
test command with `grep=pattern`. Example: | ||
## testing | ||
``` | ||
grep=connect npm test --dot | ||
``` | ||
`npm test [--dot | --spec] [--coverage]` | ||
### html coverage report | ||
Alternatively, only run test files matching a certain pattern by prefixing the command | ||
with `grep=pattern`. Example: `grep=init npm test` | ||
Open it with `npm run view-cover` or `npm run vc` | ||
Open an html coverage report after running tests with `npm run view-cover` or `npm run vc` |
@@ -5,21 +5,31 @@ var test = require('tape'), | ||
test('on-error', function (t) { | ||
function handleIt () { | ||
var args = Array.prototype.slice.call(arguments, 0) | ||
errorCalls.push(args) | ||
} | ||
function callback () { | ||
var args = Array.prototype.slice.call(arguments, 0) | ||
return args | ||
} | ||
var errorCalls = [] | ||
t.equal(typeof onError.emit, 'function', '.emit should be a function') | ||
t.notOk(onError(handleIt, callback)(new Error('error 1')), 'should return undefined on error without alwaysCall') | ||
t.deepEqual(onError(handleIt, {alwaysCall:true}, callback)(new Error('error 2')), [new Error('error 2')], 'should return args on error with alwaysCall') | ||
t.deepEqual(onError(handleIt, callback)(null, 1, 2), [1, 2], 'should return args on no error') | ||
t.notOk(onError(handleIt)(new Error('error 2')), 'should return undefined on error with no cb') | ||
t.deepEqual(onError(handleIt)(), undefined, 'should return undefined on no error with no cb') | ||
t.deepEqual(errorCalls.length, 3, 'should have emitted 3 error events') | ||
t.equal(typeof onError, 'function', 'should be a function') | ||
t.throws(onError, 'requires a callback') | ||
test('when passed an error', function (t) { | ||
var errorCalls = [] | ||
function errHandler () { | ||
var args = Array.prototype.slice.call(arguments, 0) | ||
errorCalls.push(args) | ||
} | ||
var anError = new Error('error 1') | ||
t.notOk(onError(errHandler)(anError), 'should return undefined') | ||
t.equal(errorCalls.length, 1, 'should invoke error handler') | ||
t.equal(errorCalls[0][0], anError, 'should pass error to handler') | ||
t.end() | ||
}) | ||
test('when passed no error', function (t) { | ||
var errorCalls = [] | ||
function errHandler () { | ||
var args = Array.prototype.slice.call(arguments, 0) | ||
errorCalls.push(args) | ||
} | ||
t.notOk(onError(errHandler)(null), 'should return undefined') | ||
t.equal(errorCalls.length, 0, 'should not invoke error handler') | ||
t.end() | ||
}) | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22574
15
279
88
1