I need this to handle errors where I have to display empty "lists" in dropdowns. This my interceptor
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError((err) => {
// check because invalid password return 401
if (err.status === 401 && !this.router.url.includes('/auth')) {
// auto logout if 401 response returned from api
localStorage.removeItem('token');
location.reload();
}
// Eror alert
if (this.router.url.includes('/auth')) {
this.message.error(
err.error ?? err.statusText,
{ nzDuration: 5000,
nzPauseOnHover: true },
);
}
console.log(err);
// I want to return empty array here
return throwError(err);
}));
}
So I can edit all my getSomethingMethod() with catchError but can i get this with interceptors?
CodePudding user response:
Yes, you can. In this section, you can show your error to the user in the form of a message or dialog and return EMPTY at the end
import { EMPTY } from 'rxjs';
catchError((error: HttpErrorResponse) => {
if (error) {
showMessage('messages.tryAgain');
}
return EMPTY
})
CodePudding user response:
return next.handle(request).pipe(catchError((err) => {
// YOUR OWN CODE
// RETURN THIS IF YOU WANT TO THROW ERROR, IT WILL IMMEDIATELY RESOLVE TO ERROR WITH GIVEN VALUE WHICH CAN BE AN EMPTY ARRAY
return throwError([]);
// RETURN THIS RETURN AN OBSERVABLE THAT WILL IMMEDIATELY GO TO COMPLETE STATE
return EMPTY;
// RETURN THIS IF YOU WANT TO RETURN AN EMPTY ARRAY
return of([]);
}));
}