summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2017-09-26 01:42:16 +0200
committerAnna Henningsen <anna@addaleax.net>2018-06-06 19:43:59 +0200
commitd1c096cc9d88a865094b91ef86b771cd23d8bfe7 (patch)
treef52222e455d3b953d69388fb542b4343d810e492 /test
parent6d59bfb4f823e30e291a6841410e324879f6486e (diff)
downloadandroid-node-v8-d1c096cc9d88a865094b91ef86b771cd23d8bfe7.tar.gz
android-node-v8-d1c096cc9d88a865094b91ef86b771cd23d8bfe7.tar.bz2
android-node-v8-d1c096cc9d88a865094b91ef86b771cd23d8bfe7.zip
worker: improve error (de)serialization
Rather than passing errors using some sort of string representation, do a best effort for faithful serialization/deserialization of uncaught exception objects. PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-error-serdes.js46
-rw-r--r--test/parallel/test-worker-uncaught-exception-async.js3
-rw-r--r--test/parallel/test-worker-uncaught-exception.js3
3 files changed, 48 insertions, 4 deletions
diff --git a/test/parallel/test-error-serdes.js b/test/parallel/test-error-serdes.js
new file mode 100644
index 0000000000..e9d91e5736
--- /dev/null
+++ b/test/parallel/test-error-serdes.js
@@ -0,0 +1,46 @@
+// Flags: --expose-internals
+'use strict';
+require('../common');
+const assert = require('assert');
+const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
+const { serializeError, deserializeError } = require('internal/error-serdes');
+
+function cycle(err) {
+ return deserializeError(serializeError(err));
+}
+
+assert.strictEqual(cycle(0), 0);
+assert.strictEqual(cycle(-1), -1);
+assert.strictEqual(cycle(1.4), 1.4);
+assert.strictEqual(cycle(null), null);
+assert.strictEqual(cycle(undefined), undefined);
+assert.strictEqual(cycle('foo'), 'foo');
+
+{
+ const err = cycle(new Error('foo'));
+ assert(err instanceof Error);
+ assert.strictEqual(err.name, 'Error');
+ assert.strictEqual(err.message, 'foo');
+ assert(/^Error: foo\n/.test(err.stack));
+}
+
+assert.strictEqual(cycle(new RangeError('foo')).name, 'RangeError');
+assert.strictEqual(cycle(new TypeError('foo')).name, 'TypeError');
+assert.strictEqual(cycle(new ReferenceError('foo')).name, 'ReferenceError');
+assert.strictEqual(cycle(new URIError('foo')).name, 'URIError');
+assert.strictEqual(cycle(new EvalError('foo')).name, 'EvalError');
+assert.strictEqual(cycle(new SyntaxError('foo')).name, 'SyntaxError');
+
+class SubError extends Error {}
+
+assert.strictEqual(cycle(new SubError('foo')).name, 'Error');
+
+assert.deepStrictEqual(cycle({ message: 'foo' }), { message: 'foo' });
+assert.strictEqual(cycle(Function), '[Function: Function]');
+
+{
+ const err = new ERR_INVALID_ARG_TYPE('object', 'Object', 42);
+ assert(/^TypeError \[ERR_INVALID_ARG_TYPE\]:/.test(err));
+ assert.strictEqual(err.name, 'TypeError [ERR_INVALID_ARG_TYPE]');
+ assert.strictEqual(err.code, 'ERR_INVALID_ARG_TYPE');
+}
diff --git a/test/parallel/test-worker-uncaught-exception-async.js b/test/parallel/test-worker-uncaught-exception-async.js
index c1d2a5f4fc..1f45c46db9 100644
--- a/test/parallel/test-worker-uncaught-exception-async.js
+++ b/test/parallel/test-worker-uncaught-exception-async.js
@@ -10,8 +10,7 @@ if (!process.env.HAS_STARTED_WORKER) {
const w = new Worker(__filename);
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
- // TODO(addaleax): be more specific here
- assert(/foo/.test(err));
+ assert(/^Error: foo$/.test(err));
}));
} else {
setImmediate(() => {
diff --git a/test/parallel/test-worker-uncaught-exception.js b/test/parallel/test-worker-uncaught-exception.js
index b0e3ad11fa..cb2a0d79d2 100644
--- a/test/parallel/test-worker-uncaught-exception.js
+++ b/test/parallel/test-worker-uncaught-exception.js
@@ -10,8 +10,7 @@ if (!process.env.HAS_STARTED_WORKER) {
const w = new Worker(__filename);
w.on('message', common.mustNotCall());
w.on('error', common.mustCall((err) => {
- // TODO(addaleax): be more specific here
- assert(/foo/.test(err));
+ assert(/^Error: foo$/.test(err));
}));
} else {
throw new Error('foo');