dongfo / autofac

Automatically exported from code.google.com/p/autofac
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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<ThrowException>();
    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 reported on code.google.com by rikard.p...@gmail.com on 6 Sep 2011 at 7:39

GoogleCodeExporter commented 8 years ago
Thanks Rikard, sounds like a good suggestion.

Original comment by nicholas...@gmail.com on 23 Jan 2012 at 5:14

GoogleCodeExporter commented 8 years ago

Original comment by nicholas...@gmail.com on 23 Jan 2012 at 5:16

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 21 Sep 2012 at 4:33

GoogleCodeExporter commented 8 years ago
In the mean time, is it possible to log the exception?

Original comment by j...@q42.nl on 25 Sep 2012 at 9:05

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 2 Jan 2013 at 8:26

GoogleCodeExporter commented 8 years ago
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.

Original comment by travis.illig on 2 Jan 2013 at 8:45