diff --git a/assets/js/src/core/components/login-form/login-form.tsx b/assets/js/src/core/components/login-form/login-form.tsx index 57dbc7595..4b71d06e3 100644 --- a/assets/js/src/core/components/login-form/login-form.tsx +++ b/assets/js/src/core/components/login-form/login-form.tsx @@ -21,7 +21,9 @@ import { useMessage } from '@Pimcore/components/message/useMessage' import { useTranslation } from 'react-i18next' import { setUser } from '@Pimcore/modules/auth/user/user-slice' import { Icon } from '../icon/icon' -import { type Credentials, useLoginMutation, type UserInformation } from '@Pimcore/modules/auth/authorization-api-slice.gen' +import { type Credentials, useLoginMutation } from '@Pimcore/modules/auth/authorization-api-slice.gen' +import { trackError } from '@Pimcore/modules/app/error-handler/error-handler' +import { ApiError } from '@Pimcore/modules/app/error-handler/classes/api-error' export interface IAdditionalLogins { key: string @@ -49,19 +51,19 @@ export const LoginForm = ({ additionalLogins }: ILoginFormProps): React.JSX.Elem const handleAuthentication = async (event: React.FormEvent): Promise => { const loginTask = login({ credentials: formState }) - loginTask.catch((error) => { - throw new Error(error.message as string) + loginTask.catch((error: Error) => { + trackError(new ApiError(error)) }) try { event.preventDefault() - const response = (await loginTask) as any + const response = (await loginTask) if (response.error !== undefined) { - throw new Error(response.error.data.error as string) + trackError(new ApiError(response.error)) } - const userInformation = response.data as UserInformation + const userInformation = response.data! dispatch(setUser(userInformation)) } catch (e: any) { void messageApi.error({ diff --git a/assets/js/src/core/modules/app/error-handler/classes/api-error.ts b/assets/js/src/core/modules/app/error-handler/classes/api-error.ts index 902db6706..60f62f156 100644 --- a/assets/js/src/core/modules/app/error-handler/classes/api-error.ts +++ b/assets/js/src/core/modules/app/error-handler/classes/api-error.ts @@ -18,8 +18,9 @@ import type { SerializedError } from '@reduxjs/toolkit' type ApiErrorData = FetchBaseQueryError | SerializedError interface IApiErrorDetails { - detail: string - message: string + detail?: string + message?: string + error?: string } const DEFAULT_ERROR_CONTENT = 'Something went wrong.' @@ -35,8 +36,16 @@ export class ApiError extends Error { public getContent (): string { if (!isEmpty(this.errorData)) { + if (!isEmpty((this.errorData as Error)?.message)) { + return (this.errorData as Error).message + } + if ('data' in this.errorData && !isEmpty((this.errorData.data as IApiErrorDetails)?.message)) { - return (this.errorData.data as IApiErrorDetails)?.message + return (this.errorData.data as IApiErrorDetails).message! + } + + if ('data' in this.errorData && !isEmpty((this.errorData.data as IApiErrorDetails)?.error)) { + return (this.errorData.data as IApiErrorDetails).error! } if ('error' in this.errorData && isString(this.errorData.error)) {