41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { IGeneralError } from "@/types/error.types";
|
|
|
|
/**
|
|
* استخراج پیام خطا از error object
|
|
* @param error - error object از React Query یا Axios
|
|
* @param fallbackMessage - پیام پیشفرض در صورت عدم وجود خطا
|
|
* @returns پیام خطا
|
|
*/
|
|
export const extractErrorMessage = (
|
|
error: Error | unknown,
|
|
fallbackMessage: string = "خطایی رخ داده است"
|
|
): string => {
|
|
try {
|
|
const axiosError = error as { response?: { data: IGeneralError } };
|
|
return axiosError?.response?.data?.error?.details?.[0] || fallbackMessage;
|
|
} catch {
|
|
return fallbackMessage;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* بررسی اینکه آیا error object معتبر است یا نه
|
|
* @param error - error object
|
|
* @returns boolean
|
|
*/
|
|
export const isValidError = (
|
|
error: unknown
|
|
): error is { response?: { data: IGeneralError } } => {
|
|
return !!(
|
|
typeof error === "object" &&
|
|
error !== null &&
|
|
"response" in error &&
|
|
error.response &&
|
|
typeof error.response === "object" &&
|
|
"data" in error.response &&
|
|
error.response.data &&
|
|
typeof error.response.data === "object" &&
|
|
"error" in error.response.data
|
|
);
|
|
};
|