From ca3e849597619d8c0a6efe8a79dd51990ef47874 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Fri, 16 Jun 2023 12:39:03 +0300 Subject: [PATCH] remove canCustomErrorService. Rename error constants file --- .../constants/{error.ts => default-errors.ts} | 2 +- .../src/lib/handlers/error.handler.ts | 12 +++++++----- .../theme-shared/src/lib/models/common.ts | 5 +---- .../abp-format-error-handler.service.ts | 4 ++-- .../can-create-custom-error.service.ts | 18 ------------------ .../services/create-error-component.service.ts | 16 ++++++++++++---- .../theme-shared/src/lib/services/index.ts | 1 - .../services/router-error-handler.service.ts | 2 +- .../status-code-error-handler.service.ts | 6 ++---- .../tenant-resolve-error-handler.service.ts | 4 ++-- ...nknown-status-code-error-handler.service.ts | 4 ++-- .../src/lib/tests/error.handler.spec.ts | 5 +++-- .../theme-shared/src/lib/utils/error.utils.ts | 2 +- .../packages/theme-shared/src/public-api.ts | 2 +- 14 files changed, 35 insertions(+), 48 deletions(-) rename npm/ng-packs/packages/theme-shared/src/lib/constants/{error.ts => default-errors.ts} (98%) delete mode 100644 npm/ng-packs/packages/theme-shared/src/lib/services/can-create-custom-error.service.ts diff --git a/npm/ng-packs/packages/theme-shared/src/lib/constants/error.ts b/npm/ng-packs/packages/theme-shared/src/lib/constants/default-errors.ts similarity index 98% rename from npm/ng-packs/packages/theme-shared/src/lib/constants/error.ts rename to npm/ng-packs/packages/theme-shared/src/lib/constants/default-errors.ts index 3b92ce0e7b..46a0605b95 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/constants/error.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/constants/default-errors.ts @@ -45,7 +45,7 @@ export const DEFAULT_ERROR_LOCALIZATIONS = { }; export const CUSTOM_HTTP_ERROR_HANDLER_PRIORITY = Object.freeze({ - veryLow: -9, + veryLow: -99, low: -9, normal: 0, high: 9, 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 010a09528b..3ae0c25d3c 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 @@ -7,19 +7,19 @@ import { CustomHttpErrorHandlerService } from '../models/common'; import { Confirmation } from '../models/confirmation'; import { ConfirmationService } from '../services/confirmation.service'; import { CUSTOM_ERROR_HANDLERS, HTTP_ERROR_HANDLER } from '../tokens/http-error.token'; -import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/error'; +import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/default-errors'; import { RouterErrorHandlerService } from '../services/router-error-handler.service'; import { HTTP_ERROR_CONFIG } from '../tokens/http-error.token'; @Injectable({ providedIn: 'root' }) export class ErrorHandler { - protected httpErrorReporter = inject(HttpErrorReporterService); - protected confirmationService = inject(ConfirmationService); + private httpErrorReporter = inject(HttpErrorReporterService); + private confirmationService = inject(ConfirmationService); private routerErrorHandlerService = inject(RouterErrorHandlerService); protected httpErrorConfig = inject(HTTP_ERROR_CONFIG); private customErrorHandlers = inject(CUSTOM_ERROR_HANDLERS); private defaultHttpErrorHandler = (_, err: HttpErrorResponse) => throwError(() => err); - protected httpErrorHandler = + private httpErrorHandler = inject(HTTP_ERROR_HANDLER, { optional: true }) || this.defaultHttpErrorHandler; constructor(protected injector: Injector) { @@ -34,7 +34,9 @@ export class ErrorHandler { protected listenToRestError() { this.httpErrorReporter.reporter$ .pipe(filter(this.filterRestErrors), switchMap(this.executeErrorHandler)) - .subscribe(); + .subscribe(err => { + this.handleError(err); + }); } private executeErrorHandler = (error: HttpErrorResponse) => { diff --git a/npm/ng-packs/packages/theme-shared/src/lib/models/common.ts b/npm/ng-packs/packages/theme-shared/src/lib/models/common.ts index 821fe52625..f497312ff3 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/models/common.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/models/common.ts @@ -21,10 +21,7 @@ export interface HttpErrorConfig { }; } -export type HttpErrorHandler = ( - injector: Injector, - httpError: HttpErrorResponse, -) => Observable; +export type HttpErrorHandler = (httpError: HttpErrorResponse) => Observable; export type LocaleDirection = 'ltr' | 'rtl'; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/abp-format-error-handler.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/abp-format-error-handler.service.ts index b7c6fc9d76..a453c25226 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/abp-format-error-handler.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/abp-format-error-handler.service.ts @@ -4,11 +4,11 @@ import { HttpErrorResponse } from '@angular/common/http'; import { getErrorFromRequestBody } from '../utils/error.utils'; import { CustomHttpErrorHandlerService } from '../models/common'; import { ConfirmationService } from '../services/confirmation.service'; -import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY } from '../constants/error'; +import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY } from '../constants/default-errors'; @Injectable({ providedIn: 'root' }) export class AbpFormatErrorHandlerService implements CustomHttpErrorHandlerService { - priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.high; + readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.high; private confirmationService = inject(ConfirmationService); private authService = inject(AuthService); private error: HttpErrorResponse | undefined = undefined; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/can-create-custom-error.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/can-create-custom-error.service.ts deleted file mode 100644 index b078e2d404..0000000000 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/can-create-custom-error.service.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { inject, Injectable } from '@angular/core'; -import { ErrorScreenErrorCodes } from '../models/common'; -import { HTTP_ERROR_CONFIG } from '../tokens/http-error.token'; - -@Injectable({ - providedIn: 'root', -}) -export class CanCreateCustomErrorService { - private httpErrorConfig = inject(HTTP_ERROR_CONFIG); - - execute(status: ErrorScreenErrorCodes): boolean { - return !!( - this.httpErrorConfig?.errorScreen?.component && - this.httpErrorConfig?.errorScreen?.forWhichErrors && - this.httpErrorConfig?.errorScreen?.forWhichErrors.indexOf(status) > -1 - ); - } -} diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/create-error-component.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/create-error-component.service.ts index a088ebffa2..6972a93f34 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/create-error-component.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/create-error-component.service.ts @@ -12,7 +12,6 @@ import { Subject } from 'rxjs'; import { ResolveEnd } from '@angular/router'; import { filter } from 'rxjs/operators'; import { RouterEvents } from '@abp/ng.core'; -import { CanCreateCustomErrorService } from './can-create-custom-error.service'; import { HTTP_ERROR_CONFIG } from '../tokens/http-error.token'; import { HttpErrorWrapperComponent } from '../components/http-error-wrapper/http-error-wrapper.component'; import { ErrorScreenErrorCodes } from '../models/common'; @@ -23,18 +22,26 @@ export class CreateErrorComponentService { protected cfRes = inject(ComponentFactoryResolver); private routerEvents = inject(RouterEvents); private injector = inject(Injector); - private canCreateCustomErrorService = inject(CanCreateCustomErrorService); private httpErrorConfig = inject(HTTP_ERROR_CONFIG); componentRef: ComponentRef | null = null; - protected getErrorHostElement() { + private getErrorHostElement() { return document.body; } + public canCreateCustomError(status: ErrorScreenErrorCodes) { + return !!( + this.httpErrorConfig?.errorScreen?.component && + this.httpErrorConfig?.errorScreen?.forWhichErrors && + this.httpErrorConfig?.errorScreen?.forWhichErrors.indexOf(status) > -1 + ); + } + constructor() { this.listenToRouterDataResolved(); } + protected listenToRouterDataResolved() { this.routerEvents .getEvents(ResolveEnd) @@ -48,6 +55,7 @@ export class CreateErrorComponentService { private isCloseIconHidden() { return !!this.httpErrorConfig.errorScreen?.hideCloseIcon; } + execute(instance: Partial) { const renderer = this.rendererFactory.createRenderer(null, null); const hostElement = this.getErrorHostElement(); @@ -67,7 +75,7 @@ export class CreateErrorComponentService { this.componentRef.instance.hideCloseIcon = this.isCloseIconHidden(); const appRef = this.injector.get(ApplicationRef); - if (this.canCreateCustomErrorService.execute(instance.status as ErrorScreenErrorCodes)) { + if (this.canCreateCustomError(instance.status as ErrorScreenErrorCodes)) { this.componentRef.instance.cfRes = this.cfRes; this.componentRef.instance.appRef = appRef; this.componentRef.instance.injector = this.injector; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts index 1985995ce1..41f1b84039 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts @@ -3,7 +3,6 @@ export * from './nav-items.service'; export * from './page-alert.service'; export * from './toaster.service'; export * from './user-menu.service'; -export * from './can-create-custom-error.service'; export * from './create-error-component.service'; export * from './abp-format-error-handler.service'; export * from './tenant-resolve-error-handler.service'; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/router-error-handler.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/router-error-handler.service.ts index cb201444b5..14a4e79266 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/router-error-handler.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/router-error-handler.service.ts @@ -4,7 +4,7 @@ import { RouterEvents } from '@abp/ng.core'; import { NavigationError } from '@angular/router'; import { HTTP_ERROR_CONFIG } from '../tokens/'; import { CreateErrorComponentService } from '../services'; -import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/error'; +import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/default-errors'; @Injectable({ providedIn: 'root' }) export class RouterErrorHandlerService { diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts index 65e75fedec..e566ccb792 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts @@ -3,18 +3,16 @@ import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY, DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES, -} from '../constants/error'; +} from '../constants/default-errors'; import { AuthService, LocalizationParam } from '@abp/ng.core'; import { Observable } from 'rxjs'; import { inject, Injectable } from '@angular/core'; import { ConfirmationService } from './confirmation.service'; -import { CanCreateCustomErrorService } from './can-create-custom-error.service'; import { CreateErrorComponentService } from './create-error-component.service'; @Injectable({ providedIn: 'root' }) export class StatusCodeErrorHandlerService implements CustomHttpErrorHandlerService { private readonly confirmationService = inject(ConfirmationService); - private readonly canCreateCustomErrorService = inject(CanCreateCustomErrorService); private readonly createErrorComponentService = inject(CreateErrorComponentService); private readonly authService = inject(AuthService); readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.normal; @@ -37,7 +35,7 @@ export class StatusCodeErrorHandlerService implements CustomHttpErrorHandlerServ defaultValue: DEFAULT_ERROR_MESSAGES[key]?.details, }; - const canCreateCustomError = this.canCreateCustomErrorService.execute(this.status); + const canCreateCustomError = this.createErrorComponentService.canCreateCustomError(this.status); switch (this.status) { case 401: case 404: diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/tenant-resolve-error-handler.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/tenant-resolve-error-handler.service.ts index 57dc7dab9b..d58e0da72b 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/tenant-resolve-error-handler.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/tenant-resolve-error-handler.service.ts @@ -2,11 +2,11 @@ import { CustomHttpErrorHandlerService } from '../models/common'; import { inject, Injectable } from '@angular/core'; import { AuthService } from '@abp/ng.core'; import { HttpErrorResponse } from '@angular/common/http'; -import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY } from '../constants/error'; +import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY } from '../constants/default-errors'; @Injectable({ providedIn: 'root' }) export class TenantResolveErrorHandlerService implements CustomHttpErrorHandlerService { - priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.high; + readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.high; private authService = inject(AuthService); private isTenantResolveError(error: unknown) { return error instanceof HttpErrorResponse && !!error.headers.get('Abp-Tenant-Resolve-Error'); diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/unknown-status-code-error-handler.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/unknown-status-code-error-handler.service.ts index 88de56c14d..129c9ed5b9 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/unknown-status-code-error-handler.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/unknown-status-code-error-handler.service.ts @@ -3,13 +3,13 @@ import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY, DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES, -} from '../constants/error'; +} from '../constants/default-errors'; import { inject, Injectable } from '@angular/core'; import { CreateErrorComponentService } from './create-error-component.service'; @Injectable({ providedIn: 'root' }) export class UnknownStatusCodeErrorHandlerService implements CustomHttpErrorHandlerService { - priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.normal; + readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.normal; private statusText = 'Unknown Error'; private message = ''; private createErrorComponentService = inject(CreateErrorComponentService); 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 e6564ea99f..8688572436 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 @@ -7,7 +7,8 @@ import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { OAuthService } from 'angular-oauth2-oidc'; import { of, Subject } from 'rxjs'; import { HttpErrorWrapperComponent } from '../components/http-error-wrapper/http-error-wrapper.component'; -import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES, ErrorHandler } from '../handlers'; +import { ErrorHandler } from '../handlers'; +import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/default-errors'; import { ConfirmationService } from '../services'; import { httpErrorConfigFactory } from '../tokens/http-error.token'; @@ -16,7 +17,7 @@ const reporter$ = new Subject(); @NgModule({ exports: [HttpErrorWrapperComponent], declarations: [HttpErrorWrapperComponent], - entryComponents: [HttpErrorWrapperComponent], + //entryComponents: [HttpErrorWrapperComponent], imports: [CoreTestingModule], }) class MockModule {} diff --git a/npm/ng-packs/packages/theme-shared/src/lib/utils/error.utils.ts b/npm/ng-packs/packages/theme-shared/src/lib/utils/error.utils.ts index c03f16cf09..e417b53eb8 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/utils/error.utils.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/utils/error.utils.ts @@ -1,5 +1,5 @@ import { LocalizationParam } from '@abp/ng.core'; -import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/error'; +import { DEFAULT_ERROR_LOCALIZATIONS, DEFAULT_ERROR_MESSAGES } from '../constants/default-errors'; export function getErrorFromRequestBody(body: { details?: string; message?: string } | undefined) { let message: LocalizationParam; diff --git a/npm/ng-packs/packages/theme-shared/src/public-api.ts b/npm/ng-packs/packages/theme-shared/src/public-api.ts index 840463a70f..22661e3855 100644 --- a/npm/ng-packs/packages/theme-shared/src/public-api.ts +++ b/npm/ng-packs/packages/theme-shared/src/public-api.ts @@ -5,7 +5,7 @@ export * from './lib/animations'; export * from './lib/components'; export * from './lib/constants/validation'; -export * from './lib/constants/error'; +export * from './lib/constants/default-errors'; export * from './lib/directives'; export * from './lib/enums'; export * from './lib/handlers';