FAQ: how do I stop axios from throwing an error on 4XX / 5XX requests?

By default, the axios HTTP library throws an error anytime the destination server responds with a 4XX / 5XX error (for example, a 400 Bad Request). Since axios raises an error, your workflow will stop at this step.

If you want to handle the error response in a custom way, and have axios not throw an error in this condition, use the validateStatus function in the axios config. From the axios docs:

// `validateStatus` defines whether to resolve or reject the promise for a given
// HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
// or `undefined`), the promise will be resolved; otherwise, the promise will be
// rejected.
validateStatus: function (status) {
  return status >= 200 && status < 300; // default
},

If you never want axios to throw an error, you can have the function always return true:

const axios = require("axios")

await axios({
  method: "GET",
  url: "https://example.com",
  validateStatus: () => true,
})

See the axios docs for more information.

1 Like