Last active
February 22, 2019 03:47
-
-
Save hidoos/f5a5290fcee21eb5aa25e0ce90a1437d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// errors-handler.ts | |
import { ErrorHandler, Injectable} from '@angular/core'; | |
import { HttpErrorResponse } from '@angular/common/http'; | |
@Injectable() | |
export class ErrorsHandler implements ErrorHandler { | |
handleError(error: Error | HttpErrorResponse) { | |
if (error instanceof HttpErrorResponse) { | |
// Server or connection error happened | |
if (!navigator.onLine) { | |
// Handle offline error | |
} else { | |
// Handle Http Error (error.status === 403, 404...) | |
} | |
} else { | |
// Handle Client Error (Angular Error, ReferenceError...) | |
} | |
// Log the error anyway | |
console.error('It happens: ', error); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// errors-handler.ts | |
@Injectable() | |
export class ErrorsHandler implements ErrorHandler { | |
constructor( | |
// Because the ErrorHandler is created before the providers, we’ll have to use the Injector to get them. | |
private injector: Injector, | |
) { } | |
handleError(error: Error | HttpErrorResponse) { | |
const notificationService = this.injector.get(NotificationService); | |
if (error instanceof HttpErrorResponse) { | |
// Server or connection error happened | |
if (!navigator.onLine) { | |
// Handle offline error | |
return notificationService.notify('No Internet Connection'); | |
} else { | |
// Handle Http Error (error.status === 403, 404...) | |
return notificationService.notify(`${error.status} - ${error.message}`); | |
} | |
} else { | |
// Handle Client Error (Angular Error, ReferenceError...) | |
} | |
// Log the error anyway | |
console.error('It happens: ', error); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// errors-handler.ts | |
@Injectable() | |
export class ErrorsHandler implements ErrorHandler { | |
constructor( | |
private injector: Injector | |
) { } | |
handleError(error: Error | HttpErrorResponse) { | |
const notificationService = this.injector.get(NotificationService); | |
const router = this.injector.get(Router); | |
if (error instanceof HttpErrorResponse) { | |
// Server or connection error happened | |
if (!navigator.onLine) { | |
// Handle offline error | |
return notificationService.notify('No Internet Connection'); | |
} else { | |
// Handle Http Error (error.status === 403, 404...) | |
return notificationService.notify(`${error.status} - ${error.message}`); | |
} | |
} else { | |
// Handle Client Error (Angular Error, ReferenceError...) | |
router.navigate(['/error'], { queryParams: {error: error} }); | |
} | |
// Log the error anyway | |
console.error('It happens: ', error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment