I've looked around at a number of the ReactiveUI samples, but I can't see a good simple example of how to handle exceptions, where a message should be displayed to the user. (If there is a good example can somebody point me to it?).
My first question is how to handle an exception with ReactiveCommand and ToProperty. For example, I have the following code:
public class MainWindowViewModel : ReactiveObject
{
public ReactiveCommand CalculateTheAnswer { get; set; }
public MainWindowViewModel()
{
CalculateTheAnswer = new ReactiveCommand();
CalculateTheAnswer
.SelectMany(_ => AnswerCalculator())
.ToProperty(this, x => x.TheAnswer);
CalculateTheAnswer.ThrownExceptions
.Select(exception => MessageBox.Show(exception.Message));
}
private readonly ObservableAsPropertyHelper<int> _theAnswer;
public int TheAnswer
{
get { return _theAnswer.Value; }
}
private static IObservable<int> AnswerCalculator()
{
var task = Task.Factory.StartNew(() =>
{
throw new ApplicationException("Unable to calculate answer, because I don't know what the question is");
return 42;
});
return task.ToObservable();
}
}
I think I must be misunderstanding ThrownExceptions, because this observable is not receiving any items when I run the code above. What am I doing wrong?
My second question is how would I do this in a MVVM-friendly way. This blog entry mentions a User Errors feature, but I can't find any documentation on how to use it. How would I implement it into the above example?
Edit: I've published an example solution on github based on Paul's answer below.
You're understanding ThrownExceptions
, but it's on the wrong guy, _theAnswer.ThrownExceptions
will receive the Exception. But the tricky part, is now that button doesn't work any more - once an Observable ends OnError, it's done for good.
You end up having to do a few backflips here, something like:
static IObservable<int?> AnswerCalculator()
CalculateTheAnswer
.SelectMany(_ => AnswerCalculator())
.Catch(Observable.Return(null))
.Where(x => x != null)
.Select(x => x.Value)
.ToProperty(this, x => x.TheAnswer);
In this case, ReactiveAsyncCommand
is much easier, since a new IObservable
is created for every invocation, so you'd do:
// ReactiveAsyncCommand handles exceptions thrown for you
CalculateTheAnswer.RegisterAsyncTask(_ => AnswerCalculator())
.ToProperty(this, x => x.TheAnswer);
CalculateTheAnswer.ThrownExceptions.Subscribe(ex => MessageBox.Show("Aieeeee"));
So, UserError
is like an exception intended to be thrown at a user (i.e. it contains friendly text, not programmer text)
To use UserError
, you have to do two things - first, change your ThrownExceptions:
CalculateTheAnswer.ThrownExceptions
.SelectMany(ex => UserError.Throw("Something bad happened", ex))
.Subscribe(result => /* Decide what to do here, either nothing or retry */);
And in your View code-behind, call `RegisterHandler":
UserError.RegisterHandler(err => {
MessageBox.Show(err.ErrorMessage);
// This is what the ViewModel should do in response to the user's decision
return Observable.Return(RecoveryOptionResult.CancelOperation);
});
The cool part, is that this makes error dialogs testable - in a unit test:
var fixture = new MainWindowViewModel();
bool errorCalled;
using (UserError.OverrideHandlersForTesting(_ => { errorCalled = true; return RecoveryOptionResult.CancelOperation })) {
CalculateTheAnswer.Execute(null);
}
Assert.True(errorCalled);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With