Skip to main content

Posts

Showing posts with the label Global Error Handling

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 { ...

Angular 5 ErrorHandler - Custom and Global Error Handling

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 . errorLogService...