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/node-module-load-failure-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bugsee/node': patch
---

An uncaught exception or unhandled rejection raised while LOADING a module now names the module that failed, instead of being reported as an error at `node:internal/modules/...` (Node) or with no frames at all (Bun). The failed module becomes the first frame (`import()` or `require()`), and the report description adds the importer, how it was loaded, the error `code` and the CommonJS `require` stack. Node reports a failed ESM/CJS resolution (`ERR_MODULE_NOT_FOUND`, `MODULE_NOT_FOUND`, `ERR_UNSUPPORTED_DIR_IMPORT`, `ERR_PACKAGE_PATH_NOT_EXPORTED`, ...) with the module in the message and in `url`/`requireStack`; Bun (via `@bugsee/bun`, which re-exports this composition) reports a stackless `ResolveMessage` whose `specifier`/`referrer`/`importKind` the SDK now reads. A syntax error becomes a frame at its file, line and column, from Bun's build-error `position` or from the header of Node's stack (present for a CommonJS file and for a missing named export; Node gives no file for a syntax error in a dynamically imported ES module, so that case is unchanged). Bun's stackless missing-named-export SyntaxError names the module from its message. The runtime's own frames are kept after the new first frame, and a caller-supplied frame enricher still runs first.
4 changes: 4 additions & 0 deletions packages/instrumentation-tests/test/spec-drift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ const NOT_A_WIRE_NAME: Readonly<
reason: "the on-disk subdirectory 'capture'",
literals: { capture: 1 },
},
'node/src/module-load-failure.ts': {
reason: "the Node error property 'url' (the URL of the module that failed to load)",
literals: { url: 1 },
},
'node/src/recover-instances.ts': {
reason: "the on-disk subdirectory 'capture'",
literals: { capture: 1 },
Expand Down
48 changes: 48 additions & 0 deletions packages/node/src/detection-providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,51 @@ describe('debug-ids on node crash frames', () => {
});
});
});

describe('a failed module load', () => {
const notFound = () => {
const error = Object.assign(
new Error("Cannot find module '/app/lazy.mjs' imported from /app/main.mjs"),
{ code: 'ERR_MODULE_NOT_FOUND', url: 'file:///app/lazy.mjs' },
);
error.stack = `Error [ERR_MODULE_NOT_FOUND]: ${error.message}\n at finalizeResolution (node:internal/modules/esm/resolve:241:11)`;
return error;
};

it('is reported with the failed module as its first frame and in the description (uncaught)', () => {
const p = fakeProcess();
const requests = started(createUncaughtExceptionProvider(p.proc));
p.emit('uncaughtException', notFound());
const report = requests[0]?.report as unknown as { description: string; crash: CrashJson };
expect(report.crash.exception.frames[0]?.trace).toBe('at import() (/app/lazy.mjs)');
expect(report.description).toContain('Failed module: /app/lazy.mjs');
expect(report.description).toContain('Imported from: /app/main.mjs');
expect(report.description).toContain('finalizeResolution');
});

it('is reported the same way for an unhandled rejection, with no stack at all (bun)', () => {
const error = notFound();
error.stack = undefined;
const p = fakeProcess();
const requests = started(createUnhandledRejectionProvider(p.proc));
p.emit('unhandledRejection', error);
const report = requests[0]?.report as unknown as { description: string; crash: CrashJson };
expect(report.crash.exception.frames).toHaveLength(1);
expect(report.description).toContain('Code: ERR_MODULE_NOT_FOUND');
});

it('keeps the caller-supplied frame enricher, and still adds the module', () => {
const p = fakeProcess();
const seen: number[] = [];
const requests: ReportingRequest[] = [];
createUncaughtExceptionProvider(p.proc, (_error, frames) => {
seen.push(frames.length);
return frames;
}).start(dummyClient, (r) => requests.push(r));
p.emit('uncaughtException', notFound());
expect(seen).toEqual([1]);
expect(
(requests[0]?.report as unknown as { crash: CrashJson }).crash.exception.frames,
).toHaveLength(2);
});
});
21 changes: 16 additions & 5 deletions packages/node/src/detection-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import process from 'node:process';
import type { CrashJson, DetectionProvider } from '@bugsee/core';
import {
buildCrashJson,
composeFrameEnrichers,
DetectionProviderBase,
describeThrown,
type FrameEnricher,
formatStack,
parseV8Stack,
} from '@bugsee/core';
import { BugseeOption } from '@bugsee/protocol';
import { describeModuleLoadFailure, moduleLoadFrameEnricher } from './module-load-failure';
import { markOwnHandler } from './process-policy';

// Node crash/error detection providers (the globalErrorInterceptor equivalent on Node).
Expand All @@ -29,10 +31,14 @@ export interface ProcessEvents {
function describeError(value: unknown): { summary: string; description?: string } {
if (value instanceof Error) {
const summary = value.message || value.name;
if (value.stack) {
return { summary, description: formatStack(parseV8Stack(value.stack)) };
}
return { summary };
// A failed module load: the failed module first (the runtime's own stack is loader internals, or absent in Bun), then whatever
// stack there is.
const moduleFailure = describeModuleLoadFailure(value);
const frames = value.stack ? formatStack(parseV8Stack(value.stack)) : undefined;
const description = [moduleFailure, frames]
.filter((part) => part !== undefined && part !== '')
.join('\n');
return description === '' ? { summary } : { summary, description };
}
return { summary: describeThrown(value) };
}
Expand All @@ -46,10 +52,15 @@ function describeError(value: unknown): { summary: string; description?: string
* could not see it: they drive `logException`, which does go through the client.
*/
function crashOf(value: unknown, enrichFrames?: FrameEnricher): CrashJson | undefined {
// The failed module goes in last, after the locals enricher has seen the runtime's own frames.
const enrich = composeFrameEnrichers([
...(enrichFrames !== undefined ? [enrichFrames] : []),
moduleLoadFrameEnricher,
]);
return buildCrashJson(value, {
parseStack: parseV8Stack,
handled: false,
...(enrichFrames !== undefined ? { enrichFrames } : {}),
...(enrich !== undefined ? { enrichFrames: enrich } : {}),
});
}

Expand Down
Loading
Loading