Created
December 10, 2014 17:00
-
-
Save ashokgelal/8f3bc3b21fdb0c5da993 to your computer and use it in GitHub Desktop.
MEF Exception Unwrapper
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
/// <summary> | |
/// Attempts to retrieve the real cause of a composition failure. | |
/// </summary> | |
/// <remarks> | |
/// Sometimes a MEF composition fails because an exception occurs in the ctor of a type we're trying to | |
/// create. Unfortunately, the CompositionException doesn't make that easily available, so we don't get | |
/// that info in haystack. This method tries to find that exception as that's really the only one we care | |
/// about if it exists. If it can't find it, it returns the original composition exception. | |
/// </remarks> | |
/// <param name="exception"></param> | |
/// <returns></returns> | |
public static Exception UnwrapCompositionException(this Exception exception) | |
{ | |
var compositionException = exception as CompositionException; | |
if (compositionException == null) | |
{ | |
return exception; | |
} | |
var unwrapped = compositionException; | |
while (unwrapped != null) | |
{ | |
var firstError = unwrapped.Errors.FirstOrDefault(); | |
if (firstError == null) | |
{ | |
break; | |
} | |
var currentException = firstError.Exception; | |
if (currentException == null) | |
{ | |
break; | |
} | |
var composablePartException = currentException as ComposablePartException; | |
if (composablePartException != null | |
&& composablePartException.InnerException != null) | |
{ | |
var innerCompositionException = composablePartException.InnerException as CompositionException; | |
if (innerCompositionException == null) | |
{ | |
return currentException.InnerException ?? exception; | |
} | |
currentException = innerCompositionException; | |
} | |
unwrapped = currentException as CompositionException; | |
} | |
return exception; // Fuck it, couldn't find the real deal. Throw the original. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment