Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions docs/src/api/class-weberror.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@ Unhandled error that was thrown.
- returns: <[string]>

Unhandled error that was thrown.

## method: WebError.location
* since: v1.59
* langs: js, python
- returns: <[Object]>
- `url` <[string]> URL of the resource.
- `lineNumber` <[int]> 0-based line number in the resource.
Comment thread
cpAdm marked this conversation as resolved.
Resolved
- `columnNumber` <[int]> 0-based column number in the resource.

## method: WebError.location
* since: v1.59
Comment thread
cpAdm marked this conversation as resolved.
Resolved
* langs: csharp, java
- returns: <[string]>

URL of the resource followed by 0-based line and column numbers in the resource formatted as `URL:line:column`.
17 changes: 17 additions & 0 deletions packages/playwright-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22075,6 +22075,23 @@ export interface WebError {
*/
error(): Error;

location(): {
/**
* URL of the resource.
*/
url: string;

/**
* 0-based line number in the resource.
*/
lineNumber: number;

/**
* 0-based column number in the resource.
*/
columnNumber: number;
};

/**
* The page that produced this unhandled exception, if any.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
}
this.emit(Events.BrowserContext.Console, consoleMessage);
});
this._channel.on('pageError', ({ error, page }) => {
this._channel.on('pageError', ({ error, page, location }) => {
const pageObject = Page.from(page);
const parsedError = parseError(error);
this.emit(Events.BrowserContext.WebError, new WebError(pageObject, parsedError));
this.emit(Events.BrowserContext.WebError, new WebError(pageObject, parsedError, location));
if (pageObject)
pageObject.emit(Events.Page.PageError, parsedError);
});
Expand Down
11 changes: 10 additions & 1 deletion packages/playwright-core/src/client/webError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

import type { Page } from './page';
import type * as api from '../../types/types';
import type * as channels from '@protocol/channels';

type WebErrorLocation = channels.BrowserContextPageErrorEvent['location'];

export class WebError implements api.WebError {
private _page: Page | null;
private _error: Error;
private _location: WebErrorLocation;

constructor(page: Page | null, error: Error) {
constructor(page: Page | null, error: Error, location: WebErrorLocation) {
this._page = page;
this._error = error;
this._location = location;
}

page() {
Expand All @@ -33,4 +38,8 @@ export class WebError implements api.WebError {
error() {
return this._error;
}

location() {
return this._location;
}
}
5 changes: 5 additions & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,11 @@ scheme.BrowserContextPageEvent = tObject({
scheme.BrowserContextPageErrorEvent = tObject({
error: tType('SerializedError'),
page: tChannel(['Page']),
location: tObject({
url: tString,
lineNumber: tInt,
columnNumber: tInt,
}),
});
scheme.BrowserContextRouteEvent = tObject({
route: tChannel(['Route']),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { WebSocketRouteDispatcher } from './webSocketRouteDispatcher';
import { WritableStreamDispatcher } from './writableStreamDispatcher';
import { createGuid } from '../utils/crypto';
import { deserializeURLMatch, urlMatches } from '../../utils/isomorphic/urlMatch';
import { errorLocationFromStack } from '../../utils/isomorphic/stackTrace';
import { Recorder } from '../recorder';
import { RecorderApp } from '../recorder/recorderApp';
import { ElementHandleDispatcher } from './elementHandlerDispatcher';
Expand Down Expand Up @@ -111,7 +112,11 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
this._dispose();
});
this.addObjectListener(BrowserContext.Events.PageError, (error: Error, page: Page) => {
this._dispatchEvent('pageError', { error: serializeError(error), page: PageDispatcher.from(this, page) });
this._dispatchEvent('pageError', {
error: serializeError(error),
page: PageDispatcher.from(this, page),
location: errorLocationFromStack(error, path.sep, false),
});
});
this.addObjectListener(BrowserContext.Events.Console, (message: ConsoleMessage) => {
const pageDispatcher = PageDispatcher.fromNullable(this, message.page());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Artifact } from '../../artifact';
import { BrowserContext } from '../../browserContext';
import { Dispatcher } from '../../dispatchers/dispatcher';
import { serializeError } from '../../errors';
import { errorLocationFromStack } from '../../../utils/isomorphic/stackTrace';
import { SerializedFS, removeFolders } from '../../utils/fileUtils';
import { HarTracer } from '../../har/harTracer';
import { SdkObject } from '../../instrumentation';
Expand Down Expand Up @@ -591,7 +592,10 @@ export class Tracing extends SdkObject implements InstrumentationListener, Snaps
time: monotonicTime(),
class: 'BrowserContext',
method: 'pageError',
params: { error: serializeError(error) },
params: {
error: serializeError(error),
location: errorLocationFromStack(error, path.sep, false),
},
pageId: page.guid,
};
this._appendTraceEvent(event);
Expand Down
16 changes: 16 additions & 0 deletions packages/playwright-core/src/utils/isomorphic/stackTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ export function parseErrorStack(stack: string, pathSeparator: string, showIntern
return { message, stackLines, location };
}

export function errorLocationFromStack(error: Error | undefined, pathSeparator: string, showInternalStackFrames: boolean = false): {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code works for Node.js (V8), it does not work for errors in WebKit / Firefox.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test passes for all browser. So I am not sure what to do?
image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should plumb the location all the way from the browser-specific protocol event to here. It should be already available in the protcol.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chris (@cpAdm) web error originates from the web page. That web page runs in the browser, it can be Firefox or WebKit. You can't parse Firefox error text in Node and assume it is coming from V8 - stack frame format is different.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, thanks for the feedback!

The error is now plumbed all the way from the browsers. I did need to change the Firefox protocol, so if I understand correctly, this still needs a revision update? But I think that needs to be done via the GitHub workflow?

url: string,
lineNumber: number,
columnNumber: number
} {
const stack = error?.stack || '';
const frame = parseErrorStack(stack, pathSeparator, showInternalStackFrames).location;
if (!frame)
return { url: '', lineNumber: 0, columnNumber: 0 };
return {
url: frame.file,
lineNumber: Math.max(frame.line - 1, 0),
columnNumber: Math.max(frame.column - 1, 0),
};
}

function belongsToNodeModules(file: string, pathSeparator: string) {
return file.includes(`${pathSeparator}node_modules${pathSeparator}`);
}
Expand Down
17 changes: 17 additions & 0 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22075,6 +22075,23 @@ export interface WebError {
*/
error(): Error;

location(): {
/**
* URL of the resource.
*/
url: string;

/**
* 0-based line number in the resource.
*/
lineNumber: number;

/**
* 0-based column number in the resource.
*/
columnNumber: number;
};

/**
* The page that produced this unhandled exception, if any.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,11 @@ export type BrowserContextPageEvent = {
export type BrowserContextPageErrorEvent = {
error: SerializedError,
page: PageChannel,
location: {
url: string,
lineNumber: number,
columnNumber: number,
},
};
export type BrowserContextRouteEvent = {
route: RouteChannel,
Expand Down
6 changes: 6 additions & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,12 @@ BrowserContext:
parameters:
error: SerializedError
page: Page
location:
type: object
properties:
url: string
lineNumber: int
columnNumber: int

route:
parameters:
Expand Down
27 changes: 27 additions & 0 deletions tests/library/browsercontext-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,30 @@ test('weberror event should work', async ({ page }) => {
expect(webError.page()).toBe(page);
expect(webError.error().stack).toContain('boom');
});

test('weberror event should include location', async ({ page, server }) => {
server.setRoute('/error.js', (req, res) => {
res.setHeader('content-type', 'application/javascript');
res.end([
'console.log("a");',
'console.log("b");',
'throw new Error("boom");',
'',
].join('\n'));
});

server.setRoute('/error.html', (req, res) => {
res.setHeader('content-type', 'text/html');
res.end('<script src="/error.js"></script>');
});

const [webError] = await Promise.all([
page.context().waitForEvent('weberror'),
page.goto(server.PREFIX + '/error.html'),
]);

const location = webError.location();
expect(location.url).toBe(`${server.PREFIX}/error.js`);
expect(location.lineNumber).toBe(2);
expect(location.columnNumber).toBeGreaterThan(0); // column number is not consistent across browsers
});