Created
August 24, 2017 17:27
-
-
Save the-tatarinov-av/f6d9337ba1c49946beaed15feaa58bfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class TaskExtensions | |
| { | |
| public static Task Catch(this Task task, Action<Exception> exceptionHandler) | |
| { | |
| return task.ContinueWith(t => | |
| { | |
| if (t.IsCanceled || !t.IsFaulted || exceptionHandler == null) | |
| { | |
| return; | |
| } | |
| var innerException = t.Exception?.Flatten().InnerExceptions.FirstOrDefault(); | |
| exceptionHandler(innerException ?? t.Exception); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment