Skip to content

Commit

Permalink
chore: handle exceptions on CanExecute
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jul 9, 2023
1 parent 7ca126d commit ee47bff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/Prism.Core/Commands/DelegateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ public void Execute()
/// <returns>Returns <see langword="true"/> if the command can execute,otherwise returns <see langword="false"/>.</returns>
public bool CanExecute()
{
return _canExecuteMethod();
try
{
return _canExecuteMethod();
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;

ExceptionHandler.Handle(ex, null);

return false;
}
}

/// <summary>
Expand Down
30 changes: 28 additions & 2 deletions src/Prism.Core/Commands/DelegateCommand{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,19 @@ public void Execute(T parameter)
///</returns>
public bool CanExecute(T parameter)
{
return _canExecuteMethod(parameter);
try
{
return _canExecuteMethod(parameter);
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;

ExceptionHandler.Handle(ex, parameter);

return false;
}
}

/// <summary>
Expand Down Expand Up @@ -133,7 +145,21 @@ protected override void Execute(object parameter)
/// <returns><see langword="true"/> if the Command Can Execute, otherwise <see langword="false" /></returns>
protected override bool CanExecute(object parameter)
{
return CanExecute((T)parameter);
try
{
// Note: We don't call Execute because we would potentially invoke the Try/Catch twice.
// It is also needed here incase (T)parameter throws the exception
return CanExecute((T)parameter);
}
catch (Exception ex)
{
if (!ExceptionHandler.CanHandle(ex))
throw;

ExceptionHandler.Handle(ex, parameter);

return false;
}
}

/// <summary>
Expand Down

0 comments on commit ee47bff

Please sign in to comment.