Browse Source

remove canCustomErrorService. Rename error constants file

pull/16853/head
Mahmut Gundogdu 3 years ago
parent
commit
ca3e849597
  1. 2
      npm/ng-packs/packages/theme-shared/src/lib/constants/default-errors.ts
  2. 12
      npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts
  3. 5
      npm/ng-packs/packages/theme-shared/src/lib/models/common.ts
  4. 4
      npm/ng-packs/packages/theme-shared/src/lib/services/abp-format-error-handler.service.ts
  5. 18
      npm/ng-packs/packages/theme-shared/src/lib/services/can-create-custom-error.service.ts
  6. 16
      npm/ng-packs/packages/theme-shared/src/lib/services/create-error-component.service.ts
  7. 1
      npm/ng-packs/packages/theme-shared/src/lib/services/index.ts
  8. 2
      npm/ng-packs/packages/theme-shared/src/lib/services/router-error-handler.service.ts
  9. 6
      npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts
  10. 4
      npm/ng-packs/packages/theme-shared/src/lib/services/tenant-resolve-error-handler.service.ts
  11. 4
      npm/ng-packs/packages/theme-shared/src/lib/services/unknown-status-code-error-handler.service.ts
  12. 5
      npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts
  13. 2
      npm/ng-packs/packages/theme-shared/src/lib/utils/error.utils.ts
  14. 2
      npm/ng-packs/packages/theme-shared/src/public-api.ts

2
npm/ng-packs/packages/theme-shared/src/lib/constants/error.ts → 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,

12
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) => {

5
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<any>;
export type HttpErrorHandler = (httpError: HttpErrorResponse) => Observable<any>;
export type LocaleDirection = 'ltr' | 'rtl';

4
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;

18
npm/ng-packs/packages/theme-shared/src/lib/services/can-create-custom-error.service.ts

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

16
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<HttpErrorWrapperComponent> | 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<HttpErrorWrapperComponent>) {
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;

1
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';

2
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 {

6
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:

4
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');

4
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);

5
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 {}

2
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;

2
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';

Loading…
Cancel
Save