{"record":{"id":"1e884bb44ef7ddbc","repo":"withastro/astro","slug":"middlewarenotaresponse","errorCode":"MiddlewareNotAResponse","errorMessage":"Any data returned from middleware must be a valid `Response` object.","messagePattern":"Any data returned from middleware must be a valid `Response` object\\.","errorType":"exception","errorClass":"AstroError","httpStatus":null,"severity":"error","filePath":"packages/astro/src/core/middleware/callMiddleware.ts","lineNumber":76,"sourceCode":"\t};\n\n\tconst middlewarePromise = onRequest(apiContext, next);\n\n\treturn await Promise.resolve(middlewarePromise).then(async (value) => {\n\t\t// first we check if `next` was called\n\t\tif (nextCalled) {\n\t\t\t/**\n\t\t\t * Then we check if a value is returned. If so, we need to return the value returned by the\n\t\t\t * middleware.\n\t\t\t * e.g.\n\t\t\t * ```js\n\t\t\t * \tconst response = await next();\n\t\t\t * \tconst new Response(null, { status: 500, headers: response.headers });\n\t\t\t * ```\n\t\t\t */\n\t\t\tif (typeof value !== 'undefined') {\n\t\t\t\tif (value instanceof Response === false) {\n\t\t\t\t\tthrow new AstroError(AstroErrorData.MiddlewareNotAResponse);\n\t\t\t\t}\n\t\t\t\treturn value;\n\t\t\t} else {\n\t\t\t\t/**\n\t\t\t\t * Here we handle the case where `next` was called and returned nothing.\n\t\t\t\t */\n\t\t\t\tif (responseFunctionPromise) {\n\t\t\t\t\treturn responseFunctionPromise;\n\t\t\t\t} else {\n\t\t\t\t\tthrow new AstroError(AstroErrorData.MiddlewareNotAResponse);\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (typeof value === 'undefined') {\n\t\t\t/**\n\t\t\t * There might be cases where `next` isn't called and the middleware **must** return\n\t\t\t * something.\n\t\t\t *\n\t\t\t * If not thing is returned, then we raise an Astro error.","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/withastro/astro/blob/d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca/packages/astro/src/core/middleware/callMiddleware.ts#L58-L94","documentation":"Thrown when middleware called next() and then returned a defined value that is not a Response instance. After awaiting next() the middleware may return next()'s Response or a new Response, but returning any other type (object, string, number) is rejected because the runtime must emit an HTTP Response.","triggerScenarios":"A middleware does `await next()` and then `return someObject` or `return 'ok'` where the value is not a Response. The check `value instanceof Response === false` at line 76 of callMiddleware.ts fires for the nextCalled + defined value branch.","commonSituations":"Returning a JSON object or status string instead of `Response.json(...)`; returning locals or a payload by mistake; partial refactor where a helper that used to return a Response now returns data.","solutions":["Wrap returned data in a Response: `return new Response(JSON.stringify(data), { headers: { 'content-type': 'application/json' } })`.","If you only meant to pass through, return the result of next(): `return await next()`.","Return undefined after calling next() to let the downstream Response propagate."],"exampleFix":"// before\nexport const onRequest = async (ctx, next) => {\n  await next();\n  return { ok: true };\n};\n\n// after\nexport const onRequest = async (ctx, next) => {\n  await next();\n  return Response.json({ ok: true });\n};","handlingStrategy":"type-guard","validationCode":"// Ensure middleware returns a Response or undefined (after next).\nexport const onRequest = async (ctx, next) => {\n  await next();\n  const payload = { ok: true };\n  return Response.json(payload); // Response, not raw object\n};","typeGuard":"function isResponse(v: unknown): v is Response {\n  return v instanceof Response;\n}","tryCatchPattern":"try {\n  await callMiddleware(handler, ctx, render);\n} catch (e) {\n  if (e instanceof Error && /valid .Response./i.test(e.message)) {\n    // middleware returned a non-Response; fix the return\n  }\n  throw e;\n}","preventionTips":["After calling next(), return either next()'s result or a new Response.","Wrap data in Response.json() or new Response().","Add a return-type annotation of Response | Promise<Response> to middleware."],"tags":["middleware","response","ssr"],"backgroundTag":null,"analyzedSha":"d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca","analyzedAt":"2026-08-12T13:37:29.035Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}