What happens if the server doesn't send a response body on error, even with Axios?

Question

Grade: Education Subject: Support
What happens if the server doesn't send a response body on error, even with Axios?
Asked by:
82 Viewed 82 Answers

Answer (82)

Best Answer
(442)
If the server doesn't send a response body when an error occurs (e.g., it sends only an error status code like 400 or 500 with no additional data), `error.response` will be null or undefined. Accessing `error.response?.data` in this case will result in `undefined`. You'll need to rely on `error.response?.status` and potentially other properties of the `error` object (like `error.message`) to determine the error and handle it accordingly.