{"record":{"id":"6edabe0da10d8577","repo":"SeleniumHQ/selenium","slug":"pattern-must-be-an-instance-of-string-received","errorCode":null,"errorMessage":"Pattern must be an instance of String. Received:'${pattern}'","messagePattern":"Pattern must be an instance of String\\. Received:'(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"javascript/selenium-webdriver/bidi/addInterceptParameters.js","lineNumber":73,"sourceCode":"    patterns.forEach((pattern) => {\n      if (!(pattern instanceof UrlPattern)) {\n        throw new Error(`Pattern must be an instance of UrlPattern. Received:'${pattern}'`)\n      }\n      this.#urlPatterns.push(Object.fromEntries(pattern.asMap()))\n    })\n    return this\n  }\n\n  /**\n   * Adds string URL to intercept.\n   *\n   * @param {string} pattern - The URL pattern to be added.\n   * @returns {AddInterceptParameters} - Returns the instance of AddInterceptParameters for chaining..\n   * @throws {Error} - If the pattern is not an instance of String.\n   */\n  urlStringPattern(pattern) {\n    if (typeof pattern !== 'string') {\n      throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)\n    }\n\n    this.#urlPatterns.push({ type: 'string', pattern: pattern })\n    return this\n  }\n\n  /**\n   * Adds array of string URLs to intercept.\n   * @param {string[]} patterns - An array of URL string patterns.\n   * @returns {this} - Returns the instance of AddInterceptParameters for chaining.\n   */\n  urlStringPatterns(patterns) {\n    patterns.forEach((pattern) => {\n      if (typeof pattern !== 'string') {\n        throw new Error(`Pattern must be an instance of String. Received:'${pattern}'`)\n      }\n      this.#urlPatterns.push({ type: 'string', pattern: pattern })\n    })","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/javascript/selenium-webdriver/bidi/addInterceptParameters.js#L55-L91","documentation":"AddInterceptParameters.urlStringPattern() validates that the single pattern argument is a JS string (typeof === 'string'), then pushes it as { type: 'string', pattern }. This is the simpler alternative to UrlPattern for whole-string URL matching. Passing a UrlPattern instance, a number, an object, null, or undefined fails the typeof check.","triggerScenarios":"Calling urlStringPattern(new UrlPattern()) (wrong method — should be urlPattern). Passing a non-string (number, object). Passing null/undefined. Passing a String object (new String('x')) — note typeof is 'object', so it would fail despite being string-like.","commonSituations":"Confusing urlStringPattern (plain string) with urlPattern (structured UrlPattern). Passing URL objects from the `url` package (typeof 'object'). Forgetting to call .toString() on a value before passing.","solutions":["Pass a primitive string: urlStringPattern('https://example.com/path').","If you have a structured pattern, use urlPattern(new UrlPattern()...) instead.","Convert URL objects with .toString()/.href before passing.","Avoid new String(...) wrappers; use string literals/primitives."],"exampleFix":"// before\nparams.urlStringPattern(new UrlPattern().hostname('example.com'))\n\n// after\nparams.urlStringPattern('https://example.com/*')","handlingStrategy":"type-guard","validationCode":"if (typeof pattern !== 'string') {\n  throw new TypeError('urlStringPattern expects a primitive string')\n}","typeGuard":"function isStringPattern(p) {\n  return typeof p === 'string'\n}","tryCatchPattern":null,"preventionTips":["Pass a primitive string for whole-URL matching.","Use urlPattern(new UrlPattern()...) for structured patterns.","Convert URL objects via .toString()/.href.","Avoid new String(...) wrappers."],"tags":["bidi","network","intercept","type-validation","string"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}