{"record":{"id":"e3c614dcbbb87b23","repo":"denoland/deno","slug":"syntaxerror-e3c614","errorCode":"SyntaxError","errorMessage":"e.message","messagePattern":"e\\.message","errorType":"exception","errorClass":"DOMException","httpStatus":null,"severity":"error","filePath":"ext/websocket/01_websocket.js","lineNumber":283,"sourceCode":"    this[_sendQueue] = [];\n    this[_cancelHandle] = undefined;\n\n    const prefix = \"Failed to construct 'WebSocket'\";\n    webidl.requiredArguments(arguments.length, 1, prefix);\n    url = webidl.converters.USVString(url, prefix, \"Argument 1\");\n    initOrProtocols = webidl.converters\n      [\"WebSocketInit or sequence<DOMString> or DOMString\"](\n        initOrProtocols,\n        prefix,\n        \"Argument 2\",\n      );\n\n    let wsURL;\n\n    try {\n      wsURL = new URL(url, getLocationHref());\n    } catch (e) {\n      throw new DOMException(e.message, \"SyntaxError\");\n    }\n\n    if (wsURL.protocol === \"http:\") {\n      wsURL.protocol = \"ws:\";\n    } else if (wsURL.protocol === \"https:\") {\n      wsURL.protocol = \"wss:\";\n    }\n\n    if (wsURL.protocol !== \"ws:\" && wsURL.protocol !== \"wss:\") {\n      throw new DOMException(\n        `Only ws & wss schemes are allowed in a WebSocket URL: received ${wsURL.protocol}`,\n        \"SyntaxError\",\n      );\n    }\n\n    if (wsURL.hash !== \"\" || StringPrototypeEndsWith(wsURL.href, \"#\")) {\n      throw new DOMException(\n        \"Fragments are not allowed in a WebSocket URL\",","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/websocket/01_websocket.js#L265-L301","documentation":"The WebSocket constructor parses url with the WHATWG URL parser relative to the current location (getLocationHref()). If parsing fails, the underlying error's message (typically 'Invalid URL') is rethrown wrapped in a DOMException named SyntaxError - so the message text varies with the URL parser's failure reason.","triggerScenarios":"new WebSocket(''), new WebSocket('ws://[bad'), an out-of-range port (ws://host:99999), or a malformed string built from undefined variables such as `ws://${host}/ws` when host is undefined.","commonSituations":"Forgetting the ws:// scheme, template variables that are undefined/null, unencoded spaces in the URL, empty config values at startup, typos when concatenating parts.","solutions":["Pass an absolute ws:// or wss:// URL string","Validate first with new URL(url) in your own try/catch so you control the error message and stack","URL-encode dynamic path/query components with encodeURIComponent"],"exampleFix":"// before\nnew WebSocket(`${host}/ws`); // host = 'localhost:8080' - no scheme\n\n// after\nnew WebSocket(`ws://${host}/ws`);","handlingStrategy":"validation","validationCode":"function toWsUrl(raw: string): string {\n  const u = new URL(raw); // throws early with your stack, not the constructor's\n  if (u.protocol !== 'ws:' && u.protocol !== 'wss:') {\n    u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:';\n  }\n  return u.href;\n}","typeGuard":"const isAbsoluteWsUrl = (u: string): boolean => /^wss?:\\/\\//i.test(u);","tryCatchPattern":"try {\n  ws = new WebSocket(url);\n} catch (e) {\n  if (e instanceof DOMException && e.name === 'SyntaxError') {\n    // malformed URL - log the raw value and fix the builder\n  } else throw e;\n}","preventionTips":["Construct and log the URL string before passing it","Centralize WebSocket URL building in one helper","Reject empty or relative URLs at config-load time"],"tags":["websocket","url-parsing","syntaxerror","domexception","constructor"],"backgroundTag":"invalid-url","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}