Skip to content

Make ExecuteAsync throw the original exception #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ReactiveUI.Tests/ReactiveCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,37 @@ public void NoSubscriberOfThrownExceptionsEqualsDeath()
Assert.False(failed);
});
}

[Fact]
public async Task ExecuteAsyncThrowsExceptionOnError()
{
var command = ReactiveCommand.CreateAsyncObservable(_ =>
Observable.Throw<Unit>(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<Unit>(new Exception("Aieeeee!")));

command.ThrownExceptions.Subscribe();

command.Execute(null);
}
}

public class ReactiveAsyncCommandTest
Expand Down
8 changes: 2 additions & 6 deletions ReactiveUI/ReactiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,7 @@ public IObservable<T> ExecuteAsync(object parameter = null)
var disp = executeAsync(parameter)
.ObserveOn(scheduler)
.Finally(() => decrement.Disposable = Disposable.Empty)
.Do(x => executeResults.OnNext(x))
.Catch<T, Exception>(ex => {
exceptions.OnNext(ex);
return Observable.Empty<T>();
})
.Do(x => executeResults.OnNext(x), ex => exceptions.OnNext(ex))
.Subscribe(subj);

return new CompositeDisposable(disp, decrement);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can nuke the Catch block here and just use the OnError clause of the Do above it

Expand Down Expand Up @@ -446,7 +442,7 @@ public bool CanExecute(object parameter)

public void Execute(object parameter)
{
ExecuteAsync(parameter).Subscribe();
ExecuteAsync(parameter).Catch(Observable.Empty<T>()).Subscribe();
}

public void Dispose()
Expand Down