Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/browser-failed-import-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bugsee/browser': patch
---

A failed dynamic `import()` (and other errors a browser reports without a stack) no longer produces a report titled "TypeError at Unknown location". Safari rejects a failed module load with a bare `Importing a module script failed.` that has no stack and no URL; Chrome and Firefox name the module in the message but also ship no frames. The report now carries one frame naming the failed module, taken from the message where the browser gives it and otherwise inferred from the Resource Timing entry of the failed script fetch (same-origin only, flagged as inferred; one fixed `<unknown module>` frame when nothing identifies it, so those occurrences still group). The report description adds the page route (query string dropped), online state and the scripts that had just loaded. JavaScriptCore's own `sourceURL`/`line`/`column` on a stackless SyntaxError or evaluation error becomes a frame as well. A real stack, and the async-origin call site of a platform rejection, always take precedence.
34 changes: 34 additions & 0 deletions packages/browser/src/detection-providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,37 @@ describe('async origin (a stackless platform rejection reported WITH its call si
recorder.stop();
});
});

describe('a failed dynamic import', () => {
// Safari (and Chrome, for this error) ships no stack for it; Node would give the test's own.
const stackless = (message: string): TypeError => {
const error = new TypeError(message);
error.stack = undefined;
return error;
};

function reportOf(reason: unknown) {
const w = fakeWindow();
const requests = started(createUnhandledRejectionProvider(w.win));
w.emit('unhandledrejection', { reason });
return requests[0]?.report as unknown as { description?: string; crash?: CrashJson };
}

it('names the module, and carries a frame, when the message names it (Chrome)', () => {
const report = reportOf(
stackless(
'Failed to fetch dynamically imported module: https://app.example.com/chunk-LAZY.js',
),
);
expect(report.crash?.exception.frames).toHaveLength(1);
expect(report.crash?.exception.frames[0]?.trace).toContain('chunk-LAZY.js');
expect(report.description).toContain('Failed module: https://app.example.com/chunk-LAZY.js');
});

it('still gets one fixed frame when Safari names nothing, so occurrences group', () => {
const report = reportOf(stackless('Importing a module script failed.'));
expect(report.crash?.exception.frames).toHaveLength(1);
expect(report.crash?.exception.frames[0]?.trace).toContain('<unknown module>');
expect(report.description).toContain('Failed module: unknown');
});
});
30 changes: 28 additions & 2 deletions packages/browser/src/detection-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { CrashJson, DetectionProvider, StackFrame } from '@bugsee/core';
import {
applyDebugIds,
buildCrashJson,
composeFrameEnrichers,
DetectionProviderBase,
describeThrown,
formatStack,
Expand All @@ -11,6 +12,11 @@ import {
} from '@bugsee/core';
import { BugseeOption } from '@bugsee/protocol';
import { parseStack } from './stack';
import {
createStacklessFrameEnricher,
describeStacklessContext,
stacklessFrames,
} from './stackless-error-context';

// Browser crash/error detection providers (globalErrorInterceptor on the browser,
// mechanisms). Each subscribes to a window event and submits a ReportingRequest via
Expand Down Expand Up @@ -42,7 +48,8 @@ function describeError(
// No frames of its own (no stack, or only the `Name: message` header Chromium gives an async DOMException): an async platform
// rejection (`video.play()`). Where the call was made, and on what, if the recorder saw it.
const located = describeOrigin(value, origins);
return located === undefined ? { summary } : { summary, description: located };
// Still nothing: a failed dynamic import, or an engine that reports a location instead of a stack (see stackless-error-context).
return withStacklessContext(value, summary, located);
}
// A value that is not an `Error` here may still be a platform rejection (a DOMException from another realm is not `instanceof Error`):
// the recorder keys by reason, so it answers for an object too.
Expand All @@ -55,6 +62,20 @@ function describeError(
: { summary: describeThrown(value), description: located };
}

function withStacklessContext(
value: unknown,
summary: string,
located: string | undefined,
): { summary: string; description?: string } {
const frames = stacklessFrames(value);
const parts = [
located,
frames.length > 0 ? formatStack(frames).trimEnd() : undefined,
describeStacklessContext(value),
].filter((part) => part !== undefined && part !== '');
return parts.length === 0 ? { summary } : { summary, description: parts.join('\n') };
}

function describeOrigin(value: object, origins?: AsyncOriginReporting): string | undefined {
if (origins === undefined) {
return undefined;
Expand All @@ -72,10 +93,15 @@ function describeOrigin(value: object, origins?: AsyncOriginReporting): string |
/** Structured crash.json from a thrown value — `handled: false` (uncaught). A non-Error gets a
* synthetic exception rather than nothing. Uses the browser's multi-engine stack parser. */
function crashOf(value: unknown, origins?: AsyncOriginReporting): CrashJson {
// The async-origin enricher first (a platform rejection's call site), then the stackless context for what is still empty.
const enrichFrames = composeFrameEnrichers([
...(origins !== undefined ? [origins.enrichFrames] : []),
createStacklessFrameEnricher(),
]);
return buildCrashJson(value, {
parseStack,
handled: false,
...(origins !== undefined ? { enrichFrames: origins.enrichFrames } : {}),
...(enrichFrames !== undefined ? { enrichFrames } : {}),
});
}

Expand Down
Loading
Loading