ibrod83 / nodejs-file-downloader

129 stars 23 forks source link

Can't reach response on error #24

Closed beryasar closed 2 years ago

beryasar commented 2 years ago

When the request returns 400, I can't reach the detailed message of the error coming from the API? Is it possible for me to access this message somehow?

ibrod83 commented 2 years ago

Can you give me a code example? Because in my tests, I clearly see the error object in case of 400.

beryasar commented 2 years ago

Here is my code:

// url coming another function await (async () => { const downloader = new Downloader({ url: url, directory: "./" }); try { await downloader.download(); console.log("All done"); } catch (error) { console.log("Download failed", error); } })();

In this code sample, when status is 400, I get a try catch as expected, but I also want to reach the response body in this try catch because I want to reach the error message.

ibrod83 commented 2 years ago

When i throw the error object, i do not make a reference to the request/response objects. You're probably used to having it from Axios, It's actually a good idea to implement this. Currently i don't see any way to access those objects when an error is emitted. But why do you actually need it?

beryasar commented 2 years ago

I need this because they have put multiple error messages for 400 error code in the API I am using.

ibrod83 commented 2 years ago

I've added a reference to node's response object, to the Error. Manual handling is still needed