formatted
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
'use client'
|
||||
"use client";
|
||||
import { toast } from "react-toastify";
|
||||
import {
|
||||
errorClientToast,
|
||||
@@ -6,56 +6,55 @@ import {
|
||||
errorServerToast,
|
||||
errorTooManyToast,
|
||||
errorUnauthorizedToast,
|
||||
errorValidationToast
|
||||
errorValidationToast,
|
||||
} from "@/core/components/toasts/error";
|
||||
|
||||
const isServerError = status => status >= 500 && status <= 599;
|
||||
const isClientError = status => status >= 400 && status <= 499;
|
||||
const isServerError = (status) => status >= 500 && status <= 599;
|
||||
const isClientError = (status) => status >= 400 && status <= 499;
|
||||
|
||||
const errorServer = (response, notification, toastContainer) => {
|
||||
if (notification) errorServerToast(toastContainer)
|
||||
}
|
||||
if (notification) errorServerToast(toastContainer);
|
||||
};
|
||||
|
||||
const errorClient = (response, notification, toastContainer) => {
|
||||
switch (response.status) {
|
||||
case 401:
|
||||
if (notification) errorUnauthorizedToast(toastContainer)
|
||||
if (notification) errorUnauthorizedToast(toastContainer);
|
||||
break;
|
||||
case 422:
|
||||
if ('type' in response.data) {
|
||||
if ("type" in response.data) {
|
||||
if (Array.isArray(response.data.message)) {
|
||||
response.data.message.map((item) => {
|
||||
if (notification) errorLogicToast(item, toastContainer)
|
||||
})
|
||||
if (notification) errorLogicToast(item, toastContainer);
|
||||
});
|
||||
} else {
|
||||
if (notification) errorLogicToast(response.data.message, toastContainer)
|
||||
if (notification) errorLogicToast(response.data.message, toastContainer);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (notification) {
|
||||
const errorsMap = Object.keys(response.data.errors)
|
||||
const errorsArray = response.data.errors
|
||||
const errorsMap = Object.keys(response.data.errors);
|
||||
const errorsArray = response.data.errors;
|
||||
|
||||
errorsMap.map((item, index) => {
|
||||
errorValidationToast(errorsArray[item][0], toastContainer);
|
||||
})
|
||||
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 429:
|
||||
if (notification) errorTooManyToast(toastContainer)
|
||||
break
|
||||
if (notification) errorTooManyToast(toastContainer);
|
||||
break;
|
||||
default:
|
||||
if (notification) errorClientToast(toastContainer)
|
||||
break
|
||||
if (notification) errorClientToast(toastContainer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const errorResponse = (response, notification, toastContainer) => {
|
||||
if (notification) toast.dismiss({ container: toastContainer })
|
||||
if (notification) toast.dismiss({ container: toastContainer });
|
||||
if (isServerError(response.status)) {
|
||||
errorServer(response, notification, toastContainer)
|
||||
errorServer(response, notification, toastContainer);
|
||||
} else if (isClientError(response.status)) {
|
||||
errorClient(response, notification, toastContainer)
|
||||
errorClient(response, notification, toastContainer);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user