2

I don't know what happened but from someday I cannot see any javascript error on console.

If the page is blank for some javascript error I need to put a breakpoint on that Angular function to see which is the error.

function getDebugContext(error) {
return (/** @type {?} */ (error))[ERROR_DEBUG_CONTEXT];

}

3
  • what is the version of angular? Commented Oct 4, 2018 at 11:32
  • Anglar 5.2.0. But in compiler I have ~5.2.0 and in compiler-cli ^5.2.9 Commented Oct 4, 2018 at 13:41
  • Ok. Give me some time.i'll post an answer. Commented Oct 4, 2018 at 13:44

2 Answers 2

10

please create a .ts file called GlobalErrorHandler.ts and paste this

import { Router } from '@angular/router';
import { ErrorHandler, Injectable, Injector } from '@angular/core';
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
  constructor(private injector: Injector) { }
  handleError(error) {
    const router = this.injector.get(Router);
    console.log(error.stack.toString());

  }

}

Import this to your app.module.ts provider

providers: [

    {
      provide: ErrorHandler,
      useClass: GlobalErrorHandler
    }
  ],

this should work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! It works. I changed console.log for console.error
I've come here regarding this article: medium.com/@kamrankhatti/… because I've implemented the global error handler but there isn't mentioned that all erros will be obscured by implementing that ¯_(ツ)_/¯ Thanks @Asanka
Why are we injecting the Injector and getting a reference to the router?
0
@Injectable()
export class GlobalErrorHandler extends ErrorHandler {
  constructor(private injector: Injector) {
    super();
  }

  handleError(error: any): void {
    const router = this.injector.get(Router);

    if (error.message.includes('Loading chunk')) {
      console.error('There was a problem loading a chunk:', error);
      window.location.reload();
    } else {
      super.handleError(error);
    }
  }
}

if you extend the ErrorHandler class, you have access to the default error handling behavior provided by Angular’s ErrorHandler class. You can override the handleError method to provide your own error handling logic, and you can call super.handleError(error) to fall back to the default behavior.

That not obscured all erros

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.