Skip to content
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
30 changes: 30 additions & 0 deletions src/ReactiveUI.Tests/Commands/ReactiveCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,5 +1151,35 @@ public void SynchronousCommandsFailCorrectly()
fixture4.Execute().Subscribe(_ => { }, _ => { });
Assert.Equal(4, failureCount);
}

[Fact]
public async Task ReactiveCommandCreateFromTaskHandlesTaskExceptionAsync()
{
var subj = new Subject<Unit>();
var isExecuting = false;
Exception? fail = null;
var fixture = ReactiveCommand.CreateFromTask(async _ =>
{
await subj.Take(1);
throw new Exception("break execution");
});
fixture.IsExecuting.Subscribe(x => isExecuting = x);
fixture.ThrownExceptions.Subscribe(ex => fail = ex);

Assert.False(isExecuting);
Assert.Null(fail);

fixture.Execute().Subscribe();
Assert.True(isExecuting);
Assert.Null(fail);

subj.OnNext(Unit.Default);

// Wait 1 ms to allow execution to complete
await Task.Delay(1).ConfigureAwait(false);

Assert.False(isExecuting);
Assert.Equal("break execution", fail?.Message);
}
}
}
54 changes: 29 additions & 25 deletions src/ReactiveUI/ReactiveCommand/ReactiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,19 +804,21 @@ public override IObservable<TResult> Execute(TParam parameter)
_synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateBegin());
return Observable<TResult>.Empty;
}).Concat(_execute(parameter))
.Do(result => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateResult(result)))
.Catch<TResult, Exception>(
ex =>
{
_exceptions.OnNext(ex);
return Observable.Throw<TResult>(ex);
}).Finally(() => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd()))
.PublishLast()
.RefCount()
.ObserveOn(_outputScheduler);
.Do(result => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateResult(result)))
.Catch<TResult, Exception>(
ex =>
{
_synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd());
_exceptions.OnNext(ex);
return Observable.Throw<TResult>(ex);
}).Finally(() => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd()))
.PublishLast()
.RefCount()
.ObserveOn(_outputScheduler);
}
catch (Exception ex)
{
_synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd());
_exceptions.OnNext(ex);
return Observable.Throw<TResult>(ex);
}
Expand All @@ -828,24 +830,26 @@ public override IObservable<TResult> Execute()
try
{
return Observable.Defer(
() =>
{
_synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateBegin());
return Observable<TResult>.Empty;
}).Concat(_execute(default!))
.Do(result => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateResult(result)))
.Catch<TResult, Exception>(
ex =>
{
_exceptions.OnNext(ex);
return Observable.Throw<TResult>(ex);
}).Finally(() => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd()))
.PublishLast()
.RefCount()
.ObserveOn(_outputScheduler);
() =>
{
_synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateBegin());
return Observable<TResult>.Empty;
}).Concat(_execute(default!))
.Do(result => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateResult(result)))
.Catch<TResult, Exception>(
ex =>
{
_synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd());
_exceptions.OnNext(ex);
return Observable.Throw<TResult>(ex);
}).Finally(() => _synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd()))
.PublishLast()
.RefCount()
.ObserveOn(_outputScheduler);
}
catch (Exception ex)
{
_synchronizedExecutionInfo.OnNext(ExecutionInfo.CreateEnd());
_exceptions.OnNext(ex);
return Observable.Throw<TResult>(ex);
}
Expand Down