Browse Source

DialogErrorHandler add support for void Tasks

pull/5/head
Ionite 1 year ago
parent
commit
20c1c85537
No known key found for this signature in database
  1. 27
      StabilityMatrix/Helper/DialogErrorHandler.cs
  2. 8
      StabilityMatrix/Helper/IDialogErrorHandler.cs

27
StabilityMatrix/Helper/DialogErrorHandler.cs

@ -42,7 +42,7 @@ public class DialogErrorHandler : IDialogErrorHandler
}
/// <summary>
/// Attempt to run the given action, showing a generic error snackbar if it fails.
/// Attempt to run the given task, showing a generic error snackbar if it fails.
/// </summary>
public async Task<TaskResult<T>> TryAsync<T>(Task<T> task, string message, LogLevel level = LogLevel.Error, int timeoutMilliseconds = 5000)
{
@ -62,4 +62,29 @@ public class DialogErrorHandler : IDialogErrorHandler
};
}
}
/// <summary>
/// Attempt to run the given void task, showing a generic error snackbar if it fails.
/// Return a TaskResult with true if the task succeeded, false if it failed.
/// </summary>
public async Task<TaskResult<bool>> TryAsync(Task task, string message, LogLevel level = LogLevel.Error, int timeoutMilliseconds = 5000)
{
try
{
await task;
return new TaskResult<bool>
{
Result = true
};
}
catch (Exception e)
{
ShowSnackbarAsync(message, level, timeoutMilliseconds);
return new TaskResult<bool>
{
Result = false,
Exception = e
};
}
}
}

8
StabilityMatrix/Helper/IDialogErrorHandler.cs

@ -12,7 +12,13 @@ public interface IDialogErrorHandler
void ShowSnackbarAsync(string message, LogLevel level = LogLevel.Error, int timeoutMilliseconds = 5000);
/// <summary>
/// Attempt to run the given action, showing a generic error snackbar if it fails.
/// Attempt to run the given task, showing a generic error snackbar if it fails.
/// </summary>
Task<TaskResult<T>> TryAsync<T>(Task<T> task, string message, LogLevel level = LogLevel.Error, int timeoutMilliseconds = 5000);
/// <summary>
/// Attempt to run the given void task, showing a generic error snackbar if it fails.
/// Return a TaskResult with true if the task succeeded, false if it failed.
/// </summary>
Task<TaskResult<bool>> TryAsync(Task task, string message, LogLevel level = LogLevel.Error, int timeoutMilliseconds = 5000);
}

Loading…
Cancel
Save