cakephp: SOAP Client exception cannot be caught

This is a (multiple allowed):

  • bug
  • enhancement
  • feature-discussion (RFC)
  • CakePHP Version: CakePHP Version: 3.2.3
  • Platform and Target: Apache 2.4.12 (Win32) PHP 5.6.11 MySQL 5.6.25, GET

What you did

I tried to call a SOAP API as a client and catch a fatal error, caused by missing WSDL file or network connectivity issues.

try {
    $soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
} 
catch (SoapFault $e) {
    echo 'a soap error occurred<br>';
} 
echo 'I can still do stuff after the error<br>';

The code above works in vanilla PHP, but not in CakePHP.

Expected Behavior

I should be able to catch the exception

Actual Behavior

The exception is not caught

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (13 by maintainers)

Commits related to this issue

Most upvoted comments

@josegonzalez I came up with a workaround.

$restore = error_reporting(0);
try {
    $soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
}
catch (SoapFault $e) {
    trigger_error($e->getMessage()); // Overwrites E_ERROR with E_USER_NOTICE
}
finally {
    error_reporting($restore);
}

Edit: use trigger_error() and keep the exception message.

I just want to add I came across this problem and the suggested solution wasn’t working for me what did work was catching \SoapFault with the backslash:

try
 {
   $client = new SoapClient( $wsdl, $this->config );
   $response = $client->SubmitJob( $param );
}
catch (\SoapFault $fault) 
{
  // custom logging here
}