fix(profile): show correct error messages

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2023-03-29 21:40:17 +02:00
parent 15374acb93
commit 5e1e773859
3 changed files with 21 additions and 19 deletions

View file

@ -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()
}

View file

@ -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<T extends string | undefined = string>(fallback: T): string | T {
if (!this.foundI18nKey) {
return fallback
public orFallbackI18nKey<T extends string | undefined = string>(
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
}
}