Skip to content
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
4 changes: 2 additions & 2 deletions e2e/cases/browser-logs/react-error/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rspackTest(

await gotoPage(page, rsbuild, '/undefinedError');
await rsbuild.expectLog(
`error [browser] Uncaught TypeError: Cannot read properties of undefined (reading 'name') (src/undefinedError.jsx:5:0)`,
`error [browser] Uncaught TypeError: Cannot read properties of undefined (reading 'name') at ComponentWithUndefinedError (src/undefinedError.jsx:5:0)`,
{ posix: true },
);

Expand All @@ -20,7 +20,7 @@ rspackTest(
await gotoPage(page, rsbuild, '/eventError');
await page.click('button');
await rsbuild.expectLog(
`error [browser] Uncaught TypeError: Cannot read properties of null (reading 'someMethod') (src/eventError.jsx:6:0)`,
`error [browser] Uncaught TypeError: Cannot read properties of null (reading 'someMethod') at handleClick (src/eventError.jsx:6:0)`,
{ posix: true },
);
},
Expand Down
4 changes: 2 additions & 2 deletions e2e/cases/browser-logs/vue-error/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ rspackTest(

await gotoPage(page, rsbuild, '/undefinedError');
await rsbuild.expectLog(
`error [browser] Uncaught TypeError: Cannot read properties of undefined (reading 'name') (src/UndefinedError.vue:2:0)`,
`error [browser] Uncaught TypeError: Cannot read properties of undefined (reading 'name') at Proxy.render (src/UndefinedError.vue:2:0)`,
{ posix: true },
);

await gotoPage(page, rsbuild, '/eventError');
await page.click('button');
await rsbuild.expectLog(
`error [browser] Uncaught TypeError: Cannot read properties of null (reading 'someMethod') (src/EventError.vue:8:0)`,
`error [browser] Uncaught TypeError: Cannot read properties of null (reading 'someMethod') at handleClick (src/EventError.vue:8:0)`,
{ posix: true },
);
},
Expand Down
31 changes: 27 additions & 4 deletions packages/core/src/server/browserLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const findFirstUserFrame = (parsed: StackFrame[]) => {
frame.column !== null &&
frame.lineNumber !== null &&
SCRIPT_REGEX.test(frame.file),
) as { file: string; column: number; lineNumber: number } | undefined;
) as
| (StackFrame &
Pick<Required<StackFrame>, 'file' | 'column' | 'lineNumber'>)
| undefined;
};

const getOriginalPositionForFrame = async (
Expand Down Expand Up @@ -102,7 +105,10 @@ const resolveOriginalLocation = async (
return;
}

return formatOriginalLocation(originalMapping, context);
return {
frame,
location: formatOriginalLocation(originalMapping, context),
};
};

const formatOriginalLocation = (
Expand Down Expand Up @@ -217,12 +223,29 @@ export const formatBrowserErrorLog = async (
if (message.stack) {
switch (stackTrace) {
case 'summary': {
const location = await resolveOriginalLocation(
const resolved = await resolveOriginalLocation(
message.stack,
fs,
context,
);
log += location ? color.dim(` (${location})`) : '';

if (!resolved) {
break;
}

const { frame, location } = resolved;
const { methodName } = frame;

let suffix = '';

// exclude unknown method name and file path like `./src/App.tsx`
if (methodName !== '<unknown>' && !/[\\/]/.test(methodName)) {
suffix += ` at ${methodName}`;
}
if (location) {
suffix += ` (${location})`;
}
log += suffix ? color.dim(suffix) : '';
break;
}
case 'full': {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/config/dev/browser-logs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The browser will throw an error, and Rsbuild will forward this error to the term

```bash
error [browser] Uncaught TypeError: Cannot read properties of undefined (reading 'name')
(src/App.jsx:3:0)
at handleClick (src/App.jsx:3:0)
```

## Disabling
Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/config/dev/browser-logs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const App = () => {

```bash
error [browser] Uncaught TypeError: Cannot read properties of undefined (reading 'name')
(src/App.jsx:3:0)
at handleClick (src/App.jsx:3:0)
```

## 禁用
Expand Down
Loading