This client using needle wants to treat 404 as an error. Imagine this is supposed to download an image. Since the request for the image is a 404 the client will want to have error handling code kick in. However since needle does not treat 404s as an error, the response body of the 404 response is done first, before the callback is called. This example shows that the console.log will output the body of the 404 response before the 404 status code can be checked in the callback.
There is no way to tell from the stream that the request is an error. Which means the client treats the 404 response as an image and blows up
What is important is to change the code to use on('response',...) and trigger error handling at this point.
In this example switching the URL back and forth between https://httpstat.us/200 and https://httpstat.us/404 has the expected results. For a 200 the body is processed by the client, for 404 the client receives an error and can do error handling.
It is not clear from the documentation how would one handle errors while attempting to stream.
Consider the following example code
This client using needle wants to treat 404 as an error. Imagine this is supposed to download an image. Since the request for the image is a 404 the client will want to have error handling code kick in. However since needle does not treat 404s as an error, the response body of the 404 response is done first, before the callback is called. This example shows that the console.log will output the body of the 404 response before the 404 status code can be checked in the callback.
There is no way to tell from the stream that the request is an error. Which means the client treats the 404 response as an image and blows up
What is important is to change the code to use
on('response',...)
and trigger error handling at this point.Consider the new example:
In this example switching the URL back and forth between https://httpstat.us/200 and https://httpstat.us/404 has the expected results. For a 200 the body is processed by the client, for 404 the client receives an error and can do error handling.