From 8bc95ae2e30b842f2350c5febf5d819692aeb793 Mon Sep 17 00:00:00 2001 From: angrycat9000 Date: Wed, 19 Jul 2023 10:00:50 -0400 Subject: [PATCH] Skip parsing cause if it is not an Error object * Merges #2013 from @angrycat9000 * Fixes #2011 --- spec/core/ExceptionFormatterSpec.js | 20 ++++++++++++++++++++ src/core/ExceptionFormatter.js | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/spec/core/ExceptionFormatterSpec.js b/spec/core/ExceptionFormatterSpec.js index 39c8f5f9..0f7efb6a 100644 --- a/spec/core/ExceptionFormatterSpec.js +++ b/spec/core/ExceptionFormatterSpec.js @@ -301,6 +301,26 @@ describe('ExceptionFormatter', function() { .withContext('first root cause stack frame') .toContain('ExceptionFormatterSpec.js'); }); + + it('does not throw if cause is a non Error', function() { + const formatter = new jasmineUnderTest.ExceptionFormatter(); + + expect(function() { + formatter.stack( + new Error('error', { + cause: function() {} + }) + ); + }).not.toThrowError(); + + expect(function() { + formatter.stack( + new Error('error', { + cause: 'another error' + }) + ); + }).not.toThrowError(); + }); }); }); }); diff --git a/src/core/ExceptionFormatter.js b/src/core/ExceptionFormatter.js index 0d69458b..1b78de0f 100644 --- a/src/core/ExceptionFormatter.js +++ b/src/core/ExceptionFormatter.js @@ -67,7 +67,7 @@ getJasmineRequireObj().ExceptionFormatter = function(j$) { lines.unshift(stackTrace.message); } - if (error.cause) { + if (error.cause && error.cause instanceof Error) { const substack = this.stack_(error.cause, { messageHandling: 'require' });