Skip to content

Commit

Permalink
updated the ApiError class
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriaMaltseva committed Dec 2, 2024
1 parent afcd331 commit defa18d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 8 additions & 6 deletions assets/js/src/core/components/login-form/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,19 +51,19 @@ export const LoginForm = ({ additionalLogins }: ILoginFormProps): React.JSX.Elem
const handleAuthentication = async (event: React.FormEvent<HTMLFormElement>): Promise<void> => {
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({
Expand Down
15 changes: 12 additions & 3 deletions assets/js/src/core/modules/app/error-handler/classes/api-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -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)) {
Expand Down

0 comments on commit defa18d

Please sign in to comment.