{"record":{"id":"a6adeb84ca5ebf34","repo":"sveltejs/kit","slug":"new-handledhttperror-status-res-status-message","errorCode":null,"errorMessage":"new HandledHttpError({ status: res.status, message: from JSON body, or 'Internal Error' / 'Not Found' for 404 })","messagePattern":"new HandledHttpError\\((.+?)\\)","errorType":"http","errorClass":"HandledHttpError","httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/client/client.js","lineNumber":3699,"sourceCode":"\tconst fetcher = DEV ? dev_fetch : window.fetch;\n\tconst res = await fetcher(data_url.href, {});\n\n\t// detect new deployments from the response header\n\tnotify_version(res.headers.get('x-sveltekit-version'));\n\n\tif (!res.ok) {\n\t\t// if `__data.json` doesn't exist or the server has an internal error,\n\t\t// avoid parsing the HTML error page as a JSON\n\t\t/** @type {App.Error} */\n\t\tlet error = { status: res.status, message: 'Internal Error' };\n\n\t\tif (res.headers.get('content-type')?.includes('application/json')) {\n\t\t\terror = { status: res.status, ...(await res.json()) };\n\t\t} else if (res.status === 404) {\n\t\t\terror.message = 'Not Found';\n\t\t}\n\n\t\tthrow new HandledHttpError(error);\n\t}\n\n\treturn new Promise((resolve, reject) => {\n\t\tprocess_stream(resolve, res).catch(reject);\n\t});\n\n\t// TODO edge case handling necessary? stream() read fails?\n}\n\n/**\n * @param {(value: ServerNodesResponse | ServerRedirectNode) => void} resolve\n * @param {Response} res\n * @returns {Promise<void>}\n */\nasync function process_stream(resolve, res) {\n\tconst reader = /** @type {ReadableStream<Uint8Array>} */ (res.body).getReader();\n\n\t/**","sourceCodeStart":3681,"sourceCodeEnd":3717,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/client/client.js#L3681-L3717","documentation":"When a server data request fails, the client reads the JSON error body and throws a HandledHttpError whose status comes from the response and whose message comes from the body — falling back to 'Internal Error', or 'Not Found' for 404. This surfaces server-side +error/+server.js failures to calling code such as load functions or streamed data consumers.","triggerScenarios":"A __data fetch (or stream resource fetch) responds non-OK: a 404 from a missing endpoint, a 500 from a throwing +server.js handler; non-JSON bodies fall back to fixed messages.","commonSituations":"Endpoint removed or route renamed after deploy; unhandled exceptions in form actions/API routes; API returning HTML error pages (no JSON) so message becomes generic 'Internal Error'.","solutions":["Fix the underlying server endpoint that returned the non-OK status","Add proper error handling in +server.js/+page.server.js and return typed fails","Inspect res.status in the HandledHttpError to distinguish 404 vs 500 causes","Ensure error responses are JSON so meaningful messages reach the client"],"exampleFix":"// before\nconst data = await fetch('/api/items').then((r) => r.json()); // throws HandledHttpError on 500\n// after\ntry {\n  const data = await fetch('/api/items').then((r) => r.json());\n} catch (e) {\n  console.error(e.status, e.message); // e.g. 404 'Not Found'\n  return fallback;\n}","handlingStrategy":"try-catch","validationCode":"// pre-flight: verify endpoint exists (dev-time check)\nasync function assertEndpoint(url) {\n  const r = await fetch(url, { method: 'HEAD' });\n  if (!r.ok) console.warn(`${url} -> ${r.status}`);\n}","typeGuard":"function isHandledHttpError(e) { return e instanceof Error && typeof e.status === 'number'; }","tryCatchPattern":"try { const data = await loadThing(); } catch (e) {\n  if (typeof e.status === 'number') {\n    if (e.status === 404) showNotFound();\n    else showServerError(e.status, e.message);\n  } else { throw e; }\n}","preventionTips":["Wrap data fetches in try/catch and branch on e.status","Return JSON errors from +server.js so messages survive to the client","Add smoke tests hitting every API route to catch 404/500s early"],"tags":["http","network","server-error"],"backgroundTag":"http-request-failed","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}