Skip to main content

Posts

Showing posts with the label Angular 5 Custom ErrorHandler

How to Create a Custom ErrorHandler?

How to create a custom ErrorHandler? The best way to log exceptions is to provide a specific log message for each possible exception. Always ensure that sufficient information is being logged and that nothing important is being excluded. The multiple steps involved in creating custom error logging in Angular - 1.            Create a constant class for global error messages. 2.            Create an error log service – ErrorLoggService . 3.            Create a global error handler for using the error log service for logging errors. Steps 1 – In the first step, we will create a constant class for logging global error messages and its look like this. export class AppConstants {   public static get baseURL (): string { return 'http://localhost:4200/api' ; }   public static get httpError (): string { ...

How to catch and log specific Angular errors in your app?

How to catch and log specific Angular errors in your app? The default implementation of ErrorHandler log error messages, status, and stack to the console.  To intercept error handling, we write a custom exception handler that replaces this default as appropriate for your app. import { Injectable , ErrorHandler } from '@angular/core' ; import { ErrorLoggService } from './error-logg.service' ; // Global error handler for logging errors @ Injectable () export class GlobalErrorHandler extends ErrorHandler {     constructor ( private errorLogService : ErrorLoggService ) {        //Angular provides a hook for centralized exception handling.        //constructor ErrorHandler(): ErrorHandler         super ();     }     handleError ( error ) : void {         this . errorLo...

Angular 4 Error Handling and Logging!

How To Get and Log an error in Angular 4? Two types of errors - 1.       If the backend returns an unsuccessful response like - 404, 500 and so on 2.       If something goes wrong in the client side like -network error etc. In the both cases - We are using HttpErrorResponse and return the useful information on what went wrong in this call! Example – http.get( '/api/users' ) .subscribe(data => {console.log(data);}, //Successful responses call the first callback. (err : HttpErrorResponse) => { if (err.error instanceof Error ) { console.log( 'Error - ' , err.error.message); } else { console.log( 'Error status - ${err.status}, and Error Detail - ${err.error}' ); } } }); How To handle and retry the failed request due to Poor Network Connection in Angular 4? In Angular 4, simply retry the request using RxJS operator called .retry (). It is auto...