{"record":{"id":"dcc0684613061b24","repo":"n8n-io/n8n","slug":"sse-connection-failed-res-status-text","errorCode":null,"errorMessage":"SSE connection failed (${res.status}): ${text}","messagePattern":"SSE connection failed \\((.+?)\\): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/instance-ai/evaluations/clients/sse-client.ts","lineNumber":40,"sourceCode":" * the response is not a valid SSE stream.\n */\nexport async function consumeSseStream(\n\turl: string,\n\tcookie: string,\n\thandler: (event: SseEvent) => void,\n\tsignal: AbortSignal,\n): Promise<void> {\n\tconst res = await fetch(url, {\n\t\theaders: {\n\t\t\tcookie,\n\t\t\tAccept: 'text/event-stream',\n\t\t},\n\t\tsignal,\n\t});\n\n\tif (!res.ok) {\n\t\tconst text = await res.text();\n\t\tthrow new Error(`SSE connection failed (${res.status}): ${text}`);\n\t}\n\n\tif (!res.body) {\n\t\tthrow new Error('SSE response has no body');\n\t}\n\n\tconst reader = res.body.pipeThrough(new TextDecoderStream()).getReader();\n\n\t// Accumulator for partial lines (SSE data can be split across chunks)\n\tlet buffer = '';\n\n\t// Current event being assembled\n\tlet eventId: string | undefined;\n\tlet eventType: string | undefined;\n\tlet dataLines: string[] = [];\n\n\ttry {\n\t\twhile (true) {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/instance-ai/evaluations/clients/sse-client.ts#L22-L58","documentation":"The SSE client opens a streaming connection to /rest/instance-ai/events/<thread> with cookie auth. If the response is not 2xx, it reads the body and throws with status+text so the caller knows why the stream never opened (typically auth, thread-not-found, or server error).","triggerScenarios":"Cookie missing/expired (401/403); threadId that doesn't exist (404); instance-ai events endpoint disabled (404); server error (5xx).","commonSituations":"Reading the cookie before login; race where the thread is created async and SSE starts before it commits; instance-ai feature flag off.","solutions":["Ensure client.login() ran and the cookie is valid before opening the SSE stream.","Confirm the threadId exists (await ensureThread) before subscribing to its events.","Retry on 5xx with backoff; do not retry on 4xx without fixing the precondition."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (!client.sessionCookie) await client.login();\nawait client.ensureThread(threadId); // ensure thread exists before SSE","typeGuard":null,"tryCatchPattern":"try { await openSse(url, cookie, signal, onEvent); }\ncatch (e) {\n  if (e instanceof Error && e.message.startsWith('SSE connection failed')) { /* classify status, retry only on 5xx */ }\n  else throw e;\n}","preventionTips":["Ensure login + ensureThread before opening the SSE stream.","Retry only transient (5xx/network) failures, not 4xx.","Surface the status from the message to drive branching."],"tags":["sse","network","n8n-api","auth"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}