I have implemented a check to call an API in the canLoad event for a module in my Angular application.
However, even if the API returns a 404 error in the network, my page continues to load. I want to change this behavior so that if a 404 error is encountered, the page should redirect to an error page.
canLoad(route: Route, segments: UrlSegment[])
{
return this.http.get('testapi/getacces').pipe(map((response: boolean) => {
if (response) {
return true;
} else {
this.router.navigate(['/error']);
return false;
}
}));
}
While the code works as expected when the API is available, it does not handle the situation where a 404 error occurs. I am looking for a solution to properly handle the exception and route to an error page in case of a 404 error.