From ce6ed02c5583f92e1db9f5edea74a7bd218d732a Mon Sep 17 00:00:00 2001 From: Tomislav Kaljevic Date: Thu, 3 Nov 2022 15:40:40 +0100 Subject: [PATCH 1/2] Http error interceptor with non-implemented methods --- .../http-error.interceptor.spec.ts | 16 +++ .../interceptors/http-error.interceptor.ts | 120 ++++++++++++++++++ src/app/core/interceptors/index.ts | 1 + 3 files changed, 137 insertions(+) create mode 100644 src/app/core/interceptors/http-error.interceptor.spec.ts create mode 100644 src/app/core/interceptors/http-error.interceptor.ts create mode 100644 src/app/core/interceptors/index.ts diff --git a/src/app/core/interceptors/http-error.interceptor.spec.ts b/src/app/core/interceptors/http-error.interceptor.spec.ts new file mode 100644 index 0000000..ad9d42d --- /dev/null +++ b/src/app/core/interceptors/http-error.interceptor.spec.ts @@ -0,0 +1,16 @@ +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/interceptors/http-error.interceptor.ts b/src/app/core/interceptors/http-error.interceptor.ts new file mode 100644 index 0000000..4d82243 --- /dev/null +++ b/src/app/core/interceptors/http-error.interceptor.ts @@ -0,0 +1,120 @@ +import { Injectable } from '@angular/core'; +import { + HttpErrorResponse, + HttpEvent, + HttpHandler, + HttpInterceptor, + HttpRequest, +} from '@angular/common/http'; +import { Observable, throwError } from 'rxjs'; +import { catchError } from 'rxjs/operators'; + +//TODO: It's necessary to inject a service (or any other mechanism) that could display error messages on UI. + +//TODO: It's necessary to add a logic for all methods that are not completed, based on system requirements. + +@Injectable() +export class HttpErrorInterceptor implements HttpInterceptor { + constructor() {} + + /** + * The interceptor method. + * + * @param req - Current request. + * @param next - HttpHandler. + */ + public intercept( + req: HttpRequest, + next: HttpHandler + ): Observable> { + return next.handle(req).pipe( + catchError((error: HttpErrorResponse) => { + const errorStatus = error.status; + // classify by code + const isClientError = errorStatus % 400 <= 99; + const isServerError = errorStatus % 500 <= 99; + + if (isClientError) { + this.handleClientError(error); + } else if (isServerError) { + this.handleServerError(error); + } else { + this.defaultErrorHandler(error); + } + + return throwError(error.message); + }) + ); + } + + /** + * Will simply load notify. + * + * @param error - Error ocurred. + */ + private defaultErrorHandler(error: HttpErrorResponse) { + throw new Error('Method not implemented.'); + } + + /** + * Handle all server errors (5xx). + * + * @param error - Error ocurred. + */ + private handleServerError(error: HttpErrorResponse) { + throw new Error('Method not implemented.'); + } + + /** + * Handle all client errors (4xx). + * + * @param error - Error ocurred. + */ + private handleClientError(error: HttpErrorResponse) { + const errorStatus = error.status; + switch (errorStatus) { + case 400: + this.handleValidationError(error); + break; + case 401: + this.handleAuthError(error); + break; + case 403: + this.handleUnauthorizedError(error); + break; + case 422: + this.handleValidationError(error); + break; + default: + this.defaultErrorHandler(error); + break; + } + } + + /** + * Authorization error. + * + * @param error - Error ocurred. + */ + private handleAuthError(error: HttpErrorResponse) { + throw new Error('Method not implemented.'); + } + + /** + * Not authorized. + * + * @param error Error that ocurred. + */ + private handleUnauthorizedError(error: HttpErrorResponse) { + throw new Error('Method not implemented.'); + } + + /** + * Form Validation error method, here we'll iterate through each validation message in response + * + * @param error - Error ocurred. + */ + private handleValidationError(error: HttpErrorResponse) { + throw new Error('Method not implemented.'); + } +} diff --git a/src/app/core/interceptors/index.ts b/src/app/core/interceptors/index.ts new file mode 100644 index 0000000..8fb032a --- /dev/null +++ b/src/app/core/interceptors/index.ts @@ -0,0 +1 @@ +export * from './http-error.interceptor'; From 11622f3cd2ce83b37daf945a6a891c512073f3f4 Mon Sep 17 00:00:00 2001 From: Boris Jotic Date: Fri, 4 Nov 2022 12:28:36 +0100 Subject: [PATCH 2/2] removed sufficient 400 call --- src/app/core/interceptors/http-error.interceptor.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/core/interceptors/http-error.interceptor.ts b/src/app/core/interceptors/http-error.interceptor.ts index 4d82243..941d561 100644 --- a/src/app/core/interceptors/http-error.interceptor.ts +++ b/src/app/core/interceptors/http-error.interceptor.ts @@ -73,9 +73,9 @@ export class HttpErrorInterceptor implements HttpInterceptor { private handleClientError(error: HttpErrorResponse) { const errorStatus = error.status; switch (errorStatus) { - case 400: - this.handleValidationError(error); - break; + // case 400: + // this.handleValidationError(error); + // break; case 401: this.handleAuthError(error); break;