Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property catch does not exist on type Observable

Tags:

angular

rxjs

I'm trying to add error handling interceptor with rxjs but i have a problem with compiling it becouse the "catch" blinking red

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
    console.log(req);
    return next.handle(req).catch(error => {
        if (error instanceof HttpErrorResponse) {
            this.messageService.error(`Błąd połączenia: ${error.message}`);
        }
        return observableThrowError(error);
    });
}

}

like image 774
johny2502 Avatar asked Oct 22 '25 01:10

johny2502


1 Answers

In newer rxjs versions you have to use the pipe operator and catch inside it. ref

Example:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
        return next
            .handle(req)
            .pipe(catchError( (error: HttpErrorResponse) => {
                 this.messageService.error(`Błąd połączenia:${error.message}`);
                 return throwError(error);
            }
        ))
    }
like image 132
Constantin Guidon Avatar answered Oct 24 '25 15:10

Constantin Guidon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!