pax_global_header00006660000000000000000000000064135047250650014521gustar00rootroot0000000000000052 comment=4f0267025d7da699e833ca50420ac0b5f9b29073 bounce-1.3.1/000077500000000000000000000000001350472506500127765ustar00rootroot00000000000000bounce-1.3.1/.gitignore000066400000000000000000000001541350472506500147660ustar00rootroot00000000000000**/node_modules **/package-lock.json coverage.* **/.DS_Store **/._* **/*.pem **/.vs **/.vscode **/.idea bounce-1.3.1/.travis.yml000066400000000000000000000002231350472506500151040ustar00rootroot00000000000000language: node_js node_js: - "8" - "10" - "12" - "node" sudo: false install: - "npm install" os: - "linux" - "osx" - "windows" bounce-1.3.1/CHANGELOG.md000066400000000000000000000005461350472506500146140ustar00rootroot00000000000000Breaking changes are documented using GitHub issues, see [issues labeled "release notes"](https://github.com/hapijs/bounce/issues?q=is%3Aissue+label%3A%22release+notes%22). If you want changes of a specific minor or patch release, you can browse the [GitHub milestones](https://github.com/hapijs/bounce/milestones?state=closed&direction=asc&sort=due_date). bounce-1.3.1/LICENSE.md000077500000000000000000000026751350472506500144170ustar00rootroot00000000000000Copyright (c) 2017-2019, Sideway Inc, and project contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. bounce-1.3.1/README.md000077500000000000000000000106001350472506500142550ustar00rootroot00000000000000 # bounce Selective error catching and rewrite rules [![Build Status](https://secure.travis-ci.org/hapijs/bounce.svg)](http://travis-ci.org/hapijs/bounce) ## Introduction Working with `async`/`await` introduces a new challange in handling errors. Unlike callbacks, which provide a dual mechanism for passing application errors via the callback `err` argument and developer errors via exceptions, `await` combines these two channels into one. It is common practice to ignore application errors in background processing or when there is no useful fallback. In those cases, it is still imperative to allow developer errors to surface and not get swallowed. 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: ```js async function email(user) { if (!user.address) { throw new Error('User has no email address'); } const message = 'Welcome!'; if (user.name) { message = `Welcome ${user.name}!`; } await mailer.send(user.address, message); } async function register(address, name) { const user = { address, name }; const id = await db.user.insert(user); user.id = id; try { await email(user); } catch (err) { } // Ignore errors return user; } ``` This will fail silently every time the user has a `name` because it is reassigning a value to a `const` variable. However, because `email()` errors are ignored, system errors are ignored as well. The idea is that `email()` can be used in both critical and non-critical paths. In the critical paths, errors are checked and addressed, but in the non-critical paths, errors are simply ignored. This can be solved by adding a `rethrow()` statement: ```js const Bounce = require('@hapi/bounce'); async function register(address, name) { const user = { address, name }; const id = await db.user.insert(user); user.id = id; try { await email(user); } catch (err) { Bounce.rethrow(err, 'system'); // Rethrows system errors and ignores application errors } return user; } ``` ## Usage ### `rethrow(err, types, [options])` Throws the error passed if it matches any of the specified rules where: - `err` - the error. - `type` - a single item or an array of items of: - An error constructor (e.g. `SyntaxError`). - `'system'` - matches any languange native error or node assertions. - `'boom'` - matches [**boom**](https://github.com/hapijs/boom) errors. - an object where each property is compared with the error and must match the error property value. All the properties in the object must match the error but do not need to include all the error properties. - `options` - optional object where: - `decorate` - an object which is assigned to the `err`, copying the properties onto the error. - `override` - an error used to override `err` when `err` matches. If used with `decorate`, the `override` object is modified. - `return` - if `true`, the error is returned instead of thrown. Defaults to `false`. ### `ignore(err, types, [options])` The opposite action of `rethrow()`. Ignores any errors matching the specified `types`. Any error not matching is thrown after applying the `options`. ### `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)` Returns `true` when `err` is a [**boom**](https://github.com/hapijs/boom) error. ### `isError(err)` Returns `true` when `err` is an error. ### `isSystem(err)` Return `true` when `err` is one of: - `EvalError` - `RangeError` - `ReferenceError` - `SyntaxError` - `TypeError` - `URIError` - Node's `AssertionError` bounce-1.3.1/lib/000077500000000000000000000000001350472506500135445ustar00rootroot00000000000000bounce-1.3.1/lib/index.js000077500000000000000000000047411350472506500152220ustar00rootroot00000000000000'use strict'; const Assert = require('assert'); const Boom = require('@hapi/boom'); const Hoek = require('@hapi/hoek'); const internals = { system: [ // JavaScript EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, // Node Assert.AssertionError, // Hoek Hoek.Error ] }; exports.rethrow = function (err, types, options = {}) { return internals.catch(err, types, options, true); }; exports.ignore = function (err, types, options = {}) { return internals.catch(err, types, options, false); }; internals.catch = function (err, types, options, match) { if (internals.match(err, types) !== match) { return; } // Error replacement if (options.override) { err = options.override; } // Error decorations if (options.decorate) { Object.assign(err, options.decorate); } if (options.return) { return err; } throw err; }; exports.background = async function (operation, action = 'rethrow', types = 'system', options = {}) { try { if (typeof operation === 'function') { await operation(); } else { await operation; } } catch (err) { exports[action](err, types, options); } }; exports.isBoom = function (err) { return Boom.isBoom(err); }; exports.isError = function (err) { return err instanceof Error; }; exports.isSystem = function (err) { if (!err) { return false; } if (err.isBoom) { return false; } for (let i = 0; i < internals.system.length; ++i) { if (err instanceof internals.system[i]) { return true; } } return false; }; internals.rules = { system: exports.isSystem, boom: exports.isBoom }; internals.match = function (err, types) { if (!types) { return true; } types = Array.isArray(types) ? types : [types]; for (let i = 0; i < types.length; ++i) { const type = types[i]; if (typeof type === 'string') { if (internals.rules[type](err)) { return true; } } else if (typeof type === 'object') { if (Hoek.contain(err, type, { deep: true, part: true })) { return true; } } else if (err instanceof type) { return true; } } return false; }; bounce-1.3.1/package.json000066400000000000000000000010541350472506500152640ustar00rootroot00000000000000{ "name": "@hapi/bounce", "description": "Selective error catching and rewrite rules", "version": "1.3.1", "repository": "git://github.com/hapijs/bounce", "main": "lib/index.js", "keywords": [ "error", "catch" ], "dependencies": { "@hapi/boom": "7.x.x", "@hapi/hoek": "8.x.x" }, "devDependencies": { "@hapi/code": "5.x.x", "@hapi/lab": "19.x.x" }, "scripts": { "test": "lab -a @hapi/code -t 100 -L", "test-cov-html": "lab -a @hapi/code -r html -o coverage.html -L" }, "license": "BSD-3-Clause" } bounce-1.3.1/test/000077500000000000000000000000001350472506500137555ustar00rootroot00000000000000bounce-1.3.1/test/index.js000077500000000000000000000320621350472506500154300ustar00rootroot00000000000000'use strict'; const Assert = require('assert'); const Code = require('@hapi/code'); const Boom = require('@hapi/boom'); const Bounce = require('..'); const Hoek = require('@hapi/hoek'); const Lab = require('@hapi/lab'); const internals = {}; const { describe, it } = exports.lab = Lab.script(); const expect = Code.expect; describe('Bounce', () => { describe('rethrow()', () => { it('rethrows all errors', () => { const orig = new Error('Something'); try { Bounce.rethrow(orig); } catch (err) { var error = err; } expect(error).to.shallow.equal(orig); expect(error).to.be.an.error('Something'); }); it('rethrows only system errors', () => { try { Bounce.rethrow(new Error('Something'), 'system'); } catch (err) { var error1 = err; } expect(error1).to.not.exist(); try { Bounce.rethrow(new URIError('Something'), 'system'); } catch (err) { var error2 = err; } expect(error2).to.be.an.error('Something', URIError); }); it('rethrows only boom errors', () => { try { Bounce.rethrow(new Error('Something'), 'boom'); } catch (err) { var error1 = err; } expect(error1).to.not.exist(); try { Bounce.rethrow(Boom.badRequest('Something'), 'boom'); } catch (err) { var error2 = err; } expect(error2).to.be.an.error('Something'); }); it('rethrows only boom/system errors', () => { try { Bounce.rethrow(new Error('Something'), ['boom', 'system']); } catch (err) { var error1 = err; } expect(error1).to.not.exist(); try { Bounce.rethrow(Boom.badRequest('Something'), ['boom', 'system']); } catch (err) { var error2 = err; } expect(error2).to.be.an.error('Something'); try { Bounce.rethrow(new SyntaxError('Something'), ['boom', 'system']); } catch (err) { var error3 = err; } expect(error3).to.be.an.error('Something', SyntaxError); }); it('rethrows only specified errors', () => { try { Bounce.rethrow(new Error('Something'), URIError); } catch (err) { var error1 = err; } expect(error1).to.not.exist(); try { Bounce.rethrow(new URIError('Something'), URIError); } catch (err) { var error2 = err; } expect(error2).to.be.an.error('Something', URIError); }); it('rethrows only specified errors', () => { try { Bounce.rethrow(new Error('Something'), URIError); } catch (err) { var error1 = err; } expect(error1).to.not.exist(); try { Bounce.rethrow(new URIError('Something'), URIError); } catch (err) { var error2 = err; } expect(error2).to.be.an.error('Something', URIError); }); it('rethrows only errors matching a pattern', () => { try { Bounce.rethrow(new Error('Something'), { x: 1 }); } catch (err) { var error1 = err; } expect(error1).to.not.exist(); const xErr = new Error('Something'); xErr.x = 1; try { Bounce.rethrow(xErr, { x: 1 }); } catch (err) { var error2 = err; } expect(error2).to.be.an.error('Something'); }); it('rethrows only errors matching a pattern (deep)', () => { try { Bounce.rethrow(new Error('Something'), { x: { y: 2 } }); } catch (err) { var error1 = err; } expect(error1).to.not.exist(); const xErr = new Error('Something'); xErr.x = { y: 2, z: 4 }; try { Bounce.rethrow(xErr, { x: { y: 2 } }); } catch (err) { var error2 = err; } expect(error2).to.be.an.error('Something'); }); it('rethrows a decorated error', () => { const orig = new Error('Something'); const decorate = { x: 1, y: 'z' }; try { Bounce.rethrow(orig, Error, { decorate }); } catch (err) { var error = err; } expect(error).to.shallow.equal(orig); expect(error).to.be.an.error('Something'); expect(error.x).to.equal(1); expect(error.y).to.equal('z'); }); it('throws a different error', () => { const orig = new Error('Something'); try { Bounce.rethrow(orig, Error, { override: new Error('Else') }); } catch (err) { var error = err; } expect(error).to.not.shallow.equal(orig); expect(error).to.be.an.error('Else'); }); it('returns error instead of throwing', () => { const orig = new Error('Something'); expect(() => Bounce.rethrow(orig, Error, { return: true })).to.not.throw(); const error = Bounce.rethrow(orig, Error, { return: true }); expect(error).to.shallow.equal(orig); expect(error).to.be.an.error('Something'); }); }); describe('ignore()', () => { it('ignores system errors', () => { try { Bounce.ignore(new Error('Something'), 'system'); } catch (err) { var error1 = err; } expect(error1).to.be.an.error('Something', Error); try { Bounce.ignore(new URIError('Something'), 'system'); } catch (err) { var error2 = err; } expect(error2).to.not.exist(); }); it('ignores boom errors', () => { try { Bounce.ignore(new Error('Something'), 'boom'); } catch (err) { var error1 = err; } expect(error1).to.be.an.error('Something', Error); try { Bounce.ignore(Boom.badRequest('Something'), 'boom'); } catch (err) { var error2 = err; } expect(error2).to.not.exist(); }); it('ignores boom/system errors', () => { try { Bounce.ignore(new Error('Something'), ['boom', 'system']); } catch (err) { var error1 = err; } expect(error1).to.be.an.error('Something', Error); try { Bounce.ignore(Boom.badRequest('Something'), ['boom', 'system']); } catch (err) { var error2 = err; } expect(error2).to.not.exist(); try { Bounce.ignore(new ReferenceError('Something'), ['boom', 'system']); } catch (err) { var error3 = err; } expect(error3).to.not.exist(); }); }); describe('background()', () => { it('rethrows system errors', async () => { const test = async () => { await Hoek.wait(10); throw new SyntaxError('Something'); }; try { await Bounce.background(test(), 'rethrow', 'system'); } catch (err) { var error = err; } expect(error).to.exist(); }); 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 () => { const test = async () => { await Hoek.wait(10); 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)', () => { const test = async () => { await Hoek.wait(10); throw new Error('Something'); }; Bounce.background(test(), 'rethrow', 'system'); }); 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'); }); }); describe('isBoom()', () => { it('identifies Boom as Boom', () => { expect(Bounce.isBoom(Boom.badRequest())).to.be.true(); }); it('identifies EvalError as non-boom', () => { expect(Bounce.isBoom(new EvalError())).to.be.false(); }); it('identifies object as non-boom', () => { expect(Bounce.isBoom({})).to.be.false(); }); it('identifies object with isBoom as non-boom', () => { expect(Bounce.isBoom({ isBoom: true })).to.be.false(); }); }); describe('isError()', () => { it('identifies Error as error', () => { expect(Bounce.isError(new Error())).to.be.true(); }); it('identifies Boom as error', () => { expect(Bounce.isError(Boom.badRequest())).to.be.true(); }); it('identifies object as non-error', () => { expect(Bounce.isBoom({})).to.be.false(); }); }); describe('isSystem()', () => { it('identifies EvalError as system', () => { expect(Bounce.isSystem(new EvalError())).to.be.true(); }); it('identifies RangeError as system', () => { expect(Bounce.isSystem(new RangeError())).to.be.true(); }); it('identifies ReferenceError as system', () => { expect(Bounce.isSystem(new ReferenceError())).to.be.true(); }); it('identifies SyntaxError as system', () => { expect(Bounce.isSystem(new SyntaxError())).to.be.true(); }); it('identifies TypeError as system', () => { expect(Bounce.isSystem(new TypeError())).to.be.true(); }); it('identifies URIError as system', () => { expect(Bounce.isSystem(new URIError())).to.be.true(); }); it('identifies node AssertionError as system', () => { expect(Bounce.isSystem(new Assert.AssertionError({}))).to.be.true(); }); it('identifies hoek Error as system', () => { expect(Bounce.isSystem(new Hoek.Error([]))).to.be.true(); }); it('identifies Error as non-system', () => { expect(Bounce.isSystem(new Error())).to.be.false(); }); it('identifies Boom as non-system', () => { expect(Bounce.isSystem(Boom.badRequest())).to.be.false(); }); it('identifies object as non-system', () => { expect(Bounce.isSystem({})).to.be.false(); }); it('identifies null as non-system', () => { expect(Bounce.isSystem(null)).to.be.false(); }); it('identifies boomified system as non-system', () => { expect(Bounce.isSystem(Boom.boomify(new TypeError()))).to.be.false(); }); }); });