DEV Community

Discussion on: Global error page in Angular

Collapse
kostyatretyak profile image
Костя Третяк

As far as I understand, all errors are redirected to the /error path. Right? The best option is to show the error page on the path where the user went. At the moment (Angular 13) there is no such built-in feature, so we have to come up with workarounds.

See solution on stackblitz

Collapse
anirbmuk profile image
Anirban Mukherjee Author

It is an interesting point. The only problem I see with this approach is the need to write an error handler inside every route resolver. That kind of makes the components lose their purpose. I believe all the user needs is to navigate back to the page where he/she came from, and for that we can make use of @angular/common's Location class.

Another approach is - (if we want to stay on the same page) - is to use { skipLocationChange: true } when we navigate to /error.

Collapse
kostyatretyak profile image
Костя Третяк

That kind of makes the components lose their purpose.

No, it's not. On the contrary, we can use the components to the fullest, because we can use the appropriate component for the error handler:

<ng-container *ngIf="!httpErrorResponse">
  Current component code here
</ng-container>

<cst-http-errors [httpErrorResponse]="httpErrorResponse"></cst-http-errors>
Enter fullscreen mode Exit fullscreen mode