Skip to content

Commit

Permalink
Merge pull request #36 from rwillians/pr35-rescue-all
Browse files Browse the repository at this point in the history
Rescue.all
  • Loading branch information
rwillians authored Oct 28, 2022
2 parents e1f07c7 + f699c97 commit cb90562
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ app.use((err, req, res, next) => {

```
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])`.
```js
const rescue = require('express-rescue')

// Doing it like this
app.post('/products', rescue.all([
validationFn,
createProductFn
]))

// Is equivalent to this
app.post('/products', [
rescue(validationFn),
rescue(createProductFn)
])
```
That's all.
Expand All @@ -64,6 +82,7 @@ Chears!
## Tests
```txt
> express-rescue@1.2.0 test
> mocha test/*.test.js --check-leaks --full-trace --use_strict --recursive
const callable = rescue(async ([err,] req, res, next) => { })
Expand All @@ -72,12 +91,16 @@ Chears!
✔ Raises a TypeError if last argument is not a function
✔ callable(req, res, next) - works for routes and middlewares
✔ callable(err, req, res, next) - works for error handler middlewares
✔ callable(foo, bar, baz, foobar, foobaz, errorHandler) - should work for basically anything, since you place an error handler as the last argument
✔ callable(foo, bar, baz, foobar, foobaz, errorHandler) - should work for
basically anything, as long as your function takes an error handler as
the last parameter
rescue.from(MyError, (err) => { })
✔ handles the error when error is instance of given constructor
✔ it call `next` function if error is not an instance of given constructor
const callables = rescue.all([fn1, fn2, fn3])
✔ All given functions are wrapped with rescue
7 passing (7ms)
8 passing (8ms)
```
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-rescue",
"version": "1.1.32",
"version": "1.2.0",
"description": "A wrapper for async functions which makes sure all async errors are passed to your errors handler",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -36,6 +36,7 @@
"coverage": "nyc --reporter=text npm test",
"coverage:report": "nyc report --reporter=lcov",
"lint": "standard --global describe --global it",
"lint:fix": "standard --global describe --global it --fix",
"test": "mocha test/*.test.js --check-leaks --full-trace --use_strict --recursive"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export declare type ErrorConstructor = { new(...args: any[]): Error }
export declare interface Rescue {
(callback: Callback): Callback
from (constructor: ErrorConstructor, callback: Callback): Callback
all (callbacks: Callback[]): Callback[]
}

const rescue: Rescue = function rescue (callback) {
Expand Down Expand Up @@ -36,5 +37,9 @@ rescue.from = function rescuefrom (constructor, callback) {
}
}

rescue.all = function rescueall (callbacks) {
return callbacks.map(rescue)
}

export default rescue
module.exports = rescue
37 changes: 25 additions & 12 deletions test/rescue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,35 @@ describe('const callable = rescue(async ([err,] req, res, next) => { })', () =>
})

it('Raises a TypeError if last argument is not a function', () => {
expect(route({}, {}, {}, {}, {}, {}))
.to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function')
return Promise.all([
expect(route({})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function'),
expect(route({}, {})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function'),
expect(route({}, {}, {})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function'),
expect(route({}, {}, {}, {})).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function')
])
})

it('callable(req, res, next) - works for routes and middlewares', () => {
const spy = sinon.spy()
route({}, {}, spy).then(() => {

return route({}, {}, spy).then(() => {
expect(spy.called).to.equals(true)
})
})

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

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

describe('rescue.from(MyError, (err) => { })', () => {
class MyError extends Error {}
class SomethingElse {}
class MyError extends Error { }
class SomethingElse { }

const req = {}
const res = {}
Expand Down Expand Up @@ -84,3 +83,17 @@ describe('rescue.from(MyError, (err) => { })', () => {
rescue.from(MyError, matchedHandler)(new SomethingElse(), req, res, next)
})
})

describe('const callables = rescue.all([fn1, fn2, fn3])', () => {
const fn = async (_cb) => { throw new Error('foo') }

it('All given functions are wrapped with rescue', () => {
const [rescuedFn] = rescue.all([fn])

return Promise.all([
// Proves that the rescued function contains additional behavir that is
// added when a fn is wrapped with `rescue`.
expect(rescuedFn()).to.eventually.be.rejectedWith(TypeError, 'The last parameter received by express-rescue is not a function')
])
})
})

0 comments on commit cb90562

Please sign in to comment.