I'm using RC6 and I'm trying to figure out how to catch HTTP errors - auth errors in particular - in the entire application.
There are a number of posts that describe how to extend the Http
class with a custom class, but I'm not sure how to register the new class exactly as it appears the syntax has changed with the the recent ngModule changes.
Here's the class (with all relevant imports added):
@Injectable()
export class InterceptedHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
super( backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
console.log('request...');
return super.request(url, options);
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
console.log('get...');
return super.get(url,options);
}
}
I thought I'd be able to do the following in the providers
section of @ngModule
:
imports: [ HttpModule, ... ],
providers: [
...
InterceptedHttp,
{provide: Http, useClass: InterceptedHttp },
ConnectionBackend
],
but this just gets me a bunch of missing module errors:
ERROR in [default] C:/WebConnectionProjects/AlbumViewer/Web/src/app/app.module.ts:64:10
Argument of type '{ imports: (ModuleWithProviders | typeof BrowserModule)[]; declarations: (typeof AlbumList | type...' is not assignable to parameter of type 'NgModuleMetadataType'.
Types of property 'providers' are incompatible.
Type '(typeof ConnectionBackend | typeof Album | typeof Artist | typeof Track | typeof AppConfiguration...' is not assignable to type 'Provider[]'.
Type 'typeof ConnectionBackend | typeof Album | typeof Artist | typeof Track | typeof AppConfiguration ...' is not assignable to type 'Provider'.
Type 'typeof ConnectionBackend' is not assignable to type 'Provider'.
Type 'typeof ConnectionBackend' is not assignable to type 'FactoryProvider'.
Property 'provide' is missing in type 'typeof ConnectionBackend'.
removing the added lines and everything works.
So, how do I register a custom Http class?