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