If a user makes a request to my API with a payload that is too large, I would like to intercept the error thrown by the server and handle it myself in order to send a more detailed JSON response to the client.
I am using the Express JSON Parser with a custom limit:
router.use(express.json({ limit: "10kb" }));
If a user makes a request over that limit, they currently get an error on the client side and I can't seem to figure out how to try
/catch
on my server in order to replace the error with a JSON response.
EDIT: I sort of figured this out by setting the limit to Infinity
, but I feel like there's probably a better solution. I think that this won't break anything since I am still handling requests that are too large myself.