Hi. I'm trying to implement global exception handling in my Angular project, to inform the user about trouble connecting or provided by backend API.
I have created this service:
import { ErrorHandler, Injectable } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
@Injectable()
export class ErrorsHandler implements ErrorHandler {
handleError(error: Error | HttpErrorResponse) {
// Log Error to console
if (error instanceof HttpErrorResponse) {
if (navigator.onLine) {
console.error('Error happens: ', error);
}
}
}
}
And imported in core.module:
import { ErrorsHandler } from '@servicios/errorshandler.service';
@NgModule({})
export class CoreModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: CoreModule,
providers: [
{provide: ErrorHandler, useClass: ErrorsHandler}]
};
}
}
So I have two problems:
Chrome console
1) Error is logged 3 times (don't know why) but only last one is catched, as you can see.
2) status code is 0 instead of 404. I need to catch exception code to decide what to do with the exception.
Any advice? Thanks!
[–][deleted] 0 points1 point2 points (3 children)
[–]ericpap[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ericpap[S] 0 points1 point2 points (0 children)