Skip to content
Open
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
17 changes: 11 additions & 6 deletions src/api/controllers/auth/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ApiError } from '@/api';
import { apiClient } from '../../client';
import { ToastType } from '@/models';
import router from '@/router';
Expand Down Expand Up @@ -37,17 +36,22 @@ export function scoped<F extends Func>(

export function showErrorToast<F extends Func>(
method: F
): Func<Promise<ReturnType<F>>, Parameters<F>> {
): Func<Promise<{ error: any; response: ReturnType<F> | null }>, Parameters<F>> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот здесь с типами не нужно запариваться

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Когда уберешь сложную конструкцию в конце, можно будет вернуть старые типы и все будет хорошо

return async (...args: any[]) => {
const toastStore = useToastStore();
try {
const response = await method(...args);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот здесь и можно деструктурировать.

const {error, response} = await method(...args);

return response;
} catch (err) {
const error = err as ApiError;
if ('error' in response) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Достаточно будет заменить на

if (error)

const errormessage = response.error?.detail?.[0].msg;
throw new Error(errormessage);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше здесь сделать

throw error;

и обрабатывать все уже в catch

} else {
return response;
}
} catch (err: any) {
const error = err?.detail?.[0] ?? err;
if (error) {
toastStore.push({
title: error.ru ?? error.message,
title: error.ru ?? error.msg ?? error,
type: ToastType.Error,
});
} else {
Expand All @@ -57,6 +61,7 @@ export function showErrorToast<F extends Func>(
type: ToastType.Error,
});
}
return { error: err, response: null };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не стоит менять формат выходных данных, сложно обрабатывать потом будет. Давай здесь возвращать undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ты, по сути, уже обработал ошибку, дальше ее прокидывать не нужно.

}
};
}
Expand Down
Loading