I have an interceptor to handle errors in my Angular app.
Also I have an input that has a keyup function that makes API calls.
My problem is that when the error is throw to the component the stream is stopped (normal behaviour because of onError in subscribe), so the keyup function doesn't work anymore.
My question is how can I manage this, so it doesn't end the stream when an error is thrown to the component ?
INTERCEPTOR :
export class HttpErrorInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError((error: HttpErrorResponse) => {
let errorMsg = ''
if (error.error instanceof ErrorEvent) {
console.log('this is client side error')
errorMsg = `Error: ${error.error.message}`
}
else {
console.log('this is server side error')
errorMsg = `Error Code: ${error.status}, Message: ${error.message}`
}
console.log(errorMsg)
return throwError(errorMsg)
}),
)
}
}
SERVICE :
search_address(query: string): Observable<Feature[]> {
const url = 'https://api.mapbox.com/geocoding/v5/mapbox.places2/'
return this.http.get<MapboxOutput>(url + query + '.json?limit=5&types=address&access_token=' + environment.mapboxToken).pipe(map((res: MapboxOutput) => {
return res.features
}))
}
COMPONENT :
ngOnInit() {
// when keyup in input, trigger mapbox service to get addresses
this.inputChanged.pipe(debounceTime(500), distinctUntilChanged(), mergeMap((searchTerm) => this.mapboxService.search_address(searchTerm)))
.subscribe((features: Feature[]) => {
this.addresses = features
}, err => {
console.log(err)
})
}