0

We are making a simple HTTP GET request to an API & noticing that when our HTTP requests error, the response body sometimes has more details then the error message. We are using the axios. We would like to access both the response and the error message from our code, but we are only seeing the error message and are unsure how to access the response. Here is our code:

Calling the API:

import { httpClient } from '../httpClient'; //AxiosInstance

type GetLocations = () => Promise<AxiosResponse<ILocation[]>>

const getLocations: GetLocations = async () => {
    const url = `/Location`;
    return httpClient.get(url);
}

const callAPI = async () => {
    try {
        const axiosResponse = await getLocations();
        console.log(axiosResponse.data);
    catch(error) {
        // error.message has the error message, but we would like access to the http response body as well.
        console.log(error)
    }
}

Some Screenshots:

console.log(error):

console.log(error);

The error object as JSON:

error as JSON

DevTools > Network > Our API Call > Response from same request that errored (has more info):

Http Response from Devtools Network Tab

We would like to access this Incorrect syntax near ','. response from our catch block.

BryceBy
  • 208
  • 5
  • 16
  • 1
    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) – Evert Apr 15 '22 at 23:19
  • That put us down the right path, thank you! Didn't know about error.response from axios! – BryceBy Apr 15 '22 at 23:58

0 Answers0