From 90a1f9bd188811a79d4feec0960d9e94f8a5008f Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 19 Aug 2020 21:55:12 +0300 Subject: [PATCH 1/3] feat: add default error localizations to error.handler --- .../src/lib/handlers/error.handler.ts | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts index af2db977fe..670655bb63 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts @@ -43,6 +43,29 @@ export const DEFAULT_ERROR_MESSAGES = { }, }; +export const DEFAULT_ERROR_LOCALIZATIONS = { + defaultError: { + title: 'AbpUi::DefaultErrorMessage', + details: 'AbpUi::DefaultErrorMessageDetail', + }, + defaultError401: { + title: 'AbpUi::DefaultErrorMessage401', + details: 'AbpUi::DefaultErrorMessage401Detail', + }, + defaultError403: { + title: 'AbpUi::DefaultErrorMessage403', + details: 'AbpUi::DefaultErrorMessage403Detail', + }, + defaultError404: { + title: 'AbpUi::DefaultErrorMessage404', + details: 'AbpUi::DefaultErrorMessage404Detail', + }, + defaultError500: { + title: 'AbpUi::500Message', + details: 'AbpUi::DefaultErrorMessage', + }, +}; + @Injectable({ providedIn: 'root' }) export class ErrorHandler { componentRef: ComponentRef; @@ -88,7 +111,10 @@ export class ErrorHandler { filter(this.filterRestErrors), ) .subscribe(err => { - const body = snq(() => err.error.error, DEFAULT_ERROR_MESSAGES.defaultError.title); + const body = snq(() => err.error.error, { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + }); if (err instanceof HttpErrorResponse && err.headers.get('_AbpErrorFormat')) { const confirmation$ = this.showError(null, null, body); @@ -168,8 +194,14 @@ export class ErrorHandler { break; default: this.showError( - DEFAULT_ERROR_MESSAGES.defaultError.details, - DEFAULT_ERROR_MESSAGES.defaultError.title, + { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.details, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.details, + }, + { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + }, ); break; } @@ -207,10 +239,16 @@ export class ErrorHandler { message = body.details; title = body.message; } else if (body.message) { - title = DEFAULT_ERROR_MESSAGES.defaultError.title; + title = { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + }; message = body.message; } else { - message = body.message || DEFAULT_ERROR_MESSAGES.defaultError.title; + message = body.message || { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + }; } } From fc673d48cbb3a32130cb3c1f125b32cf0c02d3c9 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 20 Aug 2020 09:51:47 +0300 Subject: [PATCH 2/3] refactor: use constant for localizations instead of strings --- .../src/lib/handlers/error.handler.ts | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts index 670655bb63..d54d219c6f 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts @@ -87,7 +87,10 @@ export class ErrorHandler { private listenToRouterError() { this.actions - .pipe(ofActionSuccessful(RouterError), filter(this.filterRouteErrors)) + .pipe( + ofActionSuccessful(RouterError), + filter(this.filterRouteErrors), + ) .subscribe(() => this.show404Page()); } @@ -131,11 +134,11 @@ export class ErrorHandler { ? this.show401Page() : this.showError( { - key: 'AbpAccount::DefaultErrorMessage401', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title, }, { - key: 'AbpAccount::DefaultErrorMessage401Detail', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details, }, ).subscribe(() => this.navigateToLogin()); @@ -143,11 +146,11 @@ export class ErrorHandler { case 403: this.createErrorComponent({ title: { - key: 'AbpAccount::DefaultErrorMessage403', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title, }, details: { - key: 'AbpAccount::DefaultErrorMessage403Detail', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details, }, status: 403, @@ -158,11 +161,11 @@ export class ErrorHandler { ? this.show404Page() : this.showError( { - key: 'AbpAccount::DefaultErrorMessage404', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details, }, { - key: 'AbpAccount::DefaultErrorMessage404Detail', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title, }, ); @@ -170,11 +173,11 @@ export class ErrorHandler { case 500: this.createErrorComponent({ title: { - key: 'AbpAccount::500Message', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title, }, details: { - key: 'AbpAccount::InternalServerErrorMessage', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details, }, status: 500, @@ -184,7 +187,7 @@ export class ErrorHandler { if (err.statusText === 'Unknown Error') { this.createErrorComponent({ title: { - key: 'AbpAccount::DefaultErrorMessage', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, }, details: err.message, @@ -212,7 +215,7 @@ export class ErrorHandler { private show401Page() { this.createErrorComponent({ title: { - key: 'AbpAccount::401Message', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title, }, status: 401, @@ -222,7 +225,7 @@ export class ErrorHandler { private show404Page() { this.createErrorComponent({ title: { - key: 'AbpAccount::404Message', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title, }, status: 404, From 412b26d9df94c397b7feb4abb08d68079d6de869 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 20 Aug 2020 10:00:47 +0300 Subject: [PATCH 3/3] test: change some tests of error.handler.spec --- .../src/lib/tests/error.handler.spec.ts | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts b/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts index 89e9239b6e..30dfee3e93 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts @@ -9,7 +9,7 @@ import { Actions, NgxsModule, ofActionDispatched, Store } from '@ngxs/store'; import { OAuthService } from 'angular-oauth2-oidc'; import { of } from 'rxjs'; import { HttpErrorWrapperComponent } from '../components/http-error-wrapper/http-error-wrapper.component'; -import { DEFAULT_ERROR_MESSAGES, ErrorHandler } from '../handlers'; +import { DEFAULT_ERROR_MESSAGES, ErrorHandler, DEFAULT_ERROR_LOCALIZATIONS } from '../handlers'; import { ConfirmationService } from '../services'; import { httpErrorConfigFactory } from '../tokens/http-error.token'; @@ -66,11 +66,11 @@ describe('ErrorHandler', () => { const error = new HttpErrorResponse({ status: 500 }); const params = { title: { - key: 'AbpAccount::500Message', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title, }, details: { - key: 'AbpAccount::InternalServerErrorMessage', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details, }, status: 500, @@ -95,11 +95,11 @@ describe('ErrorHandler', () => { const error = new HttpErrorResponse({ status: 403 }); const params = { title: { - key: 'AbpAccount::DefaultErrorMessage403', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title, }, details: { - key: 'AbpAccount::DefaultErrorMessage403Detail', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details, }, status: 403, @@ -124,7 +124,7 @@ describe('ErrorHandler', () => { const error = new HttpErrorResponse({ status: 0, statusText: 'Unknown Error' }); const params = { title: { - key: 'AbpAccount::DefaultErrorMessage', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, }, details: error.message, @@ -150,11 +150,11 @@ describe('ErrorHandler', () => { expect(errorConfirmation).toHaveBeenCalledWith( { - key: 'AbpAccount::DefaultErrorMessage404', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details, }, { - key: 'AbpAccount::DefaultErrorMessage404Detail', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title, }, CONFIRMATION_BUTTONS, @@ -165,8 +165,14 @@ describe('ErrorHandler', () => { store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 412 }))); expect(errorConfirmation).toHaveBeenCalledWith( - DEFAULT_ERROR_MESSAGES.defaultError.details, - DEFAULT_ERROR_MESSAGES.defaultError.title, + { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.details, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.details, + }, + { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + }, CONFIRMATION_BUTTONS, ); }); @@ -176,11 +182,11 @@ describe('ErrorHandler', () => { expect(errorConfirmation).toHaveBeenCalledWith( { - key: 'AbpAccount::DefaultErrorMessage401', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title, }, { - key: 'AbpAccount::DefaultErrorMessage401Detail', + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details, }, CONFIRMATION_BUTTONS, @@ -205,7 +211,10 @@ describe('ErrorHandler', () => { store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401, headers }))); expect(errorConfirmation).toHaveBeenCalledWith( - DEFAULT_ERROR_MESSAGES.defaultError.title, + { + key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + }, null, CONFIRMATION_BUTTONS, );