How to Get Exception Detail
You're not getting the InnerExceptionMessage because you haven't set it anywhere.
private static CustomServiceFault GetCustomException(Exception exception)
{
var customServiceFault = new CustomServiceFault
{
ErrorMessage = exception.Message,
Source = exception.Source,
StackTrace = exception.StackTrace,
Target = exception.TargetSite.ToString(),
// You should fill this property with details here.
InnerExceptionMessage = exception.InnerException.Message;
};
return customServiceFault;
}
obautista
Updated on June 09, 2022Comments
-
obautista almost 2 years
I have a WCF service where I've implemented a custom Service Fault. I am throwing an error based a specific condition, so in an if statement I throw the error below.
throw new FaultException<CustomServiceFault>( new CustomServiceFault { ErrorMessage = string.Format( "Error in response, code:{0} message:{1}", response.Error.Code, response.Error.Message), Source = "ConnectToOpenLdap Method", StackTrace = string.Format( "Error in response, code:{0} message:{1}", response.Error.Code, response.Error.Message) }, new FaultReason( string.Format(CultureInfo.InvariantCulture, "{0}", "Service fault exception")));
In the catch I rethrow the exception like this:
catch (Exception exception) { var customServiceFault = GetCustomException(exception); throw new FaultException<CustomServiceFault>( customServiceFault, new FaultReason(customServiceFault.ErrorMessage), new FaultCode("Sender")); }
The GetCustomException() method simply converts the exception to my custom exception object.
The problem is exception that gets passed to GetCustomException() does not have the details in the InnerException property. Screenshot of what I see:
How do I extract or get the custom ErrorMessage, Source, etc I set when throwing the exception in the if condition? As you can see in the screenshot expanding "exception" shows the object type (I believe) and inside of that "Detail" shows the ErrorMessage, InnerExceptionMesage, Source, and StackTrace. This is what I am after. How do I extract these values in the GetCustomException() method?
This is the GetCustomException() method:
private static CustomServiceFault GetCustomException(Exception exception) { var customServiceFault = new CustomServiceFault { ErrorMessage = exception.Message, Source = exception.Source, StackTrace = exception.StackTrace, Target = exception.TargetSite.ToString() }; return customServiceFault; }
CustomServiceFault class:
[DataContract] public class CustomServiceFault { [DataMember] public string ErrorMessage { get; set; } [DataMember] public string StackTrace { get; set; } [DataMember] public string Target { get; set; } [DataMember] public string Source { get; set; } [DataMember] public string InnerExceptionMessage { get; set; } }