Skip to content

Commit

Permalink
(webdriver): improve error stack for failing bidi commands (#12435)
Browse files Browse the repository at this point in the history
* (webdriver): improve error stack for failing bidi commands

* fix unit test in windows
  • Loading branch information
christian-bromann committed Mar 6, 2024
1 parent ed2bd48 commit 57327cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/webdriver/src/bidi/core.ts
Expand Up @@ -36,6 +36,7 @@ export class BidiCore {

public send (params: Omit<CommandData, 'id'>) {
const id = this.sendAsync(params)
const failError = new Error(`WebDriver Bidi command "${params.method}" failed`)
return new Promise<CommandResponse>((resolve, reject) => {
const t = setTimeout(() => {
reject(new Error(`Request with id ${id} timed out`))
Expand All @@ -50,7 +51,8 @@ export class BidiCore {
h.off('message', listener)
log.info('BIDI RESULT', JSON.stringify(payload))
if (payload.error) {
return reject(new Error(payload.error))
failError.message += ` with error: ${payload.error}`
return reject(failError)
}
resolve(payload)
}
Expand Down
20 changes: 20 additions & 0 deletions packages/webdriver/tests/bidi.test.ts
Expand Up @@ -46,6 +46,26 @@ describe('BidiCore', () => {
const result = await promise
expect(result).toEqual({ id: 1, result: 'foobar' })
})

it('has a proper error stack that contains the line where the command is called', async () => {
const handler = new BidiCore('ws://foo/bar')
handler.connect()
const [, cb] = vi.mocked(handler.socket.on).mock.calls[0]
cb.call(this as any)

const promise = handler.send({ method: 'session.new', params: {} })
const [, messageCallback] = vi.mocked(handler.socket.on).mock.calls[1]
setTimeout(
() => messageCallback.call(this as any, Buffer.from(JSON.stringify({ id: 1, error: 'foobar' }))),
100
)

const error = await promise.catch((err) => err)
const errorMessage = 'WebDriver Bidi command "session.new" failed with error: foobar'
expect(error.stack).toContain(path.join('packages', 'webdriver', 'tests', 'bidi.test.ts:56:'))
expect(error.stack).toContain(errorMessage)
expect(error.message).toBe(errorMessage)
})
})

describe('sendAsync', () => {
Expand Down

0 comments on commit 57327cd

Please sign in to comment.