1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-10 13:56:47 +01:00
immich/web/src/lib/utils/handle-error.ts
2023-07-18 23:40:20 -05:00

35 lines
981 B
TypeScript

import type { ApiError } from '@api';
import * as axios from 'axios';
import { notificationController, NotificationType } from '../components/shared-components/notification/notification';
export async function getServerErrorMessage(error: unknown) {
let data = (error as ApiError)?.response?.data;
if (data instanceof Blob) {
const response = await data.text();
try {
data = JSON.parse(response);
} catch {
data = { message: response };
}
}
return data?.message || null;
}
export async function handleError(error: unknown, message: string) {
if (error instanceof axios.CanceledError) {
return;
}
console.error(`[handleError]: ${message}`, error);
let serverMessage = await getServerErrorMessage(error);
if (serverMessage) {
serverMessage = `${String(serverMessage).slice(0, 75)}\n(Immich Server Error)`;
}
notificationController.show({
message: serverMessage || message,
type: NotificationType.Error,
});
}