The error object to extract the message from. It is expected to be an instance of Error.
The extracted error message text.
import axios from 'axios';
import getAxiosErrorMessageText from './get-axios-error-message-text';
async function fetchData() {
try {
const response = await axios.get('https://api.example.com/data');
console.log(response.data);
} catch (error) {
const errorMessage = getAxiosErrorMessageText(error);
console.error('Error fetching data:', errorMessage);
}
}
fetchData();
Extracts and returns the error message text from an Axios error object.
This function attempts to retrieve the error message from the
response.data.messageproperty of the Axios error object. If themessageproperty is not available, it falls back to the default error message provided by theErrorobject.