In my quest to develop a universal service for retrieving settings from the server, I've encountered an issue. When errors arise, I want to intercept them and provide a default value (I have a predetermined configuration that should be utilized when errors occur locally).
Fortunately, I am utilizing Angular 4.3.6
, which allows me to leverage the new HttpClientModule
.
Check out the code:
/**
* Fetches the map settings
* @returns { Observable<MapConfiguration> }
*/
public getMapConfiguration(): Observable<MapConfiguration> {
return this.http.get<MapConfiguration>(MapService.MAPSETTINGSURL)
.catch((exception) => {
console.warn('Could not fetch map configuration due to: ', exception);
return new MapConfiguration();
});
}
This code is triggering the following error message:
Type 'MapConfiguration' is not assignable to type 'ObservableInput<{}>'.
As someone who is familiar with promises but still learning about Observables, I'm seeking guidance on handling this challenge. With promises, I could easily return a value in case of an error. How can I address this issue to ensure there's always a value returned?
I aim to have the service manage errors rather than the subscriber