From 9b9742bd3c504d8917eba60f570083362c4884e6 Mon Sep 17 00:00:00 2001 From: anishfyle Date: Wed, 22 May 2024 15:59:32 +0530 Subject: [PATCH] PR Comments --- src/app/app.module.ts | 6 ---- .../http-error.interceptor.spec.ts | 16 --------- .../interceptor/http-error.interceptor.ts | 36 ------------------- src/app/core/services/common/api.service.ts | 16 ++++++--- 4 files changed, 11 insertions(+), 63 deletions(-) delete mode 100644 src/app/core/interceptor/http-error.interceptor.spec.ts delete mode 100644 src/app/core/interceptor/http-error.interceptor.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8f69c7459..e52f2dea6 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -18,7 +18,6 @@ import { SharedModule } from './shared/shared.module'; import { RippleModule } from 'primeng/ripple'; import { BrandingService } from './core/services/common/branding.service'; import { Sage300ConfigurationModule } from './integrations/sage300/sage300-main/sage300-configuration/sage300-configuration.module'; -import { HttpErrorInterceptor } from './core/interceptor/http-error.interceptor'; @NgModule({ declarations: [ @@ -47,11 +46,6 @@ import { HttpErrorInterceptor } from './core/interceptor/http-error.interceptor' useClass: JwtInterceptor, multi: true }, - { - provide: HTTP_INTERCEPTORS, - useClass: HttpErrorInterceptor, - multi: true - }, { provide: ErrorHandler, useClass: GlobalErrorHandler diff --git a/src/app/core/interceptor/http-error.interceptor.spec.ts b/src/app/core/interceptor/http-error.interceptor.spec.ts deleted file mode 100644 index ad9d42d17..000000000 --- a/src/app/core/interceptor/http-error.interceptor.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { TestBed } from '@angular/core/testing'; - -import { HttpErrorInterceptor } from './http-error.interceptor'; - -describe('HttpErrorInterceptor', () => { - beforeEach(() => TestBed.configureTestingModule({ - providers: [ - HttpErrorInterceptor - ] - })); - - it('should be created', () => { - const interceptor: HttpErrorInterceptor = TestBed.inject(HttpErrorInterceptor); - expect(interceptor).toBeTruthy(); - }); -}); diff --git a/src/app/core/interceptor/http-error.interceptor.ts b/src/app/core/interceptor/http-error.interceptor.ts deleted file mode 100644 index 546e5ccf6..000000000 --- a/src/app/core/interceptor/http-error.interceptor.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Injectable } from '@angular/core'; -import { - HttpEvent, - HttpInterceptor, - HttpHandler, - HttpRequest, - HttpErrorResponse -} from '@angular/common/http'; -import { Observable, throwError, of } from 'rxjs'; -import { catchError } from 'rxjs/operators'; - -@Injectable() -export class HttpErrorInterceptor implements HttpInterceptor { - intercept(req: HttpRequest, next: HttpHandler): Observable> { - return next.handle(req).pipe( - catchError((error: HttpErrorResponse) => { - if (req.method === 'POST') { - if (error.status >= 500) { - console.error(`POST error ${error.status}: ${error.message}`); - } - } else if (req.method === 'GET') { - if (error.status !== 404) { - console.error(`GET error ${error.status}: ${error.message}`); - } - // Else (404 GET error) - do not log - } else { - // For other HTTP methods, you can add custom logic or just log the errors - console.error(`HTTP ${req.method} error ${error.status}: ${error.message}`); - } - - // Return an observable that completes - return of(error as any); // Suppresses further propagation of the error - }) - ); - } -} diff --git a/src/app/core/services/common/api.service.ts b/src/app/core/services/common/api.service.ts index 7da958445..47036f566 100644 --- a/src/app/core/services/common/api.service.ts +++ b/src/app/core/services/common/api.service.ts @@ -26,12 +26,18 @@ export class ApiService { } private handleError(error: HttpErrorResponse, httpMethod: string, url: string) { - if (error.error instanceof ErrorEvent) { - console.error('An error occurred:', error.error.message); + if (httpMethod === 'POST') { + if (error.status >= 500) { + console.error(`POST error ${error.status}: ${error.message}`); + } + } else if (httpMethod === 'GET') { + if (error.status !== 404) { + console.error(`GET error ${error.status}: ${error.message}`); + } + // Else (404 GET error) - do not log } else { - console.error( - `Backend returned code ${error.status}, url was: ${url} method was: ${httpMethod}, body was: ${JSON.stringify(error.error)}` - ); + // For other HTTP methods, you can add custom logic or just log the errors + console.error(`HTTP ${httpMethod} error ${error.status}: ${error.message}`); } return throwError(error); }