2

I have the following custom exception :

 public class MyCustomException : Exception
    {
        public MyCustomException(string message) : base(message)
        { }

    }

I throw it at this point:

 private async Task<string> AquireToken()
        {
            string url = GetUrl("authentication/connect/token");
           
//...
 private static string GetUrl(string relativeUrl)
        {
            var baseUrl = Environment.GetEnvironmentVariable("BASE_URL");
            if (string.IsNullOrEmpty(baseUrl))
            {
                throw new MyCustomException("Address is not set in Enviroment Variable (BASE_URL)");
            }
            var fullUrl = $"{baseUrl.Trim('/')}/{relativeUrl}";
            return fullUrl;
        }

But when testing, I find that it is wrapped by an AggregateException, the test fails:

 MyCustomException exception = await Assert.ThrowsAsync<MyCustomException>(async () =>
            {
                Environment.SetEnvironmentVariable("BASE_URL", null);
                await serviceUnderTest.SampleMethod(input);
            });
Assert.Throws() Failure
Expected: typeof(SAMPLECOMPANY.SAMPLEPROJECT.SampleMicroservice.WebApi.Service.Exceptions.MyCustomException)
Actual:   typeof(System.AggregateException): One or more errors occurred. (Address is not set in Enviroment Variable (BASE_URL))
---- System.AggregateException : One or more errors occurred. (Address is not set in Enviroment Variable (BASE_URL))
-------- SAMPLECOMPANY.SAMPLEPROJECT.SampleMicroservice.WebApi.Service.Exceptions.MyCustomException : Address is not set in Enviroment Variable (BASE_URL)

At other places in the same class I throw it too (for example in the method SampleMethod() that calls AquireToken() and, there I only get the custom exception.

I am confused, because in other projects I do supposedly analogous and there the exceptions are not wrapped....

What does it depend on, if the exception of AggregateException is wrapped or not, how can I avoid it?

DaveVentura
  • 612
  • 5
  • 19
  • https://learn.microsoft.com/en-us/dotnet/api/system.aggregateexception?view=net-5.0#remarks -> from the code, it looks like that's something being done by your unit testing framework – Camilo Terevinto Nov 05 '21 at 11:09
  • In your test code you are calling the `SampleMethod`. How does it differ from your `AquireToken` method? – Peter Csala Nov 05 '21 at 11:23
  • @PeterCsala AquireToke is called by SampleMethod – DaveVentura Nov 05 '21 at 13:00
  • 1
    It is the behavior of Task, any exception raised in code that it runs gets wrapped by AggregateException. Iterate its InnerExceptions property to look for the expected exception. – Hans Passant Nov 05 '21 at 14:02
  • Please post a minimal, reproducible example. It's normal for an `AggregateException` wrapper to be seen when using blocking methods like `Result` and `Wait()`, but not when using `await`. – Stephen Cleary Nov 15 '21 at 15:06

1 Answers1

2

You are starting a task here by using await/aync and you are not handling exception. That is why you are getting aggregate exception. From MS docs

https://learn.microsoft.com/en-us/dotnet/api/system.aggregateexception.handle?view=netframework-4.7.2

 MyCustomException exception = await Assert.ThrowsAsync<MyCustomException>(async () =>
        {
            Environment.SetEnvironmentVariable("BASE_URL", null);
            await serviceUnderTest.SampleMethod(input);
        });
SmartCoder
  • 856
  • 6
  • 11