MystPi
MystPi9mo ago

`Request` body reading functions: possible error types

This has a section saying:
⚠️ Be aware that the req.text() call can fail if the user hangs up the connection before the body is fully received. Make sure to handle this case. Do note this can happen in all methods that read from the request body, such as req.json(), req.formData(), req.arrayBuffer(), req.body.getReader().read(), req.body.pipeTo(), etc.
I was wondering exactly which error gets thrown in this situation. Is it a NetworkError, or a TypeError, etc. I couldn't find any documentation (even in the spec) regarding what errors these body reading functions can throw. I have a use case that requires knowing exactly why the function failed. Thanks in advance!
HTTP Server APIs | Deno Docs
Deno currently has two HTTP Server APIs:
4 Replies
Leokuma
Leokuma9mo ago
Apparently it's a generic Error when the connection hangs:
Error: error reading a body from connection: end of file before message length reached
at async readableStreamCollectIntoUint8Array (ext:deno_web/06_streams.js:1076:19)
at async consumeBody (ext:deno_fetch/22_body.js:241:9)
at async file:///test.ts:3:3
at async ext:deno_http/00_serve.js:460:18
Error: error reading a body from connection: end of file before message length reached
at async readableStreamCollectIntoUint8Array (ext:deno_web/06_streams.js:1076:19)
at async consumeBody (ext:deno_fetch/22_body.js:241:9)
at async file:///test.ts:3:3
at async ext:deno_http/00_serve.js:460:18
json() throws SyntaxError if it doesn't receive JSON formData() throws TypeError if it doesn't receive form data I think none of this is documented yet
MystPi
MystPi9mo ago
Thank you so much! Is there a way I can reproduce this error manually?
Leokuma
Leokuma9mo ago
I did it on Firefox by using the "network throttle" option or something to simulate a very slow connection. Then I send a reasonably big POST to the Deno webserver and close the FF tab before the payload is entirely sent
MystPi
MystPi9mo ago
That's smart! Def will try this