File size: 1,419 Bytes
aa2e6af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const { logger } = require('~/config');

/**
 * Logs Axios errors based on the error object and a custom message.
 *
 * @param {Object} options - The options object.
 * @param {string} options.message - The custom message to be logged.
 * @param {Error} options.error - The Axios error object.
 */
const logAxiosError = ({ message, error }) => {
  const timedOutMessage = 'Cannot read properties of undefined (reading \'status\')';
  if (error.response) {
    logger.error(
      `${message} The request was made and the server responded with a status code that falls out of the range of 2xx: ${
        error.message ? error.message : ''
      }. Error response data:\n`,
      {
        headers: error.response?.headers,
        status: error.response?.status,
        data: error.response?.data,
      },
    );
  } else if (error.request) {
    logger.error(
      `${message} The request was made but no response was received: ${
        error.message ? error.message : ''
      }. Error Request:\n`,
      {
        request: error.request,
      },
    );
  } else if (error?.message?.includes(timedOutMessage)) {
    logger.error(
      `${message}\nThe request either timed out or was unsuccessful. Error message:\n`,
      error,
    );
  } else {
    logger.error(
      `${message}\nSomething happened in setting up the request. Error message:\n`,
      error,
    );
  }
};

module.exports = logAxiosError;