From 56e4d88db0a1de0945215d2e930d848ad225baf3 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Mon, 13 Nov 2023 18:22:56 -0600 Subject: [PATCH] chore: remove TaskCanceledException catch --- .../Commands/AsyncDelegateCommand.cs | 24 ++++--------------- .../Commands/AsyncDelegateCommand{T}.cs | 19 +++++++-------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/Prism.Core/Commands/AsyncDelegateCommand.cs b/src/Prism.Core/Commands/AsyncDelegateCommand.cs index e748ad40d8..8c7eec460f 100644 --- a/src/Prism.Core/Commands/AsyncDelegateCommand.cs +++ b/src/Prism.Core/Commands/AsyncDelegateCommand.cs @@ -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)) @@ -145,23 +141,11 @@ public bool CanExecute() /// Command Parameter 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); } /// diff --git a/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs b/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs index 4ed68524da..a449f5c67b 100644 --- a/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs +++ b/src/Prism.Core/Commands/AsyncDelegateCommand{T}.cs @@ -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)) @@ -149,14 +145,10 @@ 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) { @@ -164,7 +156,14 @@ await Execute((T)parameter!, cancellationToken) 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); } ///