Reputation: 1
I'm working in Angular 11 and I built an HttpInterceptor whose job is to create custom error toast whenever an http request fails.
I've been testing the interceptor by forcing bad requests to my server and I encounter no issues, with toasts appearing correctly, but when I test 5xx errors, only some requests get intercepted by Angular's HttpInterceptor although my console logs errors for every one of them. Does anyone know where's the issue?
Interceptor
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//sets the header of the intercepted request so it can be logged later
const headers = req.headers;
//handle responses
return next.handle(req)
.pipe(
catchError((error: HttpErrorResponse) => {
console.log(req, error)
if (error instanceof HttpErrorResponse) {
//shows a toast interface based on the error
}
})
}
One of the requests not getting intercepted
getPatientRiskFactors(patientId: string): Observable<type> {
return this.store$.select(selectConfigFile).pipe(
switchMap(cfg => {
let url = cfg.configFile.backend.base_url;
let queryParams = [
//params
];
return super.http.post<type[]>(url, queryParams, opt)
.pipe(
map(val => //array to item))
);
})
)
}
Call to the request
When I initialize this component, I request multiple data from my server so I used combineLatest to join the observable of each call. I get to intercept only one of these calls, while the others are just logged by the console.
this.obsElements$ = combineLatest([
this.patientsService.getPatientDetail(this.patientId)
.pipe(
map(val=>val[0])
),
this.patientsService.getPatientAllergyList(this.patientId),
this.patientsService.getPatientRiskFactors(this.patientId),
this.patientsService.getPatientsList()
]);
Upvotes: 0
Views: 489