Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read http errors when responseType is blob in Axios with VueJs?

I'm using blob responseType with Axios in my VueJS app for downloading a document from the server. When the response code is 200 it works fine and download the file but when there is any http error, I'm not able to read the status code when I catch the error because the error is a JSON response.

Has anyone had a similar issue and worked out a way to convert the blob response type to json and thrown an error based on the status code?

I have tried sending the response as a plain text from Laravel backend and tried converting the response to JSON or text in the front-end but no luck.

I have tried reading error response headers but no luck.

Axios({
        url: 'xxxx',
        method: 'GET',
        responseType: 'blob', 
        })
    .then((response) => {
        //code to read the response and create object url with the blob and download the document
    })
    .catch((error) => {
      console.log('Error', error.message); //nothing
      console.log('Error', error.error); //undefined
      console.log('Error', error.data); //undefined

      const blb    = new Blob([error], {type: "text/plain"});
      const reader = new FileReader();

      // This fires after the blob has been read/loaded.
      reader.addEventListener('loadend', (e) => {
        const text = e.srcElement.result;
        console.log(text);
      });
     // Start reading the blob as text.
     reader.readAsText(blb);
});

I just want to throw the error message based on the status code. If it's 401 just want it to be unauthorized and anything else throw it on to the component.

like image 764
Sai Avatar asked May 24 '19 05:05

Sai


People also ask

What is response object in Axios?

Axios Response Objects When we send an HTTP request to a remote server, we get a response with specific information from that server. Axios may be used to retrieve this response. The received response, according to Axios documentation, comprises the following information.

How does Axios handle JSON responses?

When the response's content type is application/json, Axios will automatically try to parse the response into a JavaScript object. Keep in mind that the response headers are sent by the server. So if the server sends back a different content type, you may need to handle it the response yourself.

How do you handle HTTP errors in Axios?

Error Handling If an error occurs while submitting an HTTP request using Axios, the returned error object contains detailed information that will assist us in determining the exact location of the error. There are three places where errors can arise, as seen in the following example.

Why is the response provided as a promise in Axios?

The response is provided as a promise because Axios is promise-based. To obtain the response and catch any errors, we must utilize the then () and catch () functions. Let's look at how we can deal with a POST request's response.


1 Answers

I believe you might be using the error variable in your catch() incorrectly.

Axios passes an error object that has a response property that is where you will want to look for your error or message.

https://github.com/axios/axios#handling-errors

On a side note if you can catch the error server side you could try setting the Content-type header to text/plain. Using either header('Content-Type: plain/text') or Laravel's Response Methods

like image 119
rosscooper Avatar answered Sep 30 '22 13:09

rosscooper