All files / src/utils errors.ts

95% Statements 19/20
85.71% Branches 6/7
100% Functions 2/2
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 376x 6x             6x 11x 11x 11x       6x 11x 1x 1x     1x     10x 6x   4x 2x   2x 2x   1x      
import { AxiosError } from 'axios';
import { toast } from 'react-toastify';
 
/**
 * 指定されたエラーオブジェクトをコンソールに出力後、画面上にトーストで通知します。
 * @param err エラーオブジェクト
 */
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
export const handleError = (err: any) => {
  console.error(err);
  const message = extractErrorMessage(err);
  toast.error(message);
};
 
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const extractErrorMessage = (err: any) => {
  if (err instanceof AxiosError) {
    const errorInfo = err.response?.data?.error;
    Iif (errorInfo) {
      return `${errorInfo.message} (${errorInfo.code})`;
    } else {
      return `${err.message} (${err.code})`;
    }
  }
  if (err instanceof Error) {
    return err.message;
  }
  if (err === null || err === undefined) {
    return 'unknown exception occurred.';
  }
  try {
    return JSON.stringify(err);
  } catch {
    return String(err);
  }
};