[BP-841] Log non http errors in error handler Signed-off-by: Christopher Keim <keim@develop-group.de>
diff --git a/src/app/core/interceptors/error.interceptor.spec.ts b/src/app/core/interceptors/error.interceptor.spec.ts index 04595d5..7251748 100644 --- a/src/app/core/interceptors/error.interceptor.spec.ts +++ b/src/app/core/interceptors/error.interceptor.spec.ts
@@ -1,16 +1,21 @@ /******************************************************************************** - * Copyright © 2018 Mettenmeier GmbH. + * Copyright © 2018 Mettenmeier GmbH and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Mettenmeier GmbH - initial implementation + * Basys GmbH - automatic report generation implementation ********************************************************************************/ -import { TestBed, inject } from '@angular/core/testing'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; -import { ErrorInterceptor } from '@core/interceptors/error.interceptor'; +import {inject, TestBed} from '@angular/core/testing'; +import {HttpClientTestingModule} from '@angular/common/http/testing'; +import {ErrorInterceptor} from '@core/interceptors/error.interceptor'; +import {HttpErrorResponse} from '@angular/common/http'; describe('ErrorInterceptor', () => { beforeEach(() => { @@ -24,26 +29,26 @@ expect(service).toBeTruthy(); })); - it('should call error handler with response from Backend', inject([ErrorInterceptor], (service: ErrorInterceptor) => { - const httpErrorResponse: any = { + it('should call error handler with response from back end', inject([ErrorInterceptor], (service: ErrorInterceptor) => { + const httpErrorResponse = new HttpErrorResponse({ error: { httpStatus: 401, message: 'Unauthorized', localizedMessage: 'Unauthorized' } - }; + }); service.handleError(httpErrorResponse); })); - it('should call error handler without response from Backend and replace undefined strings', inject([ErrorInterceptor], + it('should call error handler without response from back end and replace undefined strings', inject([ErrorInterceptor], (service: ErrorInterceptor) => { - const httpErrorResponse: any = { + const httpErrorResponse = new HttpErrorResponse({ error: { httpStatus: undefined, message: undefined, localizedMessage: undefined } - }; + }); service.handleError(httpErrorResponse); })); });
diff --git a/src/app/core/interceptors/error.interceptor.ts b/src/app/core/interceptors/error.interceptor.ts index 4864071..900af93 100644 --- a/src/app/core/interceptors/error.interceptor.ts +++ b/src/app/core/interceptors/error.interceptor.ts
@@ -1,19 +1,21 @@ /******************************************************************************** - * Copyright © 2018 Mettenmeier GmbH. + * Copyright © 2018 Mettenmeier GmbH and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Mettenmeier GmbH - initial implementation + * Basys GmbH - automatic report generation implementation ********************************************************************************/ -import { Injectable, ErrorHandler, Injector } from '@angular/core'; -import { HttpErrorResponse } from '@angular/common/http'; -import { MessageService } from 'primeng/components/common/messageservice'; - -import { UtilService } from '@core/services/util.service'; -import { ErrorObject } from '@shared/model/ErrorObject'; +import {ErrorHandler, Injectable, Injector} from '@angular/core'; +import {HttpErrorResponse} from '@angular/common/http'; +import {UtilService} from '@core/services/util.service'; +import {ErrorObject} from '@shared/model/ErrorObject'; @Injectable() export class ErrorInterceptor implements ErrorHandler { @@ -23,33 +25,36 @@ ) { } - handleError(httpErrorResponse: HttpErrorResponse) { - const utilService = this.injector.get(UtilService); - let errorMessage: ErrorObject[]; - if (Array.isArray(httpErrorResponse.error)) { // should be any kind of validation error - httpErrorResponse.error.forEach(error => { - error.httpStatus = httpErrorResponse.status; - }); - errorMessage = httpErrorResponse.error; - } else if (httpErrorResponse.error && httpErrorResponse.error.byteLength !== undefined) { // reports error - const encodedString = String.fromCharCode.apply(null, new Uint8Array(httpErrorResponse.error)); - const decodedString = decodeURIComponent(escape(encodedString)); - if (decodedString) { - errorMessage = [JSON.parse(decodedString)]; + public handleError(httpErrorResponse: any) { + if (httpErrorResponse instanceof HttpErrorResponse) { + const utilService = this.injector.get(UtilService); + let errorMessage: ErrorObject[]; + if (Array.isArray(httpErrorResponse.error)) { // should be any kind of validation error + httpErrorResponse.error.forEach(error => { + error.httpStatus = httpErrorResponse.status; + }); + errorMessage = httpErrorResponse.error; + } else if (httpErrorResponse.error && httpErrorResponse.error.byteLength !== undefined) { // reports error + const encodedString = String.fromCharCode.apply(null, new Uint8Array(httpErrorResponse.error)); + const decodedString = decodeURIComponent(escape(encodedString)); + if (decodedString) { + errorMessage = [JSON.parse(decodedString)]; + } + } else { // any other error + errorMessage = [{ + httpStatus: httpErrorResponse.error.httpStatus, + message: httpErrorResponse.error.message, + localizedMessage: httpErrorResponse.error.localizedMessage + }]; + if (errorMessage[0].httpStatus === undefined) { + errorMessage[0].httpStatus = httpErrorResponse.status; + errorMessage[0].message = httpErrorResponse.statusText; + errorMessage[0].localizedMessage = httpErrorResponse.statusText; + } } - } else { // any other error - errorMessage = [{ - httpStatus: httpErrorResponse.error.httpStatus, - message: httpErrorResponse.error.message, - localizedMessage: httpErrorResponse.error.localizedMessage - }]; - if (errorMessage[0].httpStatus === undefined) { - errorMessage[0].httpStatus = httpErrorResponse.status; - errorMessage[0].message = httpErrorResponse.statusText; - errorMessage[0].localizedMessage = httpErrorResponse.statusText; - } + utilService.throwError(errorMessage); + } else { + console.error(httpErrorResponse); } - - utilService.throwError(errorMessage); } }