0

I have the following scenario:

The backend detects that the token has expired, so it returns 403 with custom "error" response header.

This can be reproduced by firing the following request:

axios.get(MY_URL)
     .then(res => console.log("I am expecting to enter this block, and do something with the response header"))
     .catch(err => console.log("But instead I skip the Then block and go straight here...));

In Chrome's networking tab, I can indeed see that the header is there in the response:

<Response headers>
content-type: application/json
error: JWT expired at ......... <-- my header

However, it appears that I go straight into the catch block, where I do not have an access to the response and its headers. I was expecting to go into the then block and do some logic, such as:

.then(res => {
  if (res.statusCode === 403 && res.headers['error'] === 'JWT expired at .....') {
    // refresh token...
  }
});

How can I access my response and its headers, when the then block is skipped?

Avaldor
  • 317
  • 2
  • 8
  • 1
    put that logic in the catch block? 403 is an error code, the axios.get will pass that through as an error. If you are wanting to preform logic when an error occurs, thats exactly what a catch block is here for – about14sheep Dec 12 '21 at 00:32
  • But I don't have an access to the response and its headers in the catch block. – Avaldor Dec 12 '21 at 00:35
  • 1
    the response is that you are forbidden, thus the 403. What information are you wanting? have you logged the error out? – about14sheep Dec 12 '21 at 00:38
  • 403 is irrelevant to the question here. The point is that I don't have an access to response headers in the catch block... – Avaldor Dec 12 '21 at 01:19
  • can you log the error and post it here? – about14sheep Dec 12 '21 at 01:22
  • "*I do not have an access to the response and its headers*" - are you sure there? Have you looked into the `err` object that the catch handler is passed? – Bergi Dec 12 '21 at 04:08

0 Answers0