如何访问PHP异常的内部异常
When I make __soapCall
(client pointing to a WSDL) and the request is invalid in the server side, the caught exception's message is coming out as general as this:
An error occurred while executing the command definition. See the inner exception for details.
This is an example of one of my try-catch blocks:
try {
$soapCallResult = $client->myWSDLMethod(array(...));
} catch (Exception $e) {
echo 'Exception in myWSDLMethod: ', $e->getMessage(), PHP_EOL;
}
When I var_dump the getTrace()
, the array only points to the file and line where I make this call, which is not useful at all... It seems there is no getInnerExceptionMessage()
method in the Exception class or something remotely similar to that. So how should I access that inner exception?
If you var_dump($e->detail)
, you can see the entire Exception's detail object and access the inner exception's message through the following path:
$e->detail->ExceptionDetail->InnerException->Message
You may be interested in printing more fields from this object. Here's a dump:
object(stdClass)[?]
public 'ExceptionDetail' =>
object(stdClass)[?]
public 'HelpLink' => null
public 'InnerException' =>
object(stdClass)[?]
public 'HelpLink' => null
public 'InnerException' => null
public 'Message' => string 'SERVER ERROR MSG: ...' (length=?)
public 'StackTrace' => string ' at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)'... (length=?)
public 'Type' => string 'System.Data.SqlClient.SqlException' (length=34)
public 'Message' => string 'An error occurred while executing the command definition. See the inner exception for details.' (length=94)
...
Hope it helps.