From 885afb02ed469a55de03ac070a0c9d1e5b0c44b3 Mon Sep 17 00:00:00 2001 From: Chirag Bitoni Date: Sat, 14 Nov 2026 23:49:00 +0530 Subject: [PATCH] fix: slice off all leading lines of multiline error message in clean() Previously, clean() checked if line 0 was not an at line and line 1 was an at line, assuming error messages were always a single line. When an error message spanned multiple lines, none of the message lines were sliced off before the loop, causing the last message line to be retained as a leading header before the first frame. Use findIndex to locate the first at frame and slice off all preceding message lines. Fixes #53 Signed-off-by: Chirag Bitoni --- index.js | 5 +++-- test/test.js | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f567133..b67507d 100644 --- a/index.js +++ b/index.js @@ -53,8 +53,9 @@ class StackUtils { stack = stack.split('\n'); } - if (!(/^\s*at /.test(stack[0])) && (/^\s*at /.test(stack[1]))) { - stack = stack.slice(1); + const firstAt = stack.findIndex(line => /^\s*at /.test(line)); + if (firstAt > 0) { + stack = stack.slice(firstAt); } let outdent = false; diff --git a/test/test.js b/test/test.js index 8721f6c..cf20694 100644 --- a/test/test.js +++ b/test/test.js @@ -71,6 +71,13 @@ t.test('clean: truncates cwd', t => { const expectIndented = utils.join(expectedArray.map(a => ' ' + a)); t.equal(stack.clean(WindowsStack1, 4), expectIndented, 'indentation'); + const multilineWindowsStack = `TypeError: foo\nbar\nbaz\n${WindowsStack1.split('\n').slice(1).join('\n')}`; + t.equal(stack.clean(multilineWindowsStack), expected, 'slices off all lines of leading multiline message'); + + const linuxStackUtils = new StackUtils({cwd: '/user/dev/project', internals: []}); + const multilineLinuxStack = `TypeError: foo\nbar\nbaz\n${LinuxStack1.split('\n').slice(1).join('\n')}`; + t.equal(linuxStackUtils.clean(multilineLinuxStack), expected, 'slices off all lines of leading multiline message on linux'); + t.end(); });