diegosouzapw/OmniRoute · error · RequestBodyTooLargeError
Request body exceeds
Error message
Request body exceeds ${formatBytes(limit)} What it means
RequestBodyTooLargeError thrown by readRequestBodyWithLimit() once the streamed byte count of the request body exceeds the configured limit. Unlike Content-Length-only checks, this fires mid-stream even for chunked or mislabelled requests, protecting the server from memory exhaustion via oversized bodies.
Solutions
- Reduce the request body size (smaller prompt, fewer attachments)
- Increase the configured body-size limit for the route if it is legitimately too low
- Ensure the client sends an accurate Content-Length so oversized requests are rejected earlier
- Return 413 to the client and stop reading the stream immediately upon the error
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at src/shared/middleware/bodySizeGuard.ts:141 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of diegosouzapw/OmniRoute@a179ffed5b (2026-08-25).
Data as JSON: /api/errors/b885ac5e440bb89d.
Report an issue: GitHub.
Appendix: source
Thrown at src/shared/middleware/bodySizeGuard.ts:141
export class RequestBodyTooLargeError extends Error {
constructor(readonly limit: number) {
super(`Request body exceeds ${formatBytes(limit)}`);
this.name = "RequestBodyTooLargeError";
}
}
/**
* Read a request body while enforcing the actual streamed byte count. This closes the gap left by
* Content-Length-only admission for chunked or deliberately mislabelled requests.
*/
export async function readRequestBodyWithLimit(
request: Request,
limit: number
): Promise<Uint8Array<ArrayBuffer>> {
const declaredLength = Number.parseInt(request.headers.get("content-length") || "", 10);
if (!Number.isNaN(declaredLength) && declaredLength > limit) {
throw new RequestBodyTooLargeError(limit);
}
if (!request.body) return new Uint8Array(0);
const reader = request.body.getReader();
const chunks: Uint8Array[] = [];
let total = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
total += value.byteLength;
if (total > limit) {
await reader.cancel("request body too large");
throw new RequestBodyTooLargeError(limit);
}
chunks.push(value);
}
} finally {View on GitHub (pinned to a179ffed5b)