Skip to content

[js][bidi] Throw WebDriverError from generated BiDi modules - #18360

Open
pujagani wants to merge 3 commits into
SeleniumHQ:trunkfrom
pujagani:add-error-js
Open

pujagani wants to merge 3 commits into
SeleniumHQ:trunkfrom
pujagani:add-error-js

Conversation

@pujagani

@pujagani pujagani commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

🔗 Related Issues

The BiDi low-level behavioral contract ADR says an error response must error for the reason the remote end provides, unrecognized error codes included.

💥 What does this PR do?

When a command fails, the generated JavaScript BiDi modules now throw a typed WebDriverError for the BiDi error code instead of a plain Error.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s): Claude Code
    • What was generated: Domain#send error change and tests
    • I reviewed all AI output and can explain the change

🔄 Types of changes

  • Bug fix (backwards compatible)

@selenium-ci

Copy link
Copy Markdown
Member

/agentic_describe

@selenium-ci selenium-ci added C-nodejs JavaScript Bindings B-build Includes scripting, bazel and CI integrations B-devtools Includes everything BiDi or Chrome DevTools related labels Nov 20, 2026
@qodo-code-review

qodo-code-review Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Throw typed WebDriver errors from generated JavaScript BiDi modules

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Replace plain errors from generated BiDi commands with WebDriverError types matching remote error
 codes.
• Preserve wire codes, existing error messages, and remote stacktraces, including for unrecognized
 codes.
• Add unit and browser tests for shared, BiDi-only, and unknown errors.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed Domain.send path is the common error boundary for generated BiDi domain commands.
  • Error construction, code preservation, and downstream caller exception matching are one bounded BiDi integration behavior rather than separate capabilities for each generated domain or technical layer.
Sources

javascript/selenium-webdriver/bidi/domain.js[61-65]

  async send(method, params) {
    const response = await this.#bidi.send({ method, params })
    if (response?.error !== undefined) {
      throw errorFromResponse(response)
    }
Behavioral complexity 🟡 Medium 🧩 Compatibility cases, Interacting branches

  • Before: every BiDi error response from Domain.send was converted to a generic Error whose message contained the wire code and message.
  • After: the same response must select a classic WebDriver error type when codes overlap, create a dedicated type for BiDi-only spec codes, fall back to WebDriverError for unknown codes, preserve the legacy message, attach the wire code, and conditionally attach a remote stacktrace.
  • New correctness conditions: precedence between classic mappings and generated BiDi codes must remain stable; lazy initialization must avoid the domain/error module cycle; unknown codes must remain safely representable; and optional stacktrace handling must not alter the established message or rejection behavior.
  • The branches are compatibility behavior introduced by this PR, not merely reuse of an unchanged mapping helper.
Potential damage 🟡 Medium 🩹 Recoverable

  • A wrong type or missing diagnostic can cause callers that use instanceof or the wire code to select the wrong recovery path, and a conversion failure can make the BiDi command appear unavailable.
  • The consequence is confined to client-side command error handling: there is no persistent state mutation or external compensation obligation in this conversion path.
  • Reverting the client code restores the old generic exception behavior, while recovery from a typed-error mismatch requires retrying or caller-side handling; any remote operation represented by the error remains governed by the remote endpoint.
  • Equal business importance is assumed because no capability-specific declaration was supplied.
  • Recoverable by updating/reverting the client binding or handling the returned rejection; no persistent state repair is indicated.
  • If the remote command failed after a partial remote-side effect, that effect is outside this client-side conversion change and is not established by the inspected path.
Sources

javascript/selenium-webdriver/bidi/domain.js[61-65]

  async send(method, params) {
    const response = await this.#bidi.send({ method, params })
    if (response?.error !== undefined) {
      throw errorFromResponse(response)
    }
Assessed commit a94d712
Diagram

graph TD
  G["Generated domains"] --> D["Domain send"] --> R{"Error response?"} -->|yes| M["BiDi error mapping"] --> W["WebDriverError"]
  R -->|no| S["Command result"]
  P["Spec error codes"] --> M
  C["Classic error types"] --> M
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Generate a static BiDi error registry
  • ➕ Makes BiDi-specific classes explicit and avoids constructing them on first use.
  • ➖ Adds generator work and another generated artifact to maintain alongside the error-code enum.

Recommendation: Keep the lazy registry: it reuses existing classic errors and derives BiDi-only types from the generated spec codes without maintaining a second manual list. A generated static registry is worth considering only if these types need an explicitly declared public API.

Files changed (7) +221 / -2

Bug fix (2) +78 / -1
domain.jsThrow mapped errors from shared BiDi command handling +2/-1

Throw mapped errors from shared BiDi command handling

• Domain.send now converts error responses through the BiDi error mapper instead of throwing a plain Error. Successful responses continue to return their result.

javascript/selenium-webdriver/bidi/domain.js

error.jsMap BiDi error codes to WebDriverError types +76/-0

Map BiDi error codes to WebDriverError types

• Lazily reuses classic error classes for shared codes and creates stable subclasses for BiDi-only spec codes. Unknown codes use WebDriverError; constructed errors retain the wire code, existing message format, and optional remote stacktrace.

javascript/selenium-webdriver/bidi/error.js

Tests (4) +142 / -1
domain_error_test.jsCover typed BiDi error responses +119/-0

Cover typed BiDi error responses

• Tests classic, BiDi-only, and unknown code mapping, along with message and stacktrace preservation. Also checks coverage of generated spec codes and the unchanged success path.

javascript/selenium-webdriver/test/bidi/domain_error_test.js

browsing_context_test.jsAssert typed error for an unknown browsing context +5/-0

Assert typed error for an unknown browsing context

• Adds a browser test expecting NoSuchFrameError when closing a nonexistent context.

javascript/selenium-webdriver/test/bidi/generated/browsing_context_test.js

network_test.jsAssert the error type for a missing network intercept +5/-1

Assert the error type for a missing network intercept

• Changes the removal test to check the BiDi-specific error class rather than matching message text.

javascript/selenium-webdriver/test/bidi/generated/network_test.js

script_test.jsTest typed script-command failures +13/-0

Test typed script-command failures

• Adds browser assertions for NoSuchFrameError on an unknown evaluation context and the BiDi-specific type for an unknown preload script.

javascript/selenium-webdriver/test/bidi/generated/script_test.js

Other (1) +1 / -0
BUILD.bazelRegister BiDi error unit tests +1/-0

Register BiDi error unit tests

• Adds domain_error_test.js to the small Bazel test suite.

javascript/selenium-webdriver/BUILD.bazel

@qodo-code-review

qodo-code-review Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Unknown BiDi codes can get classic types 🐞 Bug ≡ Correctness ⭐ New
Description
types() registers every classic WebDriver error code before checking which codes belong to the
BiDi schema. If a remote end returns a code such as no such cookie, which is absent from BiDi's
ErrorCode, Domain.send() throws NoSuchCookieError instead of the generic WebDriverError used
for other unrecognized codes.
Code

javascript/selenium-webdriver/bidi/error.js[R32-33]

+      const { error: code } = error.encodeError(new type())
+      if (!typesByCode.has(code)) typesByCode.set(code, type)
Evidence
The new loop inserts all encoded classic error codes into the map, and errorType() returns a
mapped class before its generic fallback. Classic's mapping includes no such cookie, but the BiDi
schema's ErrorCode list does not.

javascript/selenium-webdriver/bidi/error.js[26-40]
javascript/selenium-webdriver/bidi/error.js[48-56]
javascript/selenium-webdriver/lib/error.js[445-477]
common/bidi/schema.json[1744-1774]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Classic-only error codes are registered as BiDi error types, so an unrecognized BiDi code can receive a misleading classic error class.

## Fix Focus Areas
- javascript/selenium-webdriver/bidi/error.js[26-40]

## Recommended Fix
Load the BiDi `ErrorCode` values before populating the map, and register classic error classes only when their encoded code is among those values. Continue defining dedicated classes for the remaining BiDi codes and using `WebDriverError` for codes outside the schema.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


2. Subscription failures remain plain errors 🐞 Bug ≡ Correctness
Description
Domain.send() now wraps wire-level failures in WebDriverError, but the inherited
Domain.addCallback() path delegates subscription failures to Index.addCallback() and
Index.removeCallback(), which still throw plain Error. A caller using a generated domain
receives different error types for failed commands and failed subscriptions, even though both paths
handle the same wire-error response shape.
Code

javascript/selenium-webdriver/bidi/domain.js[64]

+      const err = new WebDriverError(`${response.error}: ${response.message}`)
Evidence
The changed command path constructs a WebDriverError. Domain.addCallback() delegates to the
connection, whose subscription and unsubscription error checks still construct plain Error
instances; its comment explicitly describes the subscription check as matching Domain.send().

javascript/selenium-webdriver/bidi/domain.js[61-68]
javascript/selenium-webdriver/bidi/domain.js[82-89]
javascript/selenium-webdriver/bidi/index.js[407-416]
javascript/selenium-webdriver/bidi/index.js[467-478]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Generated-domain commands now reject with `WebDriverError`, but subscription and unsubscription failures reached through the same domain still reject with plain `Error`.

## Fix Focus Areas
- javascript/selenium-webdriver/bidi/domain.js[61-69]
- javascript/selenium-webdriver/bidi/index.js[403-416]
- javascript/selenium-webdriver/bidi/index.js[467-478]

## Recommended Fix
Use a shared wire-error conversion for domain commands, subscription and unsubscription responses. Preserve the remote error code, message and string stacktrace in all three paths, and add tests for subscription failures.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


3. Error-type check runs in a slow browser test ✓ Resolved

Resolving commit: 39b6503

Resolution reason
N/A
Description
The new rejects closing an unknown context with a WebDriverError case in the browser-driven
browsing_context_test.js only asserts that the rejection is a WebDriverError with a `no such
frame: message prefix. The new unit file domain_error_test.js` already covers that same mapping
(error code and message formatting, and the WebDriverError constructor check) without launching a
browser. The duplicate adds browser run time and flakiness risk to the generated-module suite
without covering new behavior.

Grey Divider

Context sources
Review mode: Auto: ⚖️ Balanced: Central BiDi error mapping changes runtime behavior across generated commands.

Grey Divider

Tip of the day
💡 Did you know, you can keep summaries lean with Findings visible per group, which tucks the rest behind a View link

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit a94d712

Results up to commit 758cf77 ⚖️ Balanced


🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)


Remediation recommended
1. Subscription failures remain plain errors 🐞 Bug ≡ Correctness
Description
Domain.send() now wraps wire-level failures in WebDriverError, but the inherited
Domain.addCallback() path delegates subscription failures to Index.addCallback() and
Index.removeCallback(), which still throw plain Error. A caller using a generated domain
receives different error types for failed commands and failed subscriptions, even though both paths
handle the same wire-error response shape.
Code

javascript/selenium-webdriver/bidi/domain.js[64]

+      const err = new WebDriverError(`${response.error}: ${response.message}`)
Evidence
The changed command path constructs a WebDriverError. Domain.addCallback() delegates to the
connection, whose subscription and unsubscription error checks still construct plain Error
instances; its comment explicitly describes the subscription check as matching Domain.send().

javascript/selenium-webdriver/bidi/domain.js[61-68]
javascript/selenium-webdriver/bidi/domain.js[82-89]
javascript/selenium-webdriver/bidi/index.js[407-416]
javascript/selenium-webdriver/bidi/index.js[467-478]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Generated-domain commands now reject with `WebDriverError`, but subscription and unsubscription failures reached through the same domain still reject with plain `Error`.

## Fix Focus Areas
- javascript/selenium-webdriver/bidi/domain.js[61-69]
- javascript/selenium-webdriver/bidi/index.js[403-416]
- javascript/selenium-webdriver/bidi/index.js[467-478]

## Recommended Fix
Use a shared wire-error conversion for domain commands, subscription and unsubscription responses. Preserve the remote error code, message and string stacktrace in all three paths, and add tests for subscription failures.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


2. Error-type check runs in a slow browser test ✓ Resolved

Resolving commit: 39b6503

Resolution reason
N/A
Description
The new rejects closing an unknown context with a WebDriverError case in the browser-driven
browsing_context_test.js only asserts that the rejection is a WebDriverError with a `no such
frame: message prefix. The new unit file domain_error_test.js` already covers that same mapping
(error code and message formatting, and the WebDriverError constructor check) without launching a
browser. The duplicate adds browser run time and flakiness risk to the generated-module suite
without covering new behavior.

Grey Divider

Qodo Logo

const response = await this.#bidi.send({ method, params })
if (response?.error !== undefined) {
throw new Error(`${response.error}: ${response.message}`)
const err = new WebDriverError(`${response.error}: ${response.message}`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remediation recommended

2. Subscription failures remain plain errors 🐞 Bug ≡ Correctness

Domain.send() now wraps wire-level failures in WebDriverError, but the inherited
Domain.addCallback() path delegates subscription failures to Index.addCallback() and
Index.removeCallback(), which still throw plain Error. A caller using a generated domain
receives different error types for failed commands and failed subscriptions, even though both paths
handle the same wire-error response shape.
Agent Prompt
## Issue description
Generated-domain commands now reject with `WebDriverError`, but subscription and unsubscription failures reached through the same domain still reject with plain `Error`.

## Fix Focus Areas
- javascript/selenium-webdriver/bidi/domain.js[61-69]
- javascript/selenium-webdriver/bidi/index.js[403-416]
- javascript/selenium-webdriver/bidi/index.js[467-478]

## Recommended Fix
Use a shared wire-error conversion for domain commands, subscription and unsubscription responses. Preserve the remote error code, message and string stacktrace in all three paths, and add tests for subscription failures.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

@pujagani
pujagani marked this pull request as draft November 20, 2026 08:35
Comment on lines +32 to +33
const { error: code } = error.encodeError(new type())
if (!typesByCode.has(code)) typesByCode.set(code, type)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remediation recommended

1. Unknown bidi codes can get classic types 🐞 Bug ≡ Correctness

types() registers every classic WebDriver error code before checking which codes belong to the
BiDi schema. If a remote end returns a code such as no such cookie, which is absent from BiDi's
ErrorCode, Domain.send() throws NoSuchCookieError instead of the generic WebDriverError used
for other unrecognized codes.
Agent Prompt
## Issue description
Classic-only error codes are registered as BiDi error types, so an unrecognized BiDi code can receive a misleading classic error class.

## Fix Focus Areas
- javascript/selenium-webdriver/bidi/error.js[26-40]

## Recommended Fix
Load the BiDi `ErrorCode` values before populating the map, and register classic error classes only when their encoded code is among those values. Continue defining dedicated classes for the remaining BiDi codes and using `WebDriverError` for codes outside the schema.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

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.

I agree with Qodo here

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 39b6503

@pujagani
pujagani marked this pull request as ready for review November 20, 2026 09:49
@selenium-ci

Copy link
Copy Markdown
Member

/agentic_review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-build Includes scripting, bazel and CI integrations B-devtools Includes everything BiDi or Chrome DevTools related C-nodejs JavaScript Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants