I have the following method which is responsible for calling my service class and passing the results off to another method for saving them in my db:
public IObservable<bool> SyncSessions()
{
var subject = new ReplaySubject<bool>();
try
{
var query = new ByFilterQuery { SearchPeriodStartTime = DateTime.Now };
var sessions = _sessionService.GetSessions(query).Result;
var saved = SaveSessions(sessions);
subject.OnNext(saved);
subject.OnCompleted();
}
catch (Exception ex)
{
subject.OnError(ex);
}
return subject;
}
_sessionService.GetSessions will throw a HttpRequestException if the server returns a 500 or something similar. I have a unit test which mocks this behaviour and want to test my method handles the error gracefully.
Is there any better ways of propagating the error in the Rx fashion? I tried doing:
_sessionService.GetSessions(query).ToObservable().Select(SaveSessions);
But this threw my error instead of it being passed to the error handling action in the calling method. I also plan to merge this method with several others and handle the errors in a combined manor.
EDIT: Here is how I was subscribing to the observable
Exception error = null;
_sessionManager
.SyncSessions()
.Subscribe(null, e => error = e);
Assert.That(error, Is.Not.Null.After(500));
I'm passing null into the first parameter as I don't really care about that in the context of this test