{"record":{"id":"f2ce621fb4d9a75b","repo":"denoland/deno","slug":"prefix-key-is-invalid-e-message","errorCode":null,"errorMessage":"${prefix}: ${key} is invalid; ${e.message}","messagePattern":"(.+?): (.+?) is invalid; (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/01_urlpattern.js","lineNumber":268,"sourceCode":"\n    const flags = options.ignoreCase ? \"ui\" : \"u\";\n    const components = [\n      undefined,\n      undefined,\n      undefined,\n      undefined,\n      undefined,\n      undefined,\n      undefined,\n      undefined,\n    ];\n    for (let i = 0; i < 8; ++i) {\n      const key = COMPONENTS_KEYS[i];\n      const c = parsed[key];\n      try {\n        c.regexp = new SafeRegExp(c.regexpString, flags);\n      } catch (e) {\n        throw new TypeError(`${prefix}: ${key} is invalid; ${e.message}`);\n      }\n      components[i] = c;\n    }\n    this[_components] = components;\n  }\n\n  get protocol() {\n    webidl.assertBranded(this, URLPatternPrototype);\n    return this[_components][0].patternString;\n  }\n\n  get username() {\n    webidl.assertBranded(this, URLPatternPrototype);\n    return this[_components][1].patternString;\n  }\n\n  get password() {\n    webidl.assertBranded(this, URLPatternPrototype);","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/01_urlpattern.js#L250-L286","documentation":"Thrown by the URLPattern constructor (prefix: Failed to construct 'URLPattern') when one of the eight parsed components — protocol, username, password, hostname, port, pathname, search, hash — contains a pattern fragment that cannot be compiled into a RegExp with the 'u' (or 'ui' with ignoreCase) flag (ext/web/01_urlpattern.js:266). The native parser (op_urlpattern_parse) accepted the pattern text, but per the URLPattern spec every component is compiled into a regex for matching, and the underlying SyntaxError is rethrown as a TypeError that names the offending component key. The trailing e.message is the raw V8 regexp error, which pinpoints the broken group.","triggerScenarios":"new URLPattern({ pathname: '/users/:id([0-9+' }) (unbalanced group), new URLPattern('https://example.com/:year(\\p{Year') (truncated unicode escape rejected under the 'u' flag), or { hostname: '[[:alpha' } — any component whose group text fails new SafeRegExp(text, 'u'|'ui') at ext/web/01_urlpattern.js:266.","commonSituations":"Porting Express/path-to-regexp route syntax into URLPattern; route tables assembled dynamically from config where group regex fragments are concatenated and a paren or bracket goes unbalanced; regex metacharacters in literal path segments left unquoted; template strings truncating unicode property escapes.","solutions":["Fix the RegExp syntax in the component named in the message (e.g. 'pathname is invalid'): balance group parens in :name(...) patterns and correct or complete any unicode/character-class escape.","Reproduce the underlying error by compiling the group body alone: new RegExp(groupBody, 'u') — the SyntaxError message is the same one Deno reports.","Wrap literal text that contains regex metacharacters like (, ), +, *, ?, [ in URLPattern's {...} literal-segment notation instead of escaping it.","If patterns come from user config, validate each custom group with new RegExp(re, 'u') before constructing the URLPattern and reject bad input with a clear message."],"exampleFix":"// before\nconst p = new URLPattern({ pathname: '/items/:id([0-9]+' });\n// TypeError: Failed to construct 'URLPattern': pathname is invalid; Invalid regular expression: /([0-9+/: Unterminated group\n\n// after\nconst p = new URLPattern({ pathname: '/items/:id([0-9]+)' });","handlingStrategy":"validation","validationCode":"const groupOk = (re) => {\n  try { new RegExp(re, 'u'); return true; } catch { return false; }\n};\nif (!groupOk(cfg.idGroup)) throw new Error(`invalid route group: ${cfg.idGroup}`);\nconst p = new URLPattern({ pathname: `/items/:id(${cfg.idGroup})` });","typeGuard":"const isValidPatternGroup = (re) => {\n  try { new RegExp(re, 'u'); return true; } catch { return false; }\n};","tryCatchPattern":"try {\n  pattern = new URLPattern(input);\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes(\"is invalid;\")) {\n    return respondBadPattern(e.message);\n  }\n  throw e;\n}","preventionTips":["Compile custom group bodies with new RegExp(re, 'u') before embedding them","Use {...} literal segments for text containing regex metacharacters","Centralize URLPattern construction in one helper that validates groups"],"tags":["urlpattern","regexp","url","routing"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}