Skip to content

Commit cb90562

Browse files
authored
Merge pull request #36 from rwillians/pr35-rescue-all
Rescue.all
2 parents e1f07c7 + f699c97 commit cb90562

5 files changed

Lines changed: 59 additions & 17 deletions

File tree

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ app.use((err, req, res, next) => {
5353

5454
```
5555
56+
There's a helper function `rescue.all([...])` in case you want to wrap several functions with `rescue`. With `rescue.all`, doing `[rescue(fn1), rescue(fn2)]` can be shortened to `rescue.all([fn1, fn2])`.
57+
58+
```js
59+
const rescue = require('express-rescue')
60+
61+
// Doing it like this
62+
app.post('/products', rescue.all([
63+
validationFn,
64+
createProductFn
65+
]))
66+
67+
// Is equivalent to this
68+
app.post('/products', [
69+
rescue(validationFn),
70+
rescue(createProductFn)
71+
])
72+
```
73+
5674
That's all.
5775
5876
@@ -64,6 +82,7 @@ Chears!
6482
## Tests
6583
6684
```txt
85+
> express-rescue@1.2.0 test
6786
> mocha test/*.test.js --check-leaks --full-trace --use_strict --recursive
6887
6988
const callable = rescue(async ([err,] req, res, next) => { })
@@ -72,12 +91,16 @@ Chears!
7291
✔ Raises a TypeError if last argument is not a function
7392
✔ callable(req, res, next) - works for routes and middlewares
7493
✔ callable(err, req, res, next) - works for error handler middlewares
75-
✔ callable(foo, bar, baz, foobar, foobaz, errorHandler) - should work for basically anything, since you place an error handler as the last argument
94+
✔ callable(foo, bar, baz, foobar, foobaz, errorHandler) - should work for
95+
basically anything, as long as your function takes an error handler as
96+
the last parameter
7697
7798
rescue.from(MyError, (err) => { })
7899
✔ handles the error when error is instance of given constructor
79100
✔ it call `next` function if error is not an instance of given constructor
80101
102+
const callables = rescue.all([fn1, fn2, fn3])
103+
✔ All given functions are wrapped with rescue
81104
82-
7 passing (7ms)
105+
8 passing (8ms)
83106
```

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-rescue",
3-
"version": "1.1.32",
3+
"version": "1.2.0",
44
"description": "A wrapper for async functions which makes sure all async errors are passed to your errors handler",
55
"license": "MIT",
66
"keywords": [
@@ -36,6 +36,7 @@
3636
"coverage": "nyc --reporter=text npm test",
3737
"coverage:report": "nyc report --reporter=lcov",
3838
"lint": "standard --global describe --global it",
39+
"lint:fix": "standard --global describe --global it --fix",
3940
"test": "mocha test/*.test.js --check-leaks --full-trace --use_strict --recursive"
4041
},
4142
"devDependencies": {

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export declare type ErrorConstructor = { new(...args: any[]): Error }
77
export declare interface Rescue {
88
(callback: Callback): Callback
99
from (constructor: ErrorConstructor, callback: Callback): Callback
10+
all (callbacks: Callback[]): Callback[]
1011
}
1112

1213
const rescue: Rescue = function rescue (callback) {
@@ -36,5 +37,9 @@ rescue.from = function rescuefrom (constructor, callback) {
3637
}
3738
}
3839

40+
rescue.all = function rescueall (callbacks) {
41+
return callbacks.map(rescue)
42+
}
43+
3944
export default rescue
4045
module.exports = rescue

test/rescue.test.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,35 @@ describe('const callable = rescue(async ([err,] req, res, next) => { })', () =>
2525
})
2626

2727
it('Raises a TypeError if last argument is not a function', () => {
28-
expect(route({}, {}, {}, {}, {}, {}))
29-
.to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function')
28+
return Promise.all([
29+
expect(route({})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function'),
30+
expect(route({}, {})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function'),
31+
expect(route({}, {}, {})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function'),
32+
expect(route({}, {}, {}, {})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function')
33+
])
3034
})
3135

3236
it('callable(req, res, next) - works for routes and middlewares', () => {
3337
const spy = sinon.spy()
34-
route({}, {}, spy).then(() => {
38+
39+
return route({}, {}, spy).then(() => {
3540
expect(spy.called).to.equals(true)
3641
})
3742
})
3843

3944
it('callable(err, req, res, next) - works for error handler middlewares', () => {
4045
const spy = sinon.spy()
41-
route({}, {}, {}, spy).then(() => {
42-
expect(spy.called).to.equals(true)
43-
})
44-
})
4546

46-
it('callable(foo, bar, baz, foobar, foobaz, errorHandler) - should work for basically anything, since you place an error handler as the last argument', () => {
47-
const spy = sinon.spy()
48-
route({}, {}, {}, {}, {}, {}, {}, spy).then(() => {
47+
return route({}, {}, {}, spy).then(() => {
4948
expect(spy.called).to.equals(true)
5049
})
5150
})
5251
})
5352
})
5453

5554
describe('rescue.from(MyError, (err) => { })', () => {
56-
class MyError extends Error {}
57-
class SomethingElse {}
55+
class MyError extends Error { }
56+
class SomethingElse { }
5857

5958
const req = {}
6059
const res = {}
@@ -84,3 +83,17 @@ describe('rescue.from(MyError, (err) => { })', () => {
8483
rescue.from(MyError, matchedHandler)(new SomethingElse(), req, res, next)
8584
})
8685
})
86+
87+
describe('const callables = rescue.all([fn1, fn2, fn3])', () => {
88+
const fn = async (_cb) => { throw new Error('foo') }
89+
90+
it('All given functions are wrapped with rescue', () => {
91+
const [rescuedFn] = rescue.all([fn])
92+
93+
return Promise.all([
94+
// Proves that the rescued function contains additional behavir that is
95+
// added when a fn is wrapped with `rescue`.
96+
expect(rescuedFn()).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function')
97+
])
98+
})
99+
})

0 commit comments

Comments
 (0)