question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Add support for recursive checking of inner exception

See original GitHub issue

Description

Sometimes, when writing high-level tests, we expect a function to throw an exception with a deeply nested inner exception. Writing assertions can be tedious if we only care about the existence of a certain type in the chain and not how deep it is located.

So in short, I would like to write something like this:

action.Should().Throw<ExceptionA>()
    .WithInnerException<ExceptionB>(recursive: true);

instead of:

action.Should().Throw<ExceptionA>()
    .Which.InnerException.InnerException.InnerException.BeOfType<ExceptionB>();

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:33 (25 by maintainers)

github_iconTop GitHub Comments

4reactions
dennisdoomencommented, Jan 18, 2022

I think that’s a great idea and I would prefer WhoseInnerExceptionChain to emphasize that that it exposes not just the direct inner exceptions.

3reactions
julealgoncommented, Jan 17, 2022

Another possibility for this would be to convert it into a normal collection assertion with something like:

action.Should().Throw<ExceptionA>().WhoseInnerExceptionChain.Should().Contain<ExceptionB>();

This would be similar to the WhoseValue used in places like dictionaries and leverage all the collection assertions that already exist on top of the flattened list of exceptions (that could also be empty). It could also be a computed IEnumerable (i.e. deferred execution), depending on how we want to go about it.

I think the “Chain” wording at the end makes sense, but perhaps there is a better way to put it (perhaps just WhoseInnerExceptions, even though that could be a bit misleading when dealing with an AggregateException).

From what I’m seeing in the docs, it seems we don’t have a generic Contain<T> assertion, so I’d probably add that as well to make this work, since it would be a valid assertion to have for normal collections too (would be useful for polymorphic collections). Naming could either be Contain<T>, or maybe a more explicit ContainItemOfType<T> to go along the other “Contain…Items…” methods. While we are at it, a “Single” version should also be added to match.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetching the Last inner Message from C# exception Object ...
I have modified your code you do not need to set the message to empty while you checking ex.InnerException == null just return...
Read more >
Using recursive queries - Db2 for i SQL
In a connect by query you can use any type of join supported by Db2 for i including INNER JOIN, LEFT OUTER JOIN,...
Read more >
Recursive methods using C# - CodeProject
InnerExceptions. Recursive methods are useful for getting the last innerException : C#. public Exception GetInnerException(Exception ex) ...
Read more >
StackOverflowException Class (System)
The exception that is thrown when the execution stack exceeds the stack size. This class cannot be inherited.
Read more >
AWS Lambda function errors in C# - ...
This page describes how to view Lambda function invocation errors for the C# runtime using the Lambda console and the AWS CLI.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found