{"record":{"id":"e5410646c664da01","repo":"denoland/deno","slug":"syntaxerror","errorCode":null,"errorMessage":"SyntaxError","messagePattern":"SyntaxError","errorType":"exception","errorClass":"DOMException","httpStatus":null,"severity":"error","filePath":"ext/fetch/27_eventsource.js","lineNumber":212,"sourceCode":"\n  #headers;\n\n  constructor(url, eventSourceInitDict = { __proto__: null }) {\n    super();\n    this[webidl.brand] = webidl.brand;\n    const prefix = \"Failed to construct 'EventSource'\";\n    webidl.requiredArguments(arguments.length, 1, prefix);\n    url = webidl.converters.USVString(url, prefix, \"Argument 1\");\n    eventSourceInitDict = webidl.converters.EventSourceInit(\n      eventSourceInitDict,\n      prefix,\n      \"Argument 2\",\n    );\n\n    try {\n      url = new URL(url, getLocationHref()).href;\n    } catch (e) {\n      throw new DOMException(e.message, \"SyntaxError\");\n    }\n\n    this.#url = url;\n    this.#withCredentials = eventSourceInitDict.withCredentials;\n    this.#headers = eventSourceInitDict.headers;\n\n    this.#loop();\n  }\n\n  close() {\n    webidl.assertBranded(this, EventSourcePrototype);\n    this.#abortController.abort();\n    this.#readyState = CLOSED;\n    if (this.#reconnectionTimerId) core.cancelTimer(this.#reconnectionTimerId);\n  }\n\n  async #loop() {\n    const lastEventIdValue = this.#lastEventId;","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/fetch/27_eventsource.js#L194-L230","documentation":"The EventSource constructor resolves its first argument against the current location with the URL parser. If parsing fails, the URL constructor's message is wrapped into a DOMException named 'SyntaxError'. This happens before any connection is attempted, so it is purely an invalid-URL error, not a network error.","triggerScenarios":"new EventSource(\"not a url\"), URLs with unencoded spaces or invalid percent-encoding, or a relative URL when there is no usable base location for the worker.","commonSituations":"Passing a bare path like \"/events\" where no document base exists; forgetting encodeURIComponent on dynamic path segments; copy-pasting a URL with quotes or whitespace; constructing URLs from user input unvalidated.","solutions":["Pass a fully-qualified absolute URL: new EventSource(\"https://example.com/events\")","Build the URL first with new URL(path, base) and pass .href so failures surface at the construction site","encodeURIComponent() every dynamic segment that may contain spaces or non-ASCII characters"],"exampleFix":"// before\nconst es = new EventSource(`/events?room=${roomName}`); // SyntaxError for names with spaces\n\n// after\nconst es = new EventSource(`/events?room=${encodeURIComponent(roomName)}`);","handlingStrategy":"validation","validationCode":"function toAbsoluteEventSourceUrl(input, base) {\n  try {\n    return new URL(input, base).href; // throws on invalid input\n  } catch {\n    throw new Error(`invalid EventSource URL: ${JSON.stringify(input)}`);\n  }\n}\nconst es = new EventSource(toAbsoluteEventSourceUrl(path, location.href));","typeGuard":"function isParsableUrl(input: string, base?: string): boolean {\n  try {\n    new URL(input, base);\n    return true;\n  } catch {\n    return false;\n  }\n}","tryCatchPattern":"let es;\ntry {\n  es = new EventSource(url);\n} catch (err) {\n  if (err instanceof DOMException && err.name === \"SyntaxError\") {\n    // fix the URL string; this is not a network failure\n    throw new Error(`bad EventSource URL: ${url}`);\n  }\n  throw err;\n}","preventionTips":["Always pass an absolute URL to EventSource","Build URLs with new URL(path, base).href so parsing errors surface early with context","encodeURIComponent all dynamic query/segment values","Distinguish DOMException SyntaxError (bad URL) from later connection errors when reporting"],"tags":["eventsource","sse","url","dom-exception"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}