RxJs Observable - Handle 404 by emitting default value
I think I may have figured this out. A hint was lurking in the browser console...
TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
Reading the RxJs documentation on error handling showed them doing what I'm trying to do, so I was a bit confused by this. But then I noticed that in the documentation they call a mystery function getCachedVersion()
which I assumed returned an actual cached thing. However if they instead returned an Observable.of()
some actual cached thing, then that might explain it.
When I changed my catch code to:
.catch(function (e) {
if(e.status === 404) {
// no photo for this user!
console.log(`No photo for user ${id}! Returning default...`);
return Observable.of(undefined);
}
})
...it all started working.