From 55f30c51cb0009386268ad4d89e24a08a461342a Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 6 Feb 2020 09:41:43 +0300 Subject: [PATCH] feat: add error response code condition for error title resolves #2763 --- .../src/lib/handlers/error.handler.ts | 190 ++++++++++-------- 1 file changed, 104 insertions(+), 86 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 e4663c38aa..7975fd9833 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 @@ -57,97 +57,108 @@ export class ErrorHandler { private injector: Injector, @Inject('HTTP_ERROR_CONFIG') private httpErrorConfig: HttpErrorConfig, ) { - this.actions.pipe(ofActionSuccessful(RestOccurError, RouterError, RouterDataResolved)).subscribe(res => { - if (res instanceof RestOccurError) { - const { payload: err = {} as HttpErrorResponse | any } = res; - const body = snq(() => (err as HttpErrorResponse).error.error, DEFAULT_ERROR_MESSAGES.defaultError.title); - - if (err instanceof HttpErrorResponse && err.headers.get('_AbpErrorFormat')) { - const confirmation$ = this.showError(null, null, body); - - if (err.status === 401) { - confirmation$.subscribe(() => { - this.navigateToLogin(); - }); - } - } else { - switch ((err as HttpErrorResponse).status) { - case 401: - this.canCreateCustomError(401) - ? this.show401Page() - : this.showError( - { - key: 'AbpAccount::DefaultErrorMessage401', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title, - }, - { - key: 'AbpAccount::DefaultErrorMessage401Detail', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details, - }, - ).subscribe(() => this.navigateToLogin()); - break; - case 403: - this.createErrorComponent({ - title: { - key: 'AbpAccount::DefaultErrorMessage403', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title, - }, - details: { - key: 'AbpAccount::DefaultErrorMessage403Detail', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details, - }, - status: 403, - }); - break; - case 404: - this.canCreateCustomError(404) - ? this.show404Page() - : this.showError( - { - key: 'AbpAccount::DefaultErrorMessage404', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details, - }, - { - key: 'AbpAccount::DefaultErrorMessage404Detail', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title, - }, - ); - break; - case 500: - this.createErrorComponent({ - title: { - key: 'AbpAccount::500Message', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title, - }, - details: { - key: 'AbpAccount::InternalServerErrorMessage', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details, - }, - status: 500, + this.actions + .pipe(ofActionSuccessful(RestOccurError, RouterError, RouterDataResolved)) + .subscribe(res => { + if (res instanceof RestOccurError) { + const { payload: err = {} as HttpErrorResponse | any } = res; + const body = snq( + () => (err as HttpErrorResponse).error.error, + DEFAULT_ERROR_MESSAGES.defaultError.title, + ); + + if (err instanceof HttpErrorResponse && err.headers.get('_AbpErrorFormat')) { + const confirmation$ = this.showError(null, null, body); + + if (err.status === 401) { + confirmation$.subscribe(() => { + this.navigateToLogin(); }); - break; - case 0: - if ((err as HttpErrorResponse).statusText === 'Unknown Error') { + } + } else { + switch ((err as HttpErrorResponse).status) { + case 401: + this.canCreateCustomError(401) + ? this.show401Page() + : this.showError( + { + key: 'AbpAccount::DefaultErrorMessage401', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.title, + }, + { + key: 'AbpAccount::DefaultErrorMessage401Detail', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError401.details, + }, + ).subscribe(() => this.navigateToLogin()); + break; + case 403: this.createErrorComponent({ title: { - key: 'AbpAccount::DefaultErrorMessage', - defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + key: 'AbpAccount::DefaultErrorMessage403', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.title, }, + details: { + key: 'AbpAccount::DefaultErrorMessage403Detail', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError403.details, + }, + status: 403, }); - } - break; - default: - this.showError(DEFAULT_ERROR_MESSAGES.defaultError.details, DEFAULT_ERROR_MESSAGES.defaultError.title); - break; + break; + case 404: + this.canCreateCustomError(404) + ? this.show404Page() + : this.showError( + { + key: 'AbpAccount::DefaultErrorMessage404', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.details, + }, + { + key: 'AbpAccount::DefaultErrorMessage404Detail', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError404.title, + }, + ); + break; + case 500: + this.createErrorComponent({ + title: { + key: 'AbpAccount::500Message', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.title, + }, + details: { + key: 'AbpAccount::InternalServerErrorMessage', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError500.details, + }, + status: 500, + }); + break; + case 0: + if ((err as HttpErrorResponse).statusText === 'Unknown Error') { + this.createErrorComponent({ + title: { + key: 'AbpAccount::DefaultErrorMessage', + defaultValue: DEFAULT_ERROR_MESSAGES.defaultError.title, + }, + }); + } + break; + default: + this.showError( + DEFAULT_ERROR_MESSAGES.defaultError.details, + DEFAULT_ERROR_MESSAGES.defaultError.title, + ); + break; + } } + } else if ( + res instanceof RouterError && + snq(() => res.event.error.indexOf('Cannot match') > -1, false) + ) { + this.show404Page(); + } else if (res instanceof RouterDataResolved && this.componentRef) { + this.componentRef.destroy(); + this.componentRef = null; } - } else if (res instanceof RouterError && snq(() => res.event.error.indexOf('Cannot match') > -1, false)) { - this.show404Page(); - } else if (res instanceof RouterDataResolved && this.componentRef) { - this.componentRef.destroy(); - this.componentRef = null; - } - }); + }); } private show401Page() { @@ -179,6 +190,9 @@ export class ErrorHandler { if (body.details) { message = body.details; title = body.message; + } else if (body.code) { + title = body.code; + message = body.message; } else { message = body.message || DEFAULT_ERROR_MESSAGES.defaultError.title; } @@ -192,7 +206,9 @@ export class ErrorHandler { private navigateToLogin() { this.store.dispatch( - new Navigate(['/account/login'], null, { state: { redirectUrl: this.store.selectSnapshot(RouterState.url) } }), + new Navigate(['/account/login'], null, { + state: { redirectUrl: this.store.selectSnapshot(RouterState.url) }, + }), ); } @@ -200,7 +216,9 @@ export class ErrorHandler { const renderer = this.rendererFactory.createRenderer(null, null); const host = renderer.selectRootElement(document.body, true); - this.componentRef = this.cfRes.resolveComponentFactory(HttpErrorWrapperComponent).create(this.injector); + this.componentRef = this.cfRes + .resolveComponentFactory(HttpErrorWrapperComponent) + .create(this.injector); for (const key in this.componentRef.instance) { if (this.componentRef.instance.hasOwnProperty(key)) {