This commit is contained in:
azizziy
2025-05-20 17:02:10 +05:00
commit c01e852a59
257 changed files with 27766 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
import { AxiosError } from 'axios';
import get from 'lodash.get';
import toast from 'react-hot-toast';
export async function tryToGetErrorMessage(error: unknown): Promise<string> {
if (error instanceof Error || error instanceof AxiosError || typeof error === 'string') {
try {
if (error instanceof AxiosError) {
const errMsg = get(error, 'response.data.message');
if (typeof errMsg === 'string') {
return errMsg;
} else if (error instanceof AxiosError && get(error, 'response.data.type') === 'application/json') {
const jsonBlob = get(error, 'response.data');
const responseData = JSON.parse(await jsonBlob.text());
const parsedErrText = get(responseData, 'message');
return parsedErrText;
}
}
} catch (err) {
console.error('tryToGetErrorMessage error happened: ', err);
}
return error.toString();
} else {
return '';
}
}
function notifySuccess(msg: string) {
toast.success(msg.toString());
}
async function notifyError(msg: string | Error | AxiosError) {
const errorText = await tryToGetErrorMessage(msg);
toast.error(errorText);
// try {
// if (msg instanceof AxiosError) {
// const errMsg = get(msg, 'response.data.message');
// if (typeof errMsg === 'string') {
// toast.error(errMsg);
// return;
// } else if (msg instanceof AxiosError && get(msg, 'response.data.type') === 'application/json') {
// const jsonBlob = get(msg, 'response.data');
// const responseData = JSON.parse(await jsonBlob.text());
// const parsedErrText = get(responseData, 'message');
// toast.error(parsedErrText);
// return;
// }
// }
// } catch (_) {}
// toast.error(msg.toString());
}
function notifyUnknownError(msg: unknown) {
if (msg instanceof Error || msg instanceof AxiosError || typeof msg === 'string') {
notifyError(msg);
} else {
console.error('UNKNOWN ERROR: ', msg);
}
}
export { notifySuccess, notifyError, notifyUnknownError };