Reputation: 53
In my angular 6 application I am receiving error message from API, below is the code.
intercept(req: HttpRequest<any>, next: HttpHandler):Observable<HttpEvent<any>>{
return next.handle(req)
.pipe(
catchError((error: HttpErrorResponse)=>{
// const err="error occured";
// console.log(error);
// console.log(error.error.['@errors'].code);
// console.log(`error log`, error.error.code)
if (error.error['@errors'].code != 'ERR_UNITLIMIT') {
this.MyService.errorHasOccurred(err);}
return throwError(error);
})
);
}
I am getting below response from service like below
{"@errors":[{"code":"ERR_UNITLIMIT","message":"total cart value must not be exceed 500$.","details":[]}],"@warnings":[]}
I am trying to check if response object code contains ERR_UNITLIMIT or not . I tried things which has been commented in above code using // but always getting error of code is undefined.
Upvotes: 0
Views: 75
Reputation: 2363
Since "@errors" is an array of objects , you have to refer it using the index 0.
console.log(error.error['@errors'][0].code)
will do the job.
change your function as below
intercept(req: HttpRequest<any>, next: HttpHandler):Observable<HttpEvent<any>>{
return next.handle(req)
.pipe(
catchError((error: HttpErrorResponse)=>{
if (error.error['@errors'][0].code != 'ERR_UNITLIMIT') {
this.MyService.errorHasOccurred(error);}
return throwError(error);
})
);
}
Upvotes: 1