Skip to content

Commit

Permalink
chore: remove TaskCanceledException catch
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Nov 14, 2023
1 parent 8a60502 commit 56e4d88
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
24 changes: 4 additions & 20 deletions src/Prism.Core/Commands/AsyncDelegateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ public async Task Execute(CancellationToken? cancellationToken = null)
await _executeMethod(token)
.ConfigureAwait(false);
}
catch (TaskCanceledException) when (token.IsCancellationRequested)
{
// Do nothing... the Task was cancelled
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
Expand Down Expand Up @@ -145,23 +141,11 @@ public bool CanExecute()
/// <param name="parameter">Command Parameter</param>
protected override async void Execute(object? parameter)
{
// We don't want to wrap this in a try/catch because we already handle
// or mean to rethrow the exception in the call with the CancellationToken.
var cancellationToken = _getCancellationToken();
try
{
await Execute(cancellationToken)
.ConfigureAwait(false);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Do nothing... the Task was cancelled
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;

ExceptionHandler.Handle(ex, parameter);
}
await Execute(cancellationToken)
.ConfigureAwait(false);
}

/// <summary>
Expand Down
19 changes: 9 additions & 10 deletions src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ public async Task Execute(T parameter, CancellationToken? cancellationToken = nu
await _executeMethod(parameter, token)
.ConfigureAwait(false);
}
catch (TaskCanceledException) when (token.IsCancellationRequested)
{
// Do nothing... the Task was cancelled
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
Expand Down Expand Up @@ -149,22 +145,25 @@ public bool CanExecute(T parameter)
protected override async void Execute(object? parameter)
{
var cancellationToken = _getCancellationToken();
T parameterAsT;
try
{
await Execute((T)parameter!, cancellationToken)
.ConfigureAwait(false);
}
catch (TaskCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Do nothing... the Task was cancelled
parameterAsT = (T)parameter!;
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;

ExceptionHandler.Handle(ex, parameter);
return;
}

// If we had an exception casting the parameter to T ,
// we would have already returned. We want to surface any
// exceptions thrown by the Execute method.
await Execute(parameterAsT, cancellationToken)
.ConfigureAwait(false);
}

/// <summary>
Expand Down

0 comments on commit 56e4d88

Please sign in to comment.