import toast from "react-hot-toast";

export const handleError = (err: any) => {
    if (err) {
        const errorStatus: number = err?.response?.status;
        const keys = errorStatus === 422 ? Object.keys(err?.response?.data.validation) : [];
        const handle401Error = () => {
            window.localStorage.clear()
            window.location.href = "/auth/login"
        }
        switch (errorStatus) {
            case 422: return toast.error(err?.response?.data?.validation[keys[0]]?.msg);
            case 401: return handle401Error()
            case 500: return toast.error(err?.response?.data?.msg);
            default:
                return toast.error(err?.response?.data?.msg);
        }
    }
}
