When encountering errors that require displaying empty "lists" in dropdowns, I utilize this interceptor:
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError((err) => {
// Check for invalid password returning 401
if (err.status === 401 && !this.router.url.includes('/auth')) {
// Automatically logout if 401 response is returned from the api
localStorage.removeItem('token');
location.reload();
}
// Error alert
if (this.router.url.includes('/auth')) {
this.message.error(
err.error ?? err.statusText,
{ nzDuration: 5000,
nzPauseOnHover: true },
);
}
console.log(err);
// Return an empty array here
return throwError(err);
}));
}
By using catchError in all my getSomethingMethod(), can I achieve the same outcome through interceptors?
The example provided in the comments does not work: https://stackblitz.com/edit/angular-ivy-rhtclm?file=src/app/error.ts does not trigger the "res" section.