Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include inner exception message in outer exception message when wrapping exceptions from constructors #343

Closed
alexmg opened this issue Jan 22, 2014 · 6 comments

Comments

@alexmg
Copy link
Member

alexmg commented Jan 22, 2014

From rikard.p...@gmail.com on September 06, 2011 17:39:15

I have a problem propagating exceptions from my library.
For example:

public class ThrowException
{
public ThrowException()
{
throw new FileNotFoundException("Can't find file");
}
}

[TestMethod]
public void CorrectExceptionWhenResolving()
{
var builder = new ContainerBuilder();
builder.RegisterType();
var container = builder.Build();

try
{
    container.Resolve<ThrowException>();
    Assert.Fail("Exception expected");
}
catch(FileNotFoundException)
{
}
catch(DependencyResolutionException dre)
{
    Assert.IsTrue(dre.Message.Contains("Can't find file"));
}
catch
{
    Assert.Fail("Unexpected exception");
}

}

Ideally I would like to catch FileNotFoundException, but I understand why that is not possible.
What I think would be great is that DependencyResolutionException message contains inner exception message. That way I don't need to explain to users that they should look at the inner exception instead of main exception.

For example this message "An exception was thrown while invoking the constructor 'Void .ctor()' on type 'ThrowException'. Exception: Can't find file" would be much better.

Regards,
Rikard

Original issue: http://code.google.com/p/autofac/issues/detail?id=343

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From nicholas...@gmail.com on January 22, 2012 21:14:05

Thanks Rikard, sounds like a good suggestion.

Status: Accepted
Labels: -Type-Defect Type-Enhancement

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From nicholas...@gmail.com on January 22, 2012 21:16:33

Summary: Include inner exception message in outer exception message when wrapping exceptions from constructors

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on September 21, 2012 09:33:31

Labels: Module-Core

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From j...@q42.nl on September 25, 2012 02:05:00

In the mean time, is it possible to log the exception?

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on January 02, 2013 12:26:27

Owner: travis.illig

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on January 02, 2013 12:45:13

The message for DependencyResolutionException now includes the inner exception message if there is one. It will appear like:

"Unable to resolve component. ---> Can't find file. (See inner exception for details.)"

If the complete stack trace and everything for the inner exception is desired, use ToString as with other exception types.

There is currently no logging built into Autofac. If you want the exception logged, I'd recommend implementing a logging solution in your project. Adding logging to Autofac is outside the scope of this issue.

Status: Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant