Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the top line of the Error object #19

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "fs";
import * as path from "path";
import { StackConverter } from "../src/stackconverter";
import { StackConverter } from "../lib/stack-converter";

const TESTING_DIR = "__tests__";
const TESTING_DATA_DIR = path.join(TESTING_DIR, "test-data");
Expand Down Expand Up @@ -50,20 +50,22 @@ describe("StackConverter", () => {

test("source map file that does not exist results stack frame with error message", async () => {
const missingJsFileName = "does-not-exist.js";
const missingJsMapPath = path.join(TESTING_DATA_DIR, `${missingJsFileName}.map`);
const missingJsMapFileName = `${missingJsFileName}.map`
const missingJsMapPath = path.join(TESTING_DATA_DIR, missingJsMapFileName);
const stackConverter = new StackConverter([missingJsMapPath]);
const { error, stack } = await stackConverter.convert(` at hello (${missingJsFileName}:1:1337)`);
expect(error).toBeFalsy();
expect(stack).toMatch(new RegExp(`Error loading source map for frame \\(file ${missingJsMapPath} does not exist or is inaccessible\\)`));
expect(stack).toMatch(new RegExp(`Error loading source map for frame \\(file .*${missingJsMapFileName} does not exist or is inaccessible\\)`));
});

test("empty source map file results stack frame with error message", async () => {
const emptyJsFileName = "empty.js";
const emptyJsMapPath = path.join(TESTING_DATA_DIR, `${emptyJsFileName}.map`);
const emptyJsMapFileName = `${emptyJsFileName}.map`
const emptyJsMapPath = path.join(TESTING_DATA_DIR, emptyJsMapFileName);
const stackConverter = new StackConverter([emptyJsMapPath]);
const { error, stack } = await stackConverter.convert(` at hello (${emptyJsFileName}:1:1337)`);
expect(error).toBeFalsy();
expect(stack).toMatch(new RegExp(`Error loading source map for frame \\(file ${emptyJsMapPath} was empty\\)`));
expect(stack).toMatch(new RegExp(`Error loading source map for frame \\(file .*${emptyJsMapFileName} was empty\\)`));
});

test("source map file that can't be parsed results stack frame with error message", async () => {
Expand All @@ -84,7 +86,8 @@ describe("StackConverter", () => {
const { error, stack } = await stackConverter.convert(stackText);
expect(error).toBeUndefined();
expect(stack).toMatchInlineSnapshot(`
" at <unknown> (dummy.ts:2:31)
"Error: Crush your bugs!
at <unknown> (dummy.ts:2:31)
at <unknown> (dummy.ts:2:31)
at <unknown> (dummy.ts:2:31)
at <unknown> (dummy.ts:2:31)
Expand All @@ -109,7 +112,8 @@ describe("StackConverter", () => {
expect(error).toBeUndefined();
expect(stackText).toEqual(stackText);
expect(stack).toMatchInlineSnapshot(`
" at <unknown> (webpack:///src/app/common/services/bugsplat-custom-error-handler/bugsplat-custom-error-handler.ts:32:16)
"Error: Http failure response for https://app.bugsplat.com/api/subscription.php?database=AutoDb_04102021_95345: 502 OK
at <unknown> (webpack:///src/app/common/services/bugsplat-custom-error-handler/bugsplat-custom-error-handler.ts:32:16)
at Generator.next (<anonymous>)
at next (webpack:///node_modules/tslib/tslib.es6.js:74:70)
at executor (webpack:///node_modules/zone.js/dist/zone-evergreen.js:960:32)
Expand All @@ -131,7 +135,8 @@ describe("StackConverter", () => {
expect(error).toBeUndefined();
expect(stackText).toEqual(stackText);
expect(stack).toMatchInlineSnapshot(`
" at <unknown> (webpack:///src/app/common/services/bugsplat-custom-error-handler/bugsplat-custom-error-handler.ts:32:16)
"Error: Http failure response for https://app.bugsplat.com/api/subscription.php?database=AutoDb_04102021_95345: 502 OK
at <unknown> (webpack:///src/app/common/services/bugsplat-custom-error-handler/bugsplat-custom-error-handler.ts:32:16)
at Generator.next (<anonymous>)
at next (webpack:///node_modules/tslib/tslib.es6.js:74:70)
at executor (webpack:///node_modules/zone.js/dist/zone-evergreen.js:960:32)
Expand Down
14 changes: 14 additions & 0 deletions lib/stack-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export class StackConverter {
const sourceMaps: { [filename: string]: SourceMapConsumer } = {};
const sourceMapErrors: { [filename: string]: boolean } = {};

const errorLine = StackConverter.getChromiumErrorLineOrEmpty(stack);
if (errorLine) {
buff.push(errorLine);
}

for (const frame of stackFrames) {
const { file, methodName, lineNumber, column } = frame;
if (file in sourceMapErrors) {
Expand Down Expand Up @@ -138,6 +143,15 @@ export class StackConverter {
return `${StackConverter.INDENT}at ${method} (${file}:${line}:${column})` + (comment ? ' ***' + comment : '');
}

private static getChromiumErrorLineOrEmpty(stack: string): string {
const parts = stack.split('\n');
if (parts[0].startsWith('Error:')) {
return parts[0];
}

return '';
}

private static async sourceMapFromFile(file: string): Promise<{sourceMap?: SourceMapConsumer, error?: string}> {
if (!file) {
return { error: 'file name was empty' };
Expand Down