How to receive errors while downloading a file using axios

Viewed 273

My code:

rest client:

return axios.get(url, {
    responseType: 'arraybuffer'
});

handling response:

  const link = document.createElement('a');
            link.href = window.URL.createObjectURL(new Blob([response.data]));
            link.target = "_blank"; 
            link.click();

works nicely if there are no errors on the server side. However, when there is an error in the server API, I get no details of it, because of the response type being arraybuffer.

    .catch((error) => {
        dispatch(showError("An Error Occured", error)); //error does not contain the expected details
    });

I tried not setting the arraybuffer type, instead using the TextDecoder, however the result pdf seems to be corrupt (empty display).

How can I modify the existing setup in order to be able to receive details in case an error occurs?

Edit: Solution: OK, I got it, I can decode the error information from the error.data:

var data:any = JSON.parse(new TextDecoder().decode(error.data));

and then use the error code & message.

0 Answers
Related