framework: SoapFault exception cannot be caught when using SoapClient in Laravel
try {
$client = new SoapClient("wsdl-url-here",array('trace' => 1,'exceptions'=> true));
} catch (SoapFault $fault){
echo $fault->faultstring;
}
In the example above, if I have invalid WSDL URL, I am supposed to get SoapFault exception since I set exceptions to true. However, Laravel catches this as FATAL exception and the application crashes. This code works as expected outside of Laravel.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 1
- Comments: 27 (7 by maintainers)
Actually, the
SoapFault
is successfully caught. However, Laravel is handling the last error in itshandleShutdown
method. Therefore, clear the last error to avoid the fatal error exception:Tested in Laravel 5.2.
EDIT: Still working in Laravel 5.7. Also, updated comment to use
error_clear_last()
introduced in PHP 7. For versions below PHP 7.0 replaceerror_clear_last()
with:Set your
error_reporting
to not includeE_ERROR
.I have tried that as well, it still crashed with FATAL exception.
The solution is to actually ask the Soap client to throw a SoapFault instead of reporting an E_ERROR.
When the Soap client reports an E_ERROR, there is nothing for you to catch.
To fix this initialise you SoapClient like this:
hi i have this problem and it is uncatchable i think.
This is working solution, soap thow exception and error than can not be catch. In catch SoapFault on php 7 can do this:
Less code.
My workaround in App\Exceptions\Handler for console application: