Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the real exception from FaultException<>

I catch a FaultException while calling a service API, e.g.

catch(FaultException<MyCustomEx> e)
{
    // How do I get the MyCustomEx object here?
}

I want to call some method on the MyCustomEx object embedded inside.

e.getTheActualExc().getMyCustomErrorCode();

How do I get the actual object?

like image 836
user93353 Avatar asked May 30 '14 13:05

user93353


Video Answer


2 Answers

According to

http://msdn.microsoft.com/ru-ru/library/ms576199(v=vs.110).aspx

The required property is Detail:

try {
  ...
}
catch(FaultException<MyCustomEx> e) {
  MyCustomEx detail = e.Detail;
  ...
}

Or, if you have to catch FaultException class, you can use Reflection:

  try {
    ...
  }
  catch (FaultException e) {
    PropertyInfo pi = e.GetType().GetProperty("Detail");

    if (pi != null) {
      Object rawDetail = pi.GetValue(e); 

      MyCustomEx detail = rawDetail as MyCustomEx;

      if (detail != null) {
        ...
      }
      ...
    }
    ...
  }
like image 156
Dmitry Bychenko Avatar answered Nov 15 '22 02:11

Dmitry Bychenko


the e.Detail property will give you the object of the FaultException<> , see MSDN.

like image 22
quantdev Avatar answered Nov 15 '22 04:11

quantdev