CommonExceptionExtensions.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Ips.Library.Basic
  7. {
  8. public static class CommonExceptionExtensions
  9. {
  10. public static void HandleCancelEx(this Exception source, Action<Exception> handleAct, Action<OperationCanceledException> opCancelAct = null)
  11. {
  12. if (source == null) return;
  13. var aggex = source as AggregateException;
  14. if (aggex != null)
  15. {
  16. aggex.Flatten().Handle(ex =>
  17. {
  18. HandleEx(ex, handleAct, opCancelAct);
  19. return true;
  20. });
  21. }
  22. else
  23. {
  24. HandleEx(source, handleAct, opCancelAct);
  25. }
  26. }
  27. private static void HandleEx(Exception ex, Action<Exception> handleAct, Action<OperationCanceledException> opCancelAct)
  28. {
  29. if (ex is OperationCanceledException)
  30. {
  31. opCancelAct?.Invoke((OperationCanceledException)ex);
  32. }
  33. else
  34. {
  35. handleAct(ex);
  36. }
  37. }
  38. }
  39. }