title: Can I throw error in axios post based on response status
body: <p>Is it possible to throw an error on purpose inside the .then() block in axios? For instance, if the api responds with 204 status code, could I throw an error and run the catch block?</p>
  
  <p>For example:</p>
  
  <pre><code>axios.post('link-to-my-post-service', {
          json-input
      }).then(response =&gt; {
          if (response.status === 200) {
              //proceed...
          }
          else {
              // throw error and go to catch block
          }
      }).catch(error =&gt; {
          //run this code always when status!==200
      });
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried this, but it didn't work:</p>
  
  <pre><code>var instance = axios.create({
              validateStatus: function (status)
              {
                  return status == 200;
              }
          });
  axios.post('link-to-my-post-service', {input: myInput}, instance)
      .then(response =&gt; {
              dispatch({
                      type: "FETCH_SUCCESS",
                      payload: response.data
                  });
          }).catch(error =&gt; {
              dispatch({
                  type: "FETCH_FAILED",
                  payload: error
              });
          });
  </code></pre>
  
  <p>When I get a status code 204, still the executed block is then() block instead of the catch block.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>The correct answer using Ilario's suggestion is this:</p>
  
  <pre><code>var instance = axios.create({
              validateStatus: function (status)
              {
                  return status == 200;
              }
          });
  instance.post('link-to-my-post-service', {input: myInput})
      .then(response =&gt; {
              dispatch({
                      type: "FETCH_SUCCESS",
                      payload: response.data
                  });
          }).catch(error =&gt; {
              dispatch({
                  type: "FETCH_FAILED",
                  payload: error
              });
          });
  </code></pre>
  
  <p>Now when the status code is not equal to 200 the catch block code is executed.</p>