2024-04-24 12:32:00 +00:00
|
|
|
import { isHttpError, type ApiHttpError } from '@immich/sdk';
|
2023-11-18 04:13:36 +00:00
|
|
|
import type { HandleClientError } from '@sveltejs/kit';
|
|
|
|
|
|
|
|
const DEFAULT_MESSAGE = 'Hmm, not sure about that. Check the logs or open a ticket?';
|
|
|
|
|
2024-04-24 12:32:00 +00:00
|
|
|
const parseHTTPError = (httpError: ApiHttpError) => {
|
2024-02-27 02:48:47 +00:00
|
|
|
const statusCode = httpError?.status || httpError?.data?.statusCode || 500;
|
|
|
|
const message = httpError?.data?.message || (httpError?.data && String(httpError.data)) || httpError?.message;
|
2023-11-18 04:13:36 +00:00
|
|
|
|
2024-02-27 02:48:47 +00:00
|
|
|
console.log({
|
|
|
|
status: statusCode,
|
|
|
|
response: httpError?.data || 'No data',
|
|
|
|
});
|
2023-11-18 04:13:36 +00:00
|
|
|
|
|
|
|
return {
|
2024-02-27 02:48:47 +00:00
|
|
|
message: message || DEFAULT_MESSAGE,
|
|
|
|
code: statusCode,
|
2023-11-18 04:13:36 +00:00
|
|
|
stack: httpError?.stack,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-24 12:32:00 +00:00
|
|
|
const parseError = (error: unknown, status: number, message: string) => {
|
|
|
|
if (isHttpError(error)) {
|
|
|
|
return parseHTTPError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-06-27 13:09:28 +00:00
|
|
|
message: (error as Error)?.message || message || DEFAULT_MESSAGE,
|
2024-04-24 12:32:00 +00:00
|
|
|
code: status,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const handleError: HandleClientError = ({ error, status, message }) => {
|
|
|
|
const result = parseError(error, status, message);
|
2024-06-27 13:09:28 +00:00
|
|
|
console.error(`[hooks.client.ts]:handleError ${result.message}`, error);
|
2023-11-18 04:13:36 +00:00
|
|
|
return result;
|
|
|
|
};
|