Last active
November 7, 2019 15:58
-
-
Save mikeblakeuk/c45156de2d1e362e66b3638e343b551a to your computer and use it in GitHub Desktop.
GetBaseException vs InnerException
This file contains 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
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Machine.Specifications; | |
// ReSharper disable PossibleNullReferenceException when getting InnerException from AggregateException | |
namespace Tests | |
{ | |
[Subject(typeof(AggregateException))] | |
public class AggregateExceptionTests | |
{ | |
It should_handle_exceptions = () => | |
{ | |
var taskException = new ArithmeticException("test1"); | |
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException)); | |
exception.GetBaseException().Message.ShouldEqual("test1"); | |
exception.InnerException.Message.ShouldEqual("test1"); | |
}; | |
It should_handle_AggregateException_exceptions = () => | |
{ | |
var taskException = new AggregateException(new ArithmeticException("test1")); | |
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException)); | |
exception.GetBaseException().Message.ShouldEqual("test1"); | |
exception.InnerException.Message.ShouldEqual("One or more errors occurred."); | |
}; | |
It should_handle_AggregateException_with_AggregateException_exception = () => | |
{ | |
var taskException = new AggregateException(new AggregateException(new ArithmeticException("test1"))); | |
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException)); | |
exception.GetBaseException().Message.ShouldEqual("test1"); | |
exception.InnerException.Message.ShouldEqual("One or more errors occurred."); | |
}; | |
It should_handle_AggregateException_with_multi_inner_exceptions = () => | |
{ | |
var taskException = new AggregateException(new ArithmeticException("test1"), new ArithmeticException("test2")); | |
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException)); | |
var multipleExceptions = exception.GetBaseException() as AggregateException; | |
exception.Message.ShouldEqual("One or more errors occurred."); | |
exception.InnerException.Message.ShouldEqual("One or more errors occurred."); | |
multipleExceptions.Message.ShouldEqual("One or more errors occurred."); | |
multipleExceptions.GetBaseException().Message.ShouldEqual("One or more errors occurred."); | |
multipleExceptions.InnerExceptions[0].Message.ShouldEqual("test1"); | |
multipleExceptions.InnerExceptions[1].Message.ShouldEqual("test2"); | |
multipleExceptions.InnerException.Message.ShouldEqual("test1"); | |
}; | |
It should_handle_AggregateException_with_inner_inner_exceptions = () => | |
{ | |
var taskException = new AggregateException(new ArithmeticException("test1", new ArithmeticException("test1a"))); | |
var exception = Catch.Only<AggregateException>(() => AsyncThrow(taskException)); | |
exception.GetBaseException().Message.ShouldEqual("test1"); | |
exception.InnerException.Message.ShouldEqual("One or more errors occurred."); | |
}; | |
public static void AsyncThrow(Exception exception) | |
{ | |
Throw(exception).Wait(); | |
} | |
public static async Task Throw(Exception exception) | |
{ | |
await Task.Run(() => { throw exception; }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment