[Pkg-javascript-commits] [node-loud-rejection] 01/03: Import Upstream version 1.6.0

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Thu Oct 27 07:37:19 UTC 2016


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-loud-rejection.

commit 76c42cb54d9f9d4e3ce5d677268430c18ac3baf4
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date:   Thu Oct 27 12:05:54 2016 +0530

    Import Upstream version 1.6.0
---
 .editorconfig         |  12 ++++
 .gitattributes        |   1 +
 .gitignore            |   2 +
 .travis.yml           |   5 ++
 api.js                |  11 ++++
 fixture-custom-log.js |   8 +++
 fixture.js            |  39 +++++++++++
 index.js              |  36 ++++++++++
 license               |  21 ++++++
 package.json          |  59 +++++++++++++++++
 readme.md             |  68 +++++++++++++++++++
 register.js           |   2 +
 test.js               | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++
 13 files changed, 443 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..98a761d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[{package.json,*.yml}]
+indent_style = space
+indent_size = 2
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c9106a7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+.nyc_output
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..aaac48d
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - '6'
+  - '4'
+after_success: npm run coveralls
diff --git a/api.js b/api.js
new file mode 100644
index 0000000..f6cd6a1
--- /dev/null
+++ b/api.js
@@ -0,0 +1,11 @@
+'use strict';
+var util = require('util');
+var currentlyUnhandled = require('currently-unhandled');
+
+// WARNING: This undocumented API is subject to change.
+
+module.exports = util.deprecate(function (process) {
+	return {
+		currentlyUnhandled: currentlyUnhandled(process)
+	};
+}, 'loudRejection/api is deprecated. Use the currently-unhandled module instead.');
diff --git a/fixture-custom-log.js b/fixture-custom-log.js
new file mode 100644
index 0000000..ec99a36
--- /dev/null
+++ b/fixture-custom-log.js
@@ -0,0 +1,8 @@
+'use strict';
+var loudRejection = require('./');
+
+loudRejection(function (str) {
+	console.log('custom-log', str);
+});
+
+Promise.reject(new Error('foo'));
diff --git a/fixture.js b/fixture.js
new file mode 100644
index 0000000..50e5ba0
--- /dev/null
+++ b/fixture.js
@@ -0,0 +1,39 @@
+'use strict';
+var Promise = require('bluebird');
+var loudRejection = require('./');
+
+loudRejection();
+
+var promises = {};
+
+console.log('started');
+
+function reject(key, reason) {
+	// IMPORTANT: key is always logged to stdout
+	// Make sure to remember that when grepping output (keep key and message different).
+	console.log('Rejecting:', key);
+	promises[key] = new Promise(function (resolve, reject) {
+		reject(reason);
+	});
+}
+
+function handle(key) {
+	promises[key].catch(function () {});
+}
+
+process.on('message', function (message) {
+	switch (message.action) {
+		case 'reject-error': return reject(message.key, new Error(message.message));
+		case 'reject-value': return reject(message.key, message.value);
+		case 'reject-nothing': return reject(message.key);
+		case 'reinstall': return loudRejection();
+		case 'handle': return handle(message.key);
+		default:
+			console.error('Unknown message received:', message);
+			process.exit(1);
+	}
+});
+
+process.send({status: 'ready'});
+
+setTimeout(function () {}, 30000);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..319b945
--- /dev/null
+++ b/index.js
@@ -0,0 +1,36 @@
+'use strict';
+var util = require('util');
+var onExit = require('signal-exit');
+var currentlyUnhandled = require('currently-unhandled');
+
+var installed = false;
+
+module.exports = function (log) {
+	if (installed) {
+		return;
+	}
+
+	installed = true;
+
+	log = log || console.error;
+
+	var listUnhandled = currentlyUnhandled();
+
+	onExit(function () {
+		var unhandledRejections = listUnhandled();
+
+		if (unhandledRejections.length > 0) {
+			unhandledRejections.forEach(function (x) {
+				var err = x.reason;
+
+				if (!(err instanceof Error)) {
+					err = new Error('Promise rejected with value: ' + util.inspect(err));
+				}
+
+				log(err.stack);
+			});
+
+			process.exitCode = 1;
+		}
+	});
+};
diff --git a/license b/license
new file mode 100644
index 0000000..654d0bf
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) Sindre Sorhus <sindresorhus at gmail.com> (sindresorhus.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..da5adaf
--- /dev/null
+++ b/package.json
@@ -0,0 +1,59 @@
+{
+  "name": "loud-rejection",
+  "version": "1.6.0",
+  "description": "Make unhandled promise rejections fail loudly instead of the default silent fail",
+  "license": "MIT",
+  "repository": "sindresorhus/loud-rejection",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus at gmail.com",
+    "url": "sindresorhus.com"
+  },
+  "engines": {
+    "node": ">=0.10.0"
+  },
+  "scripts": {
+    "test": "xo && nyc ava",
+    "coveralls": "nyc report --reporter=text-lcov | coveralls"
+  },
+  "files": [
+    "index.js",
+    "register.js",
+    "api.js"
+  ],
+  "keywords": [
+    "promise",
+    "promises",
+    "unhandled",
+    "uncaught",
+    "rejection",
+    "loud",
+    "fail",
+    "catch",
+    "throw",
+    "handler",
+    "exit",
+    "debug",
+    "debugging",
+    "verbose"
+  ],
+  "dependencies": {
+    "currently-unhandled": "^0.4.1",
+    "signal-exit": "^3.0.0"
+  },
+  "devDependencies": {
+    "ava": "*",
+    "bluebird": "^3.0.5",
+    "coveralls": "^2.11.4",
+    "delay": "^1.0.0",
+    "execa": "^0.4.0",
+    "get-stream": "^2.0.0",
+    "nyc": "^6.2.1",
+    "xo": "*"
+  },
+  "nyc": {
+    "exclude": [
+      "fixture.js"
+    ]
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..a973261
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,68 @@
+# loud-rejection [![Build Status](https://travis-ci.org/sindresorhus/loud-rejection.svg?branch=master)](https://travis-ci.org/sindresorhus/loud-rejection) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/loud-rejection/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/loud-rejection?branch=master)
+
+> Make unhandled promise rejections fail loudly instead of the default [silent fail](https://gist.github.com/benjamingr/0237932cee84712951a2)
+
+By default, promises fail silently if you don't attach a `.catch()` handler to them.
+
+Use this in top-level things like tests, CLI tools, apps, etc, **but not in reusable modules.**<br>
+Not needed in the browser as unhandled promises are shown in the console.
+
+
+## Install
+
+```
+$ npm install --save loud-rejection
+```
+
+
+## Usage
+
+```js
+const loudRejection = require('loud-rejection');
+const promiseFn = require('promise-fn');
+
+// Install the unhandledRejection listeners
+loudRejection();
+
+promiseFn();
+```
+
+Without this module it's more verbose and you might even miss some that will fail silently:
+
+```js
+const promiseFn = require('promise-fn');
+
+function error(err) {
+	console.error(err.stack);
+	process.exit(1);
+}
+
+promiseFn().catch(error);
+```
+
+### Register script
+
+Alternatively to the above, you may simply require `loud-rejection/register` and the unhandledRejection listener will be automagically installed for you.
+
+This is handy for ES2015 imports:
+
+```js
+import 'loud-rejection/register';
+```
+
+
+## API
+
+### loudRejection([log])
+
+#### log
+
+Type: `Function`<br>
+Default: `console.error`
+
+Custom logging function to print the rejected promise. Receives the error stack.
+
+
+## License
+
+MIT © [Sindre Sorhus](https://sindresorhus.com)
diff --git a/register.js b/register.js
new file mode 100644
index 0000000..2934812
--- /dev/null
+++ b/register.js
@@ -0,0 +1,2 @@
+'use strict';
+require('./')();
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..74d3efc
--- /dev/null
+++ b/test.js
@@ -0,0 +1,179 @@
+import {fork} from 'child_process';
+import test from 'ava';
+import getStream from 'get-stream';
+import delay from 'delay';
+import execa from 'execa';
+
+function tick(time) {
+	// slow things down for reliable tests on Travis CI
+	return delay(process.env.CI ? time * 10 : time);
+}
+
+test.cb.beforeEach(t => {
+	const child = fork('fixture.js', {silent: true});
+
+	const exit = new Promise((resolve, reject) =>
+		child.on('exit', code =>
+			(code > 0 ? reject : resolve)(code)
+		)
+	);
+
+	t.context = {
+		// tell the child to create a promise, and reject it
+		rejectWithError: (key, message) => child.send({
+			action: 'reject-error',
+			key,
+			message
+		}),
+		rejectWithValue: (key, value) => child.send({
+			action: 'reject-value',
+			key,
+			value
+		}),
+		rejectWithNothing: key => child.send({
+			action: 'reject-nothing',
+			key
+		}),
+
+		// tell the child to handle the promise previously rejected
+		handle: key => child.send({
+			action: 'handle',
+			key
+		}),
+
+		// tell the child to reinstall loudRejection
+		reinstall: () => child.send({action: 'reinstall'}),
+
+		// kill the child (returns a promise for when the child is done)
+		kill: () => {
+			child.kill();
+			return exit;
+		},
+
+		// the stdout of the child. Useful for debug
+		stdout: getStream(child.stdout),
+
+		// the stderr of the child. This is where unhandledRejections will be logged
+		stderr: getStream(child.stderr),
+
+		// promise for when the child has exited
+		exit
+	};
+
+	child.on('message', message => {
+		if (message.status !== 'ready') {
+			t.fail(`I got a message I don't understand: ${JSON.stringify(message)}`);
+		}
+
+		t.end();
+	});
+});
+
+test('no rejections', async t => {
+	const child = t.context;
+
+	await tick(20);
+	await child.kill();
+
+	t.is(await child.stderr, '');
+});
+
+test('one unhandled rejection', async t => {
+	const child = t.context;
+
+	child.rejectWithError('a', 'foo123');
+	await tick(20);
+	await child.kill();
+
+	t.regex(await child.stderr, /foo123/);
+});
+
+test('two unhandled rejections', async t => {
+	const child = t.context;
+
+	child.rejectWithError('a', 'foo456');
+	child.rejectWithError('b', 'bar789');
+	await tick(20);
+	await child.kill();
+
+	t.regex(await child.stderr, /foo456/);
+	t.regex(await child.stderr, /bar789/);
+});
+
+test('one rejection that is handled before exit', async t => {
+	const child = t.context;
+
+	child.rejectWithError('a', 'foo123');
+	await tick(20);
+	child.handle('a');
+	await tick(20);
+	await child.kill();
+
+	t.is(await child.stderr, '');
+});
+
+test('two rejections, first one handled', async t => {
+	const child = t.context;
+
+	child.rejectWithError('a', 'foo987');
+	child.rejectWithError('b', 'bar654');
+	await tick(20);
+	child.handle('a');
+	await tick(20);
+	await child.kill();
+
+	t.false(/foo987/.test(await child.stderr));
+	t.regex(await child.stderr, /bar654/);
+});
+
+test('two rejections, last one handled', async t => {
+	const child = t.context;
+
+	child.rejectWithError('a', 'foo987');
+	child.rejectWithError('b', 'bar654');
+	await tick(20);
+	child.handle('b');
+	await tick(20);
+	await child.kill();
+
+	t.regex(await child.stderr, /foo987/);
+	t.false(/bar654/.test(await child.stderr));
+});
+
+test('rejection with a string value', async t => {
+	const child = t.context;
+
+	child.rejectWithValue('a', 'foo123');
+	await tick(20);
+	await child.kill();
+
+	t.regex(await child.stderr, /Promise rejected with value: 'foo123'/);
+});
+
+test('rejection with a falsy value', async t => {
+	const child = t.context;
+
+	child.rejectWithValue('a', false);
+	child.rejectWithValue('a', 0);
+	await tick(20);
+	await child.kill();
+
+	t.regex(await child.stderr, /Promise rejected with value: false/);
+	t.regex(await child.stderr, /Promise rejected with value: 0/);
+});
+
+test('rejection with no value', async t => {
+	const child = t.context;
+
+	child.rejectWithNothing();
+	await tick(20);
+	await child.kill();
+
+	t.regex(await child.stderr, /Promise rejected with value: undefined/);
+});
+
+test('custom log function', async t => {
+	// TODO: use execa `reject: false` option
+	const stdout = await execa('node', ['fixture-custom-log.js']).catch(err => err.stdout);
+	t.is(stdout.split('\n')[0], 'custom-log Error: foo');
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-loud-rejection.git



More information about the Pkg-javascript-commits mailing list