Browse Source

execute custom error handler if it exists

pull/7960/head
mehmet-erim 6 years ago
parent
commit
7e46a48e78
  1. 229
      npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

229
npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

@ -12,13 +12,14 @@ import {
} from '@angular/core'; } from '@angular/core';
import { NavigationError, ResolveEnd } from '@angular/router'; import { NavigationError, ResolveEnd } from '@angular/router';
import { Actions, ofActionSuccessful } from '@ngxs/store'; import { Actions, ofActionSuccessful } from '@ngxs/store';
import { Observable, Subject } from 'rxjs'; import { Observable, of, Subject, throwError } from 'rxjs';
import { filter, map } from 'rxjs/operators'; import { catchError, filter, map, switchMap } from 'rxjs/operators';
import snq from 'snq'; import snq from 'snq';
import { HttpErrorWrapperComponent } from '../components/http-error-wrapper/http-error-wrapper.component'; import { HttpErrorWrapperComponent } from '../components/http-error-wrapper/http-error-wrapper.component';
import { ErrorScreenErrorCodes, HttpErrorConfig } from '../models/common'; import { ErrorScreenErrorCodes, HttpErrorConfig } from '../models/common';
import { Confirmation } from '../models/confirmation'; import { Confirmation } from '../models/confirmation';
import { ConfirmationService } from '../services/confirmation.service'; import { ConfirmationService } from '../services/confirmation.service';
import { HTTP_ERROR_HANDLER } from '../tokens/http-error.token';
export const DEFAULT_ERROR_MESSAGES = { export const DEFAULT_ERROR_MESSAGES = {
defaultError: { defaultError: {
@ -70,28 +71,32 @@ export const DEFAULT_ERROR_LOCALIZATIONS = {
export class ErrorHandler { export class ErrorHandler {
componentRef: ComponentRef<HttpErrorWrapperComponent>; componentRef: ComponentRef<HttpErrorWrapperComponent>;
protected httpErrorHandler = this.injector.get(HTTP_ERROR_HANDLER, (_, err: HttpErrorResponse) =>
throwError(err),
);
constructor( constructor(
private actions: Actions, protected actions: Actions,
private routerEvents: RouterEvents, protected routerEvents: RouterEvents,
private confirmationService: ConfirmationService, protected confirmationService: ConfirmationService,
private cfRes: ComponentFactoryResolver, protected cfRes: ComponentFactoryResolver,
private rendererFactory: RendererFactory2, protected rendererFactory: RendererFactory2,
private injector: Injector, protected injector: Injector,
@Inject('HTTP_ERROR_CONFIG') private httpErrorConfig: HttpErrorConfig, @Inject('HTTP_ERROR_CONFIG') protected httpErrorConfig: HttpErrorConfig,
) { ) {
this.listenToRestError(); this.listenToRestError();
this.listenToRouterError(); this.listenToRouterError();
this.listenToRouterDataResolved(); this.listenToRouterDataResolved();
} }
private listenToRouterError() { protected listenToRouterError() {
this.routerEvents this.routerEvents
.getNavigationEvents('Error') .getNavigationEvents('Error')
.pipe(filter(this.filterRouteErrors)) .pipe(filter(this.filterRouteErrors))
.subscribe(() => this.show404Page()); .subscribe(() => this.show404Page());
} }
private listenToRouterDataResolved() { protected listenToRouterDataResolved() {
this.routerEvents this.routerEvents
.getEvents(ResolveEnd) .getEvents(ResolveEnd)
.pipe(filter(() => !!this.componentRef)) .pipe(filter(() => !!this.componentRef))
@ -101,113 +106,127 @@ export class ErrorHandler {
}); });
} }
private listenToRestError() { protected listenToRestError() {
this.actions this.actions
.pipe( .pipe(
ofActionSuccessful(RestOccurError), ofActionSuccessful(RestOccurError),
map(action => action.payload), map(action => action.payload),
filter(this.filterRestErrors), filter(this.filterRestErrors),
switchMap(this.executeErrorHandler),
) )
.subscribe(err => { .subscribe();
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')) { private executeErrorHandler = err => {
const confirmation$ = this.showError(null, null, body); const returnValue = this.httpErrorHandler(this.injector, err);
if (err.status === 401) { return (returnValue instanceof Observable ? returnValue : of(null)).pipe(
confirmation$.subscribe(() => { catchError(err => {
this.navigateToLogin(); this.handleError(err);
}); return of(null);
} }),
} else { );
switch (err.status) { };
case 401:
this.canCreateCustomError(401) private handleError(err: any) {
? this.show401Page() const body = snq(() => err.error.error, {
: this.showError( key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title,
{ defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title,
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title, });
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title,
}, if (err instanceof HttpErrorResponse && err.headers.get('_AbpErrorFormat')) {
{ const confirmation$ = this.showError(null, null, body);
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.details,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details, if (err.status === 401) {
}, confirmation$.subscribe(() => {
).subscribe(() => this.navigateToLogin()); this.navigateToLogin();
break; });
case 403: }
this.createErrorComponent({ } else {
title: { switch (err.status) {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.title, case 401:
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title, this.canCreateCustomError(401)
}, ? this.show401Page()
details: { : this.showError(
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.details, {
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details, key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title,
}, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title,
status: 403,
});
break;
case 404:
this.canCreateCustomError(404)
? this.show404Page()
: this.showError(
{
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.details,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details,
},
{
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title,
},
);
break;
case 500:
this.createErrorComponent({
title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title,
}, },
details: { {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.details, key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.details,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details,
}, },
status: 500, ).subscribe(() => this.navigateToLogin());
}); break;
break; case 403:
case 0: this.createErrorComponent({
if (err.statusText === 'Unknown Error') { title: {
this.createErrorComponent({ key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.title,
title: { defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title,
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, },
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, details: {
}, key: DEFAULT_ERROR_LOCALIZATIONS.defaultError403.details,
details: err.message, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details,
isHomeShow: false, },
}); status: 403,
} });
break; break;
default: case 404:
this.showError( this.canCreateCustomError(404)
? this.show404Page()
: this.showError(
{ {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.details, key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.details,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.details, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details,
}, },
{ {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title, key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title,
}, },
); );
break; break;
case 500:
this.createErrorComponent({
title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title,
},
details: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError500.details,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details,
},
status: 500,
});
break;
case 0:
if (err.statusText === 'Unknown Error') {
this.createErrorComponent({
title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError.title,
defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title,
},
details: err.message,
isHomeShow: false,
});
} }
} break;
}); default:
this.showError(
{
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;
}
}
} }
private show401Page() { protected show401Page() {
this.createErrorComponent({ this.createErrorComponent({
title: { title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title, key: DEFAULT_ERROR_LOCALIZATIONS.defaultError401.title,
@ -217,7 +236,7 @@ export class ErrorHandler {
}); });
} }
private show404Page() { protected show404Page() {
this.createErrorComponent({ this.createErrorComponent({
title: { title: {
key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title, key: DEFAULT_ERROR_LOCALIZATIONS.defaultError404.title,
@ -227,7 +246,7 @@ export class ErrorHandler {
}); });
} }
private showError( protected showError(
message?: LocalizationParam, message?: LocalizationParam,
title?: LocalizationParam, title?: LocalizationParam,
body?: any, body?: any,
@ -256,7 +275,7 @@ export class ErrorHandler {
}); });
} }
private navigateToLogin() { protected navigateToLogin() {
this.injector.get(AuthService).initLogin(); this.injector.get(AuthService).initLogin();
} }
@ -304,13 +323,13 @@ export class ErrorHandler {
); );
} }
private filterRestErrors = ({ status }: HttpErrorResponse): boolean => { protected filterRestErrors = ({ status }: HttpErrorResponse): boolean => {
if (typeof status !== 'number') return false; if (typeof status !== 'number') return false;
return this.httpErrorConfig.skipHandledErrorCodes.findIndex(code => code === status) < 0; return this.httpErrorConfig.skipHandledErrorCodes.findIndex(code => code === status) < 0;
}; };
private filterRouteErrors = (navigationError: NavigationError): boolean => { protected filterRouteErrors = (navigationError: NavigationError): boolean => {
return ( return (
snq(() => navigationError.error.message.indexOf('Cannot match') > -1) && snq(() => navigationError.error.message.indexOf('Cannot match') > -1) &&
this.httpErrorConfig.skipHandledErrorCodes.findIndex(code => code === 404) < 0 this.httpErrorConfig.skipHandledErrorCodes.findIndex(code => code === 404) < 0

Loading…
Cancel
Save