{"record":{"id":"7145d7ed2941b4b0","repo":"SeleniumHQ/selenium","slug":"response-error","errorCode":null,"errorMessage":"${response.error}","messagePattern":"\\$\\{response\\.error\\}","errorType":"exception","errorClass":"WebDriverError","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/bidi/scriptManager.js","lineNumber":330,"sourceCode":"    return response.result.script\n  }\n\n  /**\n   * Removes a preload script.\n   *\n   * @param {string} script - The ID for the script to be removed.\n   * @returns {Promise<any>} - A promise that resolves with the result of the removal.\n   * @throws {WebDriverError} - If an error occurs during the removal process.\n   */\n  async removePreloadScript(script) {\n    const params = { script: script }\n    const command = {\n      method: 'script.removePreloadScript',\n      params,\n    }\n    let response = await this.bidi.send(command)\n    if ('error' in response) {\n      throw new WebDriverError(response.error)\n    }\n    return response.result\n  }\n\n  getCallFunctionParams(\n    targetType,\n    id,\n    sandbox,\n    functionDeclaration,\n    awaitPromise,\n    argumentValueList = null,\n    thisParameter = null,\n    resultOwnership = null,\n  ) {\n    const params = {\n      functionDeclaration: functionDeclaration,\n      awaitPromise: awaitPromise,\n    }","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/bidi/scriptManager.js#L312-L348","documentation":"Thrown by ScriptManager.removePreloadScript() when the BiDi response to script.removePreloadScript contains an 'error' field. Unlike most commands that rely on the transport layer for errors, this method explicitly checks for response.error and wraps it in a WebDriverError. The actual message is the remote-end error string, so the root cause is server-side (the script id was invalid/unknown, or the session rejected the command).","triggerScenarios":"Calling removePreloadScript(scriptId) with an id that was never added, an id that was already removed, an id from a different session, or after the BiDi session was closed.","commonSituations":"Removing a preload script twice; storing the wrong id; calling remove after the context/session that owned it was destroyed; timing issues where the add call failed but the id was cached.","solutions":["Verify the script id is the exact value returned by addPreloadScript and that it has not already been removed.","Track added preload script ids in a Set and guard removal to avoid double-removal.","Ensure the BiDi session and the relevant browsing context are still alive before removing.","Wrap the call in try/catch and treat 'no such script' errors as non-fatal if idempotent removal is acceptable for your flow."],"exampleFix":"// before\nawait scriptManager.removePreloadScript(cachedId) // cachedId may be stale\n\n// after\nconst added = new Set()\nconst id = await scriptManager.addPreloadScript(...)\nadded.add(id)\n// later\nif (added.has(id)) {\n  try { await scriptManager.removePreloadScript(id) } catch (e) { /* already gone */ }\n  added.delete(id)\n}","handlingStrategy":"try-catch","validationCode":"const addedScripts = new Set()\n// after addPreloadScript: addedScripts.add(id)\nif (!addedScripts.has(scriptId)) {\n  throw new Error(`script id ${scriptId} was not added or already removed`)\n}","typeGuard":null,"tryCatchPattern":"try {\n  await scriptManager.removePreloadScript(id)\n  addedScripts.delete(id)\n} catch (e) {\n  if (e instanceof WebDriverError && /no such|unknown script/i.test(e.message)) {\n    addedScripts.delete(id) // already removed, treat as success\n  } else throw e\n}","preventionTips":["Track added preload script ids in a Set and guard removal.","Never remove the same id twice.","Ensure the owning session/context is alive before removal.","Treat 'no such script' as idempotent success in cleanup flows."],"tags":["bidi","script","preload-script","remote-error","javascript"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}