diff --git a/lib/beautify-stack.js b/lib/beautify-stack.js index 52959b593..aa044cc7f 100644 --- a/lib/beautify-stack.js +++ b/lib/beautify-stack.js @@ -68,5 +68,8 @@ module.exports = stack => { return stackUtils.clean(stack) // Remove the trailing newline inserted by the `stack-utils` module - .trim(); + .trim() + // Remove remaining file:// prefixes, inserted by `esm`, that are not + // cleaned up by `stack-utils` + .split('\n').map(line => line.replace(/\(file:\/\/([^/].+:\d+:\d+)\)$/, '($1)')).join('\n'); }; diff --git a/lib/serialize-error.js b/lib/serialize-error.js index ffe53db36..6a1aa9847 100644 --- a/lib/serialize-error.js +++ b/lib/serialize-error.js @@ -101,7 +101,9 @@ function trySerializeError(err, shouldBeautifyStack) { } retval.summary = retval.summary.trim(); } else { - retval.summary = lines[0]; + // Skip the source line header inserted by `esm`: + // + retval.summary = lines.find(line => !/:\d+$/.test(line)); } } diff --git a/test/serialize-error.js b/test/serialize-error.js index 35cae7540..67371085b 100644 --- a/test/serialize-error.js +++ b/test/serialize-error.js @@ -176,3 +176,29 @@ test('creates multiline summaries for syntax errors', t => { t.is(serializedError.summary, 'Hello\nThere\nSyntaxError here'); t.end(); }); + +test('skips esm enhancement lines when finding the summary', t => { + const error = new Error(); + Object.defineProperty(error, 'stack', { + value: 'file://file.js:1\nHello' + }); + const serializedError = serialize(error); + t.is(serializedError.summary, 'Hello'); + t.end(); +}); + +test('works around esm\'s insertion of file:// urls', t => { + const fixture = sourceMapFixtures.mapFile('throws'); + try { + fixture.require().run(); + t.fail('Fixture should have thrown'); + } catch (error) { + const expected = serialize(error); + Object.defineProperty(error, 'stack', { + value: error.stack.split('\n').map(line => line.replace('(/', '(file:///')).join('\n') + }); + const serializedError = serialize(error); + t.is(serializedError.source.file, expected.source.file); + t.end(); + } +});