I create new interceptors name HttpInterceptor.
Here is the sample code:
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpResponse,
HttpErrorResponse,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { ErrorHandler } from './error_handler';
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
constructor(
public errorHandler: ErrorHandler,
) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => { }, (err: any) => {
if (err instanceof HttpErrorResponse) {
this.errorHandler.handleError(err);
}
});
}
}
i have added providers in app.module.ts
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true,
}
]
I got this error.
Error: StaticInjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS -> ErrorHandler]: StaticInjectorError(Platform: core)[InjectionToken HTTP_INTERCEPTORS -> ErrorHandler]: NullInjectorError: No provider for ErrorHandler!
My code in Angular 7. Please help me.