豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 1fad330

Browse files
authored
chore: implement formatting of Error console.log arguments (#901)
This PR implements formatting for `SymbolizedError` objects that are part of "resolved arguments". A follow-up PR will handle the actual conversion of `Error` remote objects to `SymbolizedError`s.
1 parent d459266 commit 1fad330

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

src/DevtoolsUtils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ export class SymbolizedError {
292292
}
293293
return details.text;
294294
}
295+
296+
static createForTesting(
297+
message: string,
298+
stackTrace?: DevTools.StackTrace.StackTrace.StackTrace,
299+
cause?: SymbolizedError,
300+
) {
301+
return new SymbolizedError(message, stackTrace, cause);
302+
}
295303
}
296304

297305
export async function createStackTraceForConsoleMessage(

src/formatters/ConsoleFormatter.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ export class ConsoleFormatter {
136136
}
137137

138138
#formatArg(arg: unknown) {
139+
if (arg instanceof SymbolizedError) {
140+
return [
141+
arg.message,
142+
this.#formatStackTrace(arg.stackTrace, /* includeHeading */ false),
143+
]
144+
.filter(line => !!line)
145+
.join('\n');
146+
}
139147
return typeof arg === 'object' ? JSON.stringify(arg) : String(arg);
140148
}
141149

@@ -157,13 +165,15 @@ export class ConsoleFormatter {
157165

158166
#formatStackTrace(
159167
stackTrace: DevTools.DevTools.StackTrace.StackTrace.StackTrace | undefined,
168+
includeHeading = true,
160169
): string {
161170
if (!stackTrace) {
162171
return '';
163172
}
164173

174+
const heading = includeHeading ? ['### Stack trace'] : [];
165175
return [
166-
'### Stack trace',
176+
...heading,
167177
this.#formatFragment(stackTrace.syncFragment),
168178
...stackTrace.asyncFragments.map(this.#formatAsyncFragment.bind(this)),
169179
'Note: line and column numbers use 1-based indexing',

tests/formatters/ConsoleFormatter.test.js.snapshot

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ at schedule (util.ts:5:2)
2525
Note: line and column numbers use 1-based indexing
2626
`;
2727

28+
exports[`ConsoleFormatter > toStringDetailed > formats a console message with an Error object argument 1`] = `
29+
ID: 8
30+
Message: log> JSHandle@error
31+
### Arguments
32+
Arg #0: TypeError: Cannot read properties of undefined
33+
at foo (foo.ts:10:2)
34+
at bar (foo.ts:20:2)
35+
Note: line and column numbers use 1-based indexing
36+
`;
37+
2838
exports[`ConsoleFormatter > toStringDetailed > formats a console.error message 1`] = `
2939
ID: 4
3040
Message: error> Something went wrong

tests/formatters/ConsoleFormatter.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import assert from 'node:assert';
88
import {describe, it} from 'node:test';
99

10+
import {SymbolizedError} from '../../src/DevtoolsUtils.js';
1011
import {ConsoleFormatter} from '../../src/formatters/ConsoleFormatter.js';
1112
import {UncaughtError} from '../../src/PageCollector.js';
1213
import type {ConsoleMessage} from '../../src/third_party/index.js';
@@ -260,6 +261,44 @@ describe('ConsoleFormatter', () => {
260261
).toStringDetailed();
261262
t.assert.snapshot?.(result);
262263
});
264+
265+
it('formats a console message with an Error object argument', async t => {
266+
const message = createMockMessage({
267+
type: () => 'log',
268+
text: () => 'JSHandle@error',
269+
});
270+
const stackTrace = {
271+
syncFragment: {
272+
frames: [
273+
{
274+
line: 10,
275+
column: 2,
276+
url: 'foo.ts',
277+
name: 'foo',
278+
},
279+
{
280+
line: 20,
281+
column: 2,
282+
url: 'foo.ts',
283+
name: 'bar',
284+
},
285+
],
286+
},
287+
asyncFragments: [],
288+
} as unknown as DevTools.StackTrace.StackTrace.StackTrace;
289+
const error = SymbolizedError.createForTesting(
290+
'TypeError: Cannot read properties of undefined',
291+
stackTrace,
292+
);
293+
294+
const result = (
295+
await ConsoleFormatter.from(message, {
296+
id: 8,
297+
resolvedArgsForTesting: [error],
298+
})
299+
).toStringDetailed();
300+
t.assert.snapshot?.(result);
301+
});
263302
});
264303
describe('toJSON', () => {
265304
it('formats a console.log message', async () => {

0 commit comments

Comments
 (0)