Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/app/core/interceptors/http-error.interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
120 changes: 120 additions & 0 deletions src/app/core/interceptors/http-error.interceptor.ts
Original file line number Diff line number Diff line change
@@ -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<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
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.');
}
}
1 change: 1 addition & 0 deletions src/app/core/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './http-error.interceptor';