From 0412f8d6d677d2dba71e749a9cdbf7234a47e47c Mon Sep 17 00:00:00 2001 From: Nathan Sarang-Walters Date: Fri, 3 Nov 2023 14:49:07 -0700 Subject: [PATCH] Ensure error objects are propagated without modification (#1920) * Ensure AggregateError is propagated without modification * Update test to not rely on AggregateError global * Update lib/asyncify.js Co-authored-by: Alex Early * Update .eslintrc.json to use es2021 globals --------- Co-authored-by: Alex Early --- .eslintrc.json | 2 +- lib/asyncify.js | 2 +- test/asyncify.js | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 17eca479a..e5e65bdc9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -2,7 +2,7 @@ "env": { "browser": true, "node": true, - "es6": "es2021" + "es2021": true }, "parser": "@babel/eslint-parser", "parserOptions": { diff --git a/lib/asyncify.js b/lib/asyncify.js index 21eebee1f..13978b8d7 100644 --- a/lib/asyncify.js +++ b/lib/asyncify.js @@ -87,7 +87,7 @@ function handlePromise(promise, callback) { return promise.then(value => { invokeCallback(callback, null, value); }, err => { - invokeCallback(callback, err && err.message ? err : new Error(err)); + invokeCallback(callback, err && (err instanceof Error || err.message) ? err : new Error(err)); }); } diff --git a/test/asyncify.js b/test/asyncify.js index f76c0a754..3417b71d7 100644 --- a/test/asyncify.js +++ b/test/asyncify.js @@ -59,6 +59,22 @@ describe('asyncify', () => { } }); + it('propagates error with empty message without modification', (done) => { + let originalErr; + async.asyncify(async () => { + originalErr = new AggregateError([new Error('foo'), new Error('bar')]); + throw originalErr; + })((err) => { + assert(err); + assert.strictEqual(err, originalErr); + expect(err.errors).to.be.an('array'); + expect(err.errors.length).to.equal(2); + expect(err.errors[0].message).to.equal('foo'); + expect(err.errors[1].message).to.equal('bar'); + done(); + }) + }) + describe('promisified', () => { function promisifiedTests(Promise) { it('resolve', (done) => {