From 3e2252d2ce81aed636649b8a84fae825c6be34ca Mon Sep 17 00:00:00 2001 From: Dennis Daume Date: Wed, 30 Jul 2014 18:50:55 +0200 Subject: [PATCH 1/2] Make ExecuteAsync throw the original exception This fixes #685 --- ReactiveUI.Tests/ReactiveCommandTest.cs | 31 +++++++++++++++++++++++++ ReactiveUI/ReactiveCommand.cs | 4 ++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ReactiveUI.Tests/ReactiveCommandTest.cs b/ReactiveUI.Tests/ReactiveCommandTest.cs index a24c81a1e5..1a22473d30 100644 --- a/ReactiveUI.Tests/ReactiveCommandTest.cs +++ b/ReactiveUI.Tests/ReactiveCommandTest.cs @@ -180,6 +180,37 @@ public void NoSubscriberOfThrownExceptionsEqualsDeath() Assert.False(failed); }); } + + [Fact] + public async Task ExecuteAsyncThrowsExceptionOnError() + { + var command = ReactiveCommand.CreateAsyncObservable(_ => + Observable.Throw(new Exception("Aieeeee!"))); + + var exceptions = command.ThrownExceptions.CreateCollection(); + + bool failed = false; + try { + await command.ExecuteAsync(); + } catch (Exception ex) { + failed = ex.Message == "Aieeeee!"; + } + + Assert.True(failed); + Assert.Equal(1, exceptions.Count); + Assert.Equal("Aieeeee!", exceptions[0].Message); + } + + [Fact] + public void ExecuteDoesntThrowOnError() + { + var command = ReactiveCommand.CreateAsyncObservable(_ => + Observable.Throw(new Exception("Aieeeee!"))); + + command.ThrownExceptions.Subscribe(); + + command.Execute(null); + } } public class ReactiveAsyncCommandTest diff --git a/ReactiveUI/ReactiveCommand.cs b/ReactiveUI/ReactiveCommand.cs index 412ed78e26..6bc199dd54 100644 --- a/ReactiveUI/ReactiveCommand.cs +++ b/ReactiveUI/ReactiveCommand.cs @@ -374,7 +374,7 @@ public IObservable ExecuteAsync(object parameter = null) .Do(x => executeResults.OnNext(x)) .Catch(ex => { exceptions.OnNext(ex); - return Observable.Empty(); + return Observable.Throw(ex); }) .Subscribe(subj); @@ -446,7 +446,7 @@ public bool CanExecute(object parameter) public void Execute(object parameter) { - ExecuteAsync(parameter).Subscribe(); + ExecuteAsync(parameter).Catch(Observable.Empty()).Subscribe(); } public void Dispose() From 09ae483231360bf348b1eaec7f6e7a7ffe2743ab Mon Sep 17 00:00:00 2001 From: Dennis Daume Date: Wed, 30 Jul 2014 19:35:36 +0200 Subject: [PATCH 2/2] Remove the Catch and OnNext the error on the Do method above --- ReactiveUI/ReactiveCommand.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/ReactiveUI/ReactiveCommand.cs b/ReactiveUI/ReactiveCommand.cs index 6bc199dd54..d92b6e3b45 100644 --- a/ReactiveUI/ReactiveCommand.cs +++ b/ReactiveUI/ReactiveCommand.cs @@ -371,11 +371,7 @@ public IObservable ExecuteAsync(object parameter = null) var disp = executeAsync(parameter) .ObserveOn(scheduler) .Finally(() => decrement.Disposable = Disposable.Empty) - .Do(x => executeResults.OnNext(x)) - .Catch(ex => { - exceptions.OnNext(ex); - return Observable.Throw(ex); - }) + .Do(x => executeResults.OnNext(x), ex => exceptions.OnNext(ex)) .Subscribe(subj); return new CompositeDisposable(disp, decrement);