Comparing version
@@ -70,6 +70,11 @@ 'use strict'; | ||
exports.background = async function (promise, action, types, options) { | ||
exports.background = async function (operation, action = 'rethrow', types = 'system', options = {}) { | ||
try { | ||
await promise; | ||
if (typeof operation === 'function') { | ||
await operation(); | ||
} | ||
else { | ||
await operation; | ||
} | ||
} | ||
@@ -76,0 +81,0 @@ catch (err) { |
{ | ||
"name": "bounce", | ||
"description": "Selective error catching and rewrite rules", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"repository": "git://github.com/hapijs/bounce", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -19,2 +19,6 @@ # bounce | ||
For more information read: | ||
- [Learning to Throw Again](https://medium.com/@eranhammer/learning-to-throw-again-79b498504d28) | ||
- [Catching without Awaiting](https://medium.com/@eranhammer/catching-without-awaiting-b2cb7df45790) | ||
For example: | ||
@@ -103,2 +107,12 @@ | ||
### `background(operation, [action], [types], [options])` | ||
Awaits for the value to resolve in the background and then apply either the `rethrow()` or `ignore()` | ||
actions where: | ||
- `operation` - a function, promise, or value that is `await`ed on inside a `try...catch` and any | ||
error thrown processed by the `action` rule. | ||
- `action` - one of `'rethrow'` or `'ignore'`. Defaults to `'rethrow'`. | ||
- `types` - same as the `types` argument passed to `rethrow()` or `ignore()`. Defaults to `'system'`. | ||
- `options` - same as the `options` argument passed to `rethrow()` or `ignore()`. | ||
### `isBoom(err)` | ||
@@ -105,0 +119,0 @@ |
@@ -346,2 +346,20 @@ 'use strict'; | ||
it('rethrows system errors (defaults)', async () => { | ||
const test = async () => { | ||
await Hoek.wait(10); | ||
throw new SyntaxError('Something'); | ||
}; | ||
try { | ||
await Bounce.background(test()); | ||
} | ||
catch (err) { | ||
var error = err; | ||
} | ||
expect(error).to.exist(); | ||
}); | ||
it('ignores system errors', async () => { | ||
@@ -375,2 +393,46 @@ | ||
}); | ||
it('rethrows system errors (sync)', async () => { | ||
const test = () => { | ||
throw new SyntaxError('Something'); | ||
}; | ||
try { | ||
await Bounce.background(test, 'rethrow', 'system'); | ||
} | ||
catch (err) { | ||
var error = err; | ||
} | ||
expect(error).to.exist(); | ||
}); | ||
it('ignores system errors (sync)', async () => { | ||
const test = () => { | ||
throw new Error('Something'); | ||
}; | ||
try { | ||
await Bounce.background(test, 'rethrow', 'system'); | ||
} | ||
catch (err) { | ||
var error = err; | ||
} | ||
expect(error).to.not.exist(); | ||
}); | ||
it('ignores system errors (background sync)', () => { | ||
const test = () => { | ||
throw new Error('Something'); | ||
}; | ||
Bounce.background(test, 'rethrow', 'system'); | ||
}); | ||
}); | ||
@@ -377,0 +439,0 @@ |
22538
11.73%483
11.03%134
11.67%