Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle RxJs timeout complete - Angular HttpClient

How to detect an error by timeout operator? I would like to show an alert or something like that just when the server doesn't response.

I have a similiar code in my interceptor:

this.http.post('http://localhost:3000/api/core', data)
        .pipe(
            timeout(30000),
            map((response: any) => { // Success...
              return response;
            }),
            catchError((error) => { // Error...
              // Timeout over also handled here
              // I want to return an error for timeout
              return throwError(error || 'Timeout Exception');
            }),
            finalize(() => {
              console.log('Request it is over');
            })
        );

["rxjs": "^6.0.0", "@angular/http": "^6.0.3",]

like image 379
Daniel Delgado Avatar asked Nov 10 '18 01:11

Daniel Delgado


1 Answers

This works

import { throwError, TimeoutError } from 'rxjs';

catchError((error) => { // Error...
   // Handle 'timeout over' error
   if (error instanceof TimeoutError) {
      return throwError('Timeout Exception');
   }

   // Return other errors
   return throwError(error);
})

I implemented this at my intecerptor that has other functionalities, the code here

like image 81
Daniel Delgado Avatar answered Oct 12 '22 18:10

Daniel Delgado