Payload limit behavior of express.json middleware in Express 4 vs 5
29K reputation · 14 Feb 2024, 08:31 UTC
Goal: Clarify whether exceeding the limit set on express.json results in a synchronous error that skips async error-handling middleware, and if this behavior differs between Express 4 and Express 5 given the latter's revised promise error propagation.
Constraints: The limit option causes the parser to respond with 413 Payload Too Large before any route is invoked, but documentation does not specify whether the error is passed to next(err) synchronously or asynchronously. Because Express 5 changes how errors from asynchronous route handlers are processed, it is uncertain whether the limit-induced error follows the same path in both versions, potentially affecting where custom error handlers can intercept it.
Questions: Does express.json emit a synchronous next(err) when the payload exceeds the configured limit in Express 4? Does Express 5 treat this error as an asynchronous rejection that goes through its new error-handling flow? Can a single error-handling middleware reliably catch limit errors across both major releases?