Function getAxiosErrorMessageText

  • Extracts and returns the error message text from an Axios error object.

    This function attempts to retrieve the error message from the response.data.message property of the Axios error object. If the message property is not available, it falls back to the default error message provided by the Error object.

    Parameters

    • error: Error

      The error object to extract the message from. It is expected to be an instance of Error.

    Returns string

    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();