From b3a2726cbcfee3f5576daf231957626bc395454a Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Sun, 28 Jul 2024 15:13:59 +0200 Subject: [PATCH] assert: use isError instead of instanceof in innerOk Co-Authored-By: Ruben Bridgewater Co-Authored-By: Nihar Phansalkar PR-URL: https://github.com/nodejs/node/pull/53980 Fixes: https://github.com/nodejs/node/issues/50780 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Marco Ippolito --- lib/assert.js | 2 +- test/parallel/test-assert.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/assert.js b/lib/assert.js index 9dfcf80a913942..eadc3844c20128 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -393,7 +393,7 @@ function innerOk(fn, argLen, value, message) { } else if (message == null) { generatedMessage = true; message = getErrMessage(message, fn); - } else if (message instanceof Error) { + } else if (isError(message)) { throw message; } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 4c4073699f0548..26d239e8889b26 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -55,6 +55,16 @@ assert.throws(() => a.ok(false), a.AssertionError, 'ok(false)'); assert.ok(threw, 'Error: ok(false)'); } +// Errors created in different contexts are handled as any other custom error +{ + const context = vm.createContext(); + const error = vm.runInContext('new SyntaxError("custom error")', context); + + assert.throws(() => assert(false, error), { + message: 'custom error', + name: 'SyntaxError' + }); +} a(true); a('test', 'ok(\'test\')');