Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular routing exceptions handing in error handler

How to handle navigation errors in ErrorHandler?

I tried using the following condition to check whether Error object is of NavigationError but it says false.

export class AppErrorHandler implements ErrorHandler {

  constructor() { }

  handleError(error: Error) {
if (error instanceof NavigationError) {
      //do something here, may be notify or redirect to error page.
    }
}

How to handle router navigation errors in ErrorHandler? I don't want to add wildcard routing.

Will the above condition works if the type is correct? If so, against what type I should check?

like image 282
Prajwal Avatar asked Sep 02 '26 11:09

Prajwal


1 Answers

@prajwal

try to use the next way to handle errors, it should works on 99.9% =):

import { Router, NavigationError } from '@angular/router';

 constructor( private router: Router) {
this.router.events.filter(e => e instanceof NavigationError).subscribe(e => {
  console.log(`NAV ERROR -> ${e}`);
});

}

Result:

The right relative path is ./home, but I have tried to go on ./home2

like image 166
Damir Beylkhanov Avatar answered Sep 05 '26 14:09

Damir Beylkhanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!