private static void _unpack(PrexoniteRuntimeException originalException, out Exception lowestException, out PrexoniteRuntimeException lowestRuntimeException) { Exception exc = originalException; TargetInvocationException targetInvocationExc = null; lowestRuntimeException = originalException; while ( (exc is PrexoniteRuntimeException || (targetInvocationExc = exc as TargetInvocationException) != null) && exc.InnerException != null) { if (targetInvocationExc == null) lowestRuntimeException = (PrexoniteRuntimeException) exc; exc = exc.InnerException; } lowestRuntimeException = (exc as PrexoniteRuntimeException) ?? lowestRuntimeException; lowestException = exc; }
/// <summary> /// Tries to unwrap the inner exception of the supplied <see cref = "PrexoniteRuntimeException" /> so /// the inner exception of the returned <see cref = "PrexoniteRuntimeException" /> contains relevant information. /// </summary> /// <remarks> /// The method replaces <see cref = "PrexoniteRuntimeException" />s as well as /// <see cref = "TargetInvocationException" />s with their inner exceptions (unless they are null). /// In some cases, a new <see cref = "PrexoniteRuntimeException" /> instance is created. /// </remarks> /// <param name = "pExc">Any <see cref = "PrexoniteRuntimeException" />.</param> /// <returns>An instance of <see cref = "PrexoniteRuntimeException" /> with a relevant inner exception.</returns> public static PrexoniteRuntimeException UnpackException(PrexoniteRuntimeException pExc) { if (pExc == null) throw new ArgumentNullException("pExc"); //Exception exc = pExc; //Exception lastExc = null; //while ( // (exc is PrexoniteRuntimeException || exc is TargetInvocationException) && // exc.InnerException != null) //{ // var ipexc = exc as PrexoniteRuntimeException; // if (ipexc != null) // lastpExc = ipexc; // lastExc = exc; // exc = exc.InnerException; //} //if (ReferenceEquals(exc, pExc)) // return pExc; //No unpacking needed //if (ReferenceEquals(lastExc, pExc)) // return pExc; //Use lowest prexonite runtime exception //else // //Construct new runtime exception // return // new PrexoniteRuntimeException(exc.Message, exc, pExc._prexoniteStackTrace); PrexoniteRuntimeException lowestRuntimeException; Exception lowestException; if (pExc.InnerException == null) return pExc; _unpack(pExc, out lowestException, out lowestRuntimeException); //Check if something changed if (ReferenceEquals(lowestException, pExc.InnerException) && ReferenceEquals(lowestRuntimeException, pExc)) return pExc; return new PrexoniteRuntimeException(lowestException.Message, lowestException, lowestRuntimeException._prexoniteStackTrace); }