Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): fetch error reporting #7391

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions open-api/typescript-sdk/fetch-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpError } from '@oazapfts/runtime';

export interface ApiExceptionResponse {
message: string;
error?: string;
statusCode: number;
}

export interface ApiHttpError extends HttpError {
data: ApiExceptionResponse;
}

export function isHttpError(error: unknown): error is ApiHttpError {
return error instanceof HttpError;
}
1 change: 1 addition & 0 deletions open-api/typescript-sdk/fetch.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './fetch-client';
export * from './fetch-errors';
32 changes: 10 additions & 22 deletions web/src/hooks.client.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
Expand Down
27 changes: 17 additions & 10 deletions web/src/lib/utils/handle-error.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import type { HttpError } from '@sveltejs/kit';
import { isHttpError } from '@immich/sdk';
import { isAxiosError } from 'axios';
import { notificationController, NotificationType } from '../components/shared-components/notification/notification';

export async function getServerErrorMessage(error: unknown) {
let data = (error as HttpError)?.body;
if (data instanceof Blob) {
const response = await data.text();
try {
data = JSON.parse(response);
} catch {
data = { message: response };
}
if (isHttpError(error)) {
return error.data?.message || error.data;
}

return data?.message || null;
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;
}
}

export async function handleError(error: unknown, message: string) {
Expand Down
Loading