I am attempting to redirect to an error component when the response data contains a "401" error. Below is my code snippet, but it seems that it is not properly handling the error. I have tried redirecting to an external URL without success. Any guidance would be greatly appreciated. Thank you in advance.
Service.ts:-
public searchGroups(searchReviewData: SurveillanceReviewSearchViewModel): Observable<SurveillanceReviewSearchViewModel> {
this._urlSurveillanceDetails = this.baseHref + "/ReviewProfile/SearchGroups";
const headers: HttpHeaders = new HttpHeaders();
headers.append('Content-Type', 'application/json');
let getData = this.http.post<SurveillanceReviewSearchViewModel>(this._urlSurveillanceDetails, searchReviewData, { headers: headers })
.pipe(
catchError(this.handleError)
);
return getData;
}
handleError(data: any) {
if (data instanceof ErrorEvent) {
}
else {
switch (data.errorResponse.httpStatusCode) {
case 401:
this.router.navigate(['error', 'unauthorized']);
break;
}
}
// return an observable with a user-facing error message
return throwError(
'Something bad happened; please try again later:' + JSON.stringify(data.errorResponse.httpStatusCode));
}
Component.ts
searchGroups(searchReviewData: SurveillanceReviewSearchViewModel) {
this._surveillanceService.searchGroups(searchReviewData).subscribe(data => {
if (data != undefined) {
this.reviewData = data;
if (data.reviews != null) {
if (data.reviews.length == 1) {
this.pagerSelectedData = data.reviews;
this.loading = false;
}
}
else {
this.loading = true;
}
}
});
}
ResponseJson from MVC controller:
{
"loggedInUser": {
"v2020UserId": "kdevisenakanthan"
},
"errorResponse": {
"errorCode": null,
"transactionId": null,
"httpStatusCode": 401
}
}