diff --git a/frontend/src/api/common/api-error.ts b/frontend/src/api/common/api-error.ts index c5726cc92..b59b7fdf6 100644 --- a/frontend/src/api/common/api-error.ts +++ b/frontend/src/api/common/api-error.ts @@ -7,8 +7,8 @@ export class ApiError extends Error { constructor( public readonly statusCode: number, - public readonly backendErrorName: string | undefined, - public readonly backendErrorMessage: string | undefined + public readonly backendErrorName?: string, + public readonly backendErrorMessage?: string ) { super() } diff --git a/frontend/src/api/common/error-to-i18n-key-mapper.ts b/frontend/src/api/common/error-to-i18n-key-mapper.ts index d52dc6bef..d84fe86f0 100644 --- a/frontend/src/api/common/error-to-i18n-key-mapper.ts +++ b/frontend/src/api/common/error-to-i18n-key-mapper.ts @@ -13,35 +13,38 @@ export class ErrorToI18nKeyMapper { constructor(private apiError: Error, private i18nNamespace?: string) {} - public withHttpCode(code: number, i18nKey: string): this { + public withHttpCode(code: number, i18nKey: string, treatAsAbsoluteKey?: boolean): this { if (this.foundI18nKey === undefined && this.apiError instanceof ApiError && this.apiError.statusCode === code) { - this.foundI18nKey = i18nKey + this.foundI18nKey = treatAsAbsoluteKey ? i18nKey : `${this.i18nNamespace ?? ''}.${i18nKey}` } return this } - public withBackendErrorName(errorName: string, i18nKey: string): this { + public withBackendErrorName(errorName: string, i18nKey: string, treatAsAbsoluteKey?: boolean): this { if ( this.foundI18nKey === undefined && this.apiError instanceof ApiError && this.apiError.backendErrorName === errorName ) { - this.foundI18nKey = i18nKey + this.foundI18nKey = treatAsAbsoluteKey ? i18nKey : `${this.i18nNamespace ?? ''}.${i18nKey}` } return this } - public withErrorMessage(message: string, i18nKey: string): this { + public withErrorMessage(message: string, i18nKey: string, treatAsAbsoluteKey?: boolean): this { if (this.foundI18nKey === undefined && this.apiError.message === message) { - this.foundI18nKey = i18nKey + this.foundI18nKey = treatAsAbsoluteKey ? i18nKey : `${this.i18nNamespace ?? ''}.${i18nKey}` } return this } - public orFallbackI18nKey(fallback: T): string | T { - if (!this.foundI18nKey) { - return fallback + public orFallbackI18nKey( + fallback: T, + treatAsAbsoluteKey?: boolean + ): string | T { + if (this.foundI18nKey) { + return this.foundI18nKey } - return this.i18nNamespace ? `${this.i18nNamespace}.${this.foundI18nKey}` : this.foundI18nKey + return !treatAsAbsoluteKey && fallback ? `${this.i18nNamespace ?? ''}.${fallback}` : fallback } } diff --git a/frontend/src/components/profile-page/settings/profile-change-password.tsx b/frontend/src/components/profile-page/settings/profile-change-password.tsx index d2724d732..fc0c5b7f6 100644 --- a/frontend/src/components/profile-page/settings/profile-change-password.tsx +++ b/frontend/src/components/profile-page/settings/profile-change-password.tsx @@ -36,11 +36,11 @@ export const ProfileChangePassword: React.FC = () => { return true } catch (error) { const foundI18nKey = new ErrorToI18nKeyMapper(error as Error, 'login.auth.error') - .withHttpCode(401, 'invalidCredentials') + .withHttpCode(401, 'usernamePassword') .withBackendErrorName('FeatureDisabledError', 'loginDisabled') - .withBackendErrorName('PasswordTooWeakError', 'passwordTooWeak') + .withBackendErrorName('PasswordTooWeakError', 'login.register.error.passwordTooWeak', true) .orFallbackI18nKey('other') - return Promise.reject(foundI18nKey) + return Promise.reject(new Error(foundI18nKey)) } finally { if (formRef.current) { formRef.current.reset() @@ -79,14 +79,13 @@ export const ProfileChangePassword: React.FC = () => { - + - + - -