This is my error interceptor class. I need to throw error error to ccomponent class observable method: i haved checked throwError(error) is now deprecated, but there is no new Error(HttpErrorResponse)
@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
constructor(private toastr: ToastrService,private authService: AuthService,
private router:Router) {
}
intercept( request: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> {
return next.handle(request)
.pipe(
catchError((error: HttpErrorResponse) => {
debugger
let message = '';
if (error.error instanceof ErrorEvent) {
// handle client-side error
message = `Error: ${error.error.message}`;
this.toastr.error(message);
} else {
// handle server-side error
debugger
message = `Error: ${ error?.error?.Message || error?.statusText}`;
if(!error.status)
{
this.toastr.error('Not able connect to server');
}
else if ([400].includes(error.status) && error.error?.Message === 'Session Expired') {
this.toastr.error("Session Expired");
this.authService.logout();
}
.....
else if ([404].includes(error.status)) {
this.router.navigate(['404']);
}
else
{
this.toastr.error(message);
}
}
return throwError(() => error) //If i throw errror like this it is coming error inteceptor agian
})
)
}
}
component
getEditCollectionById(id:string)
{
debugger
this.collectionService.getEditCollectionById(id).pipe(takeUntil(this.unsubscribe$)).subscribe({
next: (result: any) => {
if (result) {
this.collection=result;
}
else {
this.close();
}
},
error: (error:any) => {
// If i throw error in interceptor it is not coming here
this.goToDetail(this.collectionId);
}
});
}
service
getEditCollectionById(id: string): Observable<ICollection> {
return this.httpClient.get<Result<ICollection>>(baseUrl + '/Collection/GetEditCollectionById'+ `/${id}`)
.pipe(map((res:any)=>{ res.data.valueDate = new Date(res.data.valueDate);
return res.data;
})
);
}
i need to throw error in interceptor class. i am getting 400 error from server. i need show error message from interceptor class and i need to throw error to controller method.
EDIT:
Error Debug
EDIT: afer debugging infigured out its happening becuase of
logout() {
debugger
this.httpClient.post<any>(`${baseUrl}/Auth/revoke-token`, {}, { withCredentials: true })
.subscribe();
this.stopRefreshTokenTimer();
this.setUserValue(null);
this.router.navigate(['login']);
}
Is there an update i need to do in this method?