Failed to catch and register "Error" response from axios request

I have an axios request in my answer application for which I am following axios npm docs.

This is my axiom request

 axios.post(helper.getLoginApi(), data)
        .then((response) => {
            console.log(response);

            this.props.history.push(from.pathname)
        })
        .catch((error)=> {
            console.log(error);
        })

I can successfully register data on a successful request. However, when I intentionally create an error and try to execute console.log, I do not get the result in return, I just see

POST http: // localhost: 3000 / login 401 (unauthorized): 3000 / login: 1
Error: request failed with status code 401 login.js: 66

in createError (createError.js: 16)

when settling (sett.js: 18)

in XMLHttpRequest.handleLoad (xhr.js: 77)

However, when I go to the "Network" tab in the Chrome console, I can see the answer received below.

enter image description here

Thanks for the help in advance.

+1
1

Github Docs. axios

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  statusText: 'OK',

  // `headers` the headers that the server responded with
  // All header names are lower cased
  headers: {},

  // `config` is the config that was provided to `axios` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance the browser
  request: {}
}

, catch(error => ) catch(response => )

error.response.data, .

console.log(error), , , string toString error.

, ,

axios.post(helper.getLoginApi(), data)
        .then((response) => {
            console.log(response);

            this.props.history.push(from.pathname)
        })
        .catch((error)=> {
            if (error.response) {
              // The request was made and the server responded with a status code
              // that falls out of the range of 2xx
              console.log(error.response.data);
              console.log(error.response.status);
              console.log(error.response.headers);
            } else if (error.request) {
              // The request was made but no response was received
              // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
              // http.ClientRequest in node.js
              console.log(error.request);
            } else {
              // Something happened in setting up the request that triggered an Error
              console.log('Error', error.message);
            }
        })
+6

Source: https://habr.com/ru/post/1684814/


All Articles