2

A 404 error is not returned directly as res.status in the axios library but thrown in the catch as an error. My question is now can I somehow query in the catch, which number the error has?

.catch(error => {
if(error.status === 404)     
// do something     
}

Or how can I use the axios library to query if it is a 404 error or not?

const handleLogin = () => {
        axios.post('localhost:4000', {"email":`${email}`, "password":`${password}`})
          .then(res => {
            console.log(res);
            console.log(res.data);
            console.log(res.status);
            if(res.status === 200) {
              setValidationNoExist(true);
               history.push({
                pathname: '/app',
                state: {
                  email: email,
                  name: password,
                },
              })
            }
            else if(res.status === 203) {
              setValidationWrong(false);
            }
            else if(res.status === 404) {
                setValidationPending(false);
            }
             else if(res.status === 204) {
                setValidationNoExist(false);
              }
          })
          .catch(error => {
            console.log(error)
            setValidationNoExist(false);
        })
       };
Kazim
  • 109
  • 2
  • 7
  • Does this answer your question? [How can I get the status code from an http error in Axios?](https://stackoverflow.com/questions/39153080/how-can-i-get-the-status-code-from-an-http-error-in-axios) – thordarson Apr 11 '21 at 17:52

1 Answers1

4

https://www.npmjs.com/package/axios#handling-errors

can you check if error.response.status gives you the value you need in your catch block.

N.K.
  • 464
  • 2
  • 4