Last active
May 14, 2019 14:15
-
-
Save harbirchahal/8dc42d570bc3454a427b6af1fa428410 to your computer and use it in GitHub Desktop.
Request Timeout HTTP Interceptor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; | |
import { empty, TimeoutError } from 'rxjs'; | |
import { timeout, catchError } from 'rxjs/operators'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class RequestTimeoutHttpInterceptor implements HttpInterceptor { | |
intercept(req: HttpRequest<any>, next: HttpHandler) { | |
return next.handle(req).pipe( | |
timeout(1000), | |
catchError(err => { | |
if (err instanceof TimeoutError) | |
console.error('Timeout has occurred', req.url); | |
return empty(); | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment