A solution to your query is to utilize the Http Interceptor.
HTTP Interceptor acts as a mediator between the client-side and server-side, capturing all requests and responses.
This means you can intercept HTTP Response errors and direct the user accordingly:
@Injectable()
export class AuthInterceptorService implements HttpInterceptor {
constructor(private router: Router) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any> | any> {
return next.handle(req).pipe(
tap(event => {
// handle successful response
}, err => {
// redirect in case of errors
this.router.navigate(['/error-page'])
})
);
}
}