fix(web): fetch error reporting (#7391)

This commit is contained in:
Michel Heusschen 2024-02-27 03:48:47 +01:00 committed by GitHub
parent 8a05ff51e9
commit c8bdeb8fec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 43 additions and 32 deletions

View file

@ -1,34 +1,22 @@
import { isHttpError } from '@immich/sdk';
import type { HandleClientError } from '@sveltejs/kit';
import type { AxiosError, AxiosResponse } from 'axios';
const LOG_PREFIX = '[hooks.client.ts]';
const DEFAULT_MESSAGE = 'Hmm, not sure about that. Check the logs or open a ticket?';
const parseError = (error: unknown) => {
const httpError = error as AxiosError;
const request = httpError?.request as Request & { path: string };
const response = httpError?.response as AxiosResponse<{
message: string;
statusCode: number;
error: string;
}>;
const httpError = isHttpError(error) ? error : undefined;
const statusCode = httpError?.status || httpError?.data?.statusCode || 500;
const message = httpError?.data?.message || (httpError?.data && String(httpError.data)) || httpError?.message;
let code = response?.data?.statusCode || response?.status || httpError.code || '500';
if (response) {
code += ` - ${response.data?.error || response.statusText}`;
}
if (request && response) {
console.log({
status: response.status,
url: `${request.method} ${request.path}`,
response: response.data || 'No data',
});
}
console.log({
status: statusCode,
response: httpError?.data || 'No data',
});
return {
message: response?.data?.message || httpError?.message || DEFAULT_MESSAGE,
code,
message: message || DEFAULT_MESSAGE,
code: statusCode,
stack: httpError?.stack,
};
};