Skip to content

Instantly share code, notes, and snippets.

@harbirchahal
Last active May 14, 2019 14:15
Show Gist options
  • Save harbirchahal/8dc42d570bc3454a427b6af1fa428410 to your computer and use it in GitHub Desktop.
Save harbirchahal/8dc42d570bc3454a427b6af1fa428410 to your computer and use it in GitHub Desktop.
Request Timeout HTTP Interceptor
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