2024-02-27 03:48:47 +01:00
|
|
|
import { isHttpError } from '@immich/sdk';
|
|
|
|
|
import { isAxiosError } from 'axios';
|
2024-02-14 11:24:18 -05:00
|
|
|
import { notificationController, NotificationType } from '../components/shared-components/notification/notification';
|
2022-12-26 10:35:52 -05:00
|
|
|
|
2023-07-15 00:03:56 -04:00
|
|
|
export async function getServerErrorMessage(error: unknown) {
|
2024-02-27 03:48:47 +01:00
|
|
|
if (isHttpError(error)) {
|
|
|
|
|
return error.data?.message || error.data;
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
2023-06-30 12:24:28 -04:00
|
|
|
|
2024-02-27 03:48:47 +01:00
|
|
|
if (isAxiosError(error)) {
|
|
|
|
|
let data = error.response?.data;
|
|
|
|
|
if (data instanceof Blob) {
|
|
|
|
|
const response = await data.text();
|
|
|
|
|
try {
|
|
|
|
|
data = JSON.parse(response);
|
|
|
|
|
} catch {
|
|
|
|
|
data = { message: response };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data?.message;
|
|
|
|
|
}
|
2023-07-15 00:03:56 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-27 08:37:37 -08:00
|
|
|
export function handleError(error: unknown, message: string) {
|
2024-02-14 11:24:18 -05:00
|
|
|
if ((error as Error)?.name === 'AbortError') {
|
2023-07-15 00:03:56 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-16 16:31:22 -05:00
|
|
|
console.error(`[handleError]: ${message}`, error, (error as Error)?.stack);
|
2023-07-15 00:03:56 -04:00
|
|
|
|
2024-02-27 08:37:37 -08:00
|
|
|
getServerErrorMessage(error)
|
|
|
|
|
.then((serverMessage) => {
|
|
|
|
|
if (serverMessage) {
|
|
|
|
|
serverMessage = `${String(serverMessage).slice(0, 75)}\n(Immich Server Error)`;
|
|
|
|
|
}
|
2023-01-13 17:04:59 -05:00
|
|
|
|
2024-02-27 08:37:37 -08:00
|
|
|
notificationController.show({
|
|
|
|
|
message: serverMessage || message,
|
|
|
|
|
type: NotificationType.Error,
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
});
|
2022-12-26 10:35:52 -05:00
|
|
|
}
|