simonsays Posted November 9, 2018 Share Posted November 9, 2018 (edited) Hello, I am facing a mysterious issue. I have a module for connecting to external SOAP service (which is initialized along with the project). Since SOAP service times out every now and then, it is crucial to handle this timeout and hide error from the user. I have this bit of code in the module public function init() { ini_set('default_socket_timeout', 5); $configData = $this->wire('modules')->getModuleConfigData($this); if (!isset($configData['url_wsdl']) || empty($configData['url_wsdl'])) { return false; } $url = $configData['url_wsdl']; try { $this->client = new SoapClient($url); } catch (SoapFault $e) { return $this->client = false; } $this->session_id = $this->wire('input')->cookie('SSID'); } It works well in my local XAMPP environment and does not show any error or exception when connection fails. However, on the server I keep getting ugly error displays at the bottom of the page (red when loggen in and generic "internal" when I am logged out from admin). At first I thought, that this had something to do with php error settings on the server, but afterwards I tried this bit code to see if it works outside of PW and it did! No error was thrown despite forcing PHP errors. <?php error_reporting(E_ALL); ini_set('display_errors', 1); ini_set('default_socket_timeout', 5); try { $client = new SoapClient('faulty url goes here'); } catch (SoapFault $e) { echo 'wrong'; } var_dump($client); So, I assumed that processwire has to do something with that. What exactly is the issue and how can I disable displaying these errors? try ... catch block is not enough and I already tried force setting $config->debug to 'false' Edited November 9, 2018 by simonsays remove image Link to comment Share on other sites More sharing options...
simonsays Posted November 9, 2018 Author Share Posted November 9, 2018 Also, forgot to mention, that the service works perfectly when SOAP service URL is correct and up. I just want to handle situations when it is not available for one reason or another. Link to comment Share on other sites More sharing options...
MoritzLost Posted November 9, 2018 Share Posted November 9, 2018 What namespace is your module file in? If you're using the Processwire namespace (or your own namespace, for that matter), the catch block can't catch the SoapFault because it's looking in the wrong namespace. If you module file uses the Processwire namespace, all type-hints (including those in catch-blocks) will be relative to that namespace. So your block catch (SoapFault $e) will only catch \Processwire\SoapFault exceptions, instead of \SoapFault (the SoapFault class in the global namespace, as provided by the SOAP PHP extension). That would explain why it's working in your test file which doesn't have a namespace, but not in your module. In this case, you can fix it by type-hinting the global object: catch (\SoapFault $e). 1 Link to comment Share on other sites More sharing options...
simonsays Posted November 12, 2018 Author Share Posted November 12, 2018 On 11/9/2018 at 5:38 PM, MoritzLost said: What namespace is your module file in? If you're using the Processwire namespace (or your own namespace, for that matter), the catch block can't catch the SoapFault because it's looking in the wrong namespace. If you module file uses the Processwire namespace, all type-hints (including those in catch-blocks) will be relative to that namespace. So your block catch (SoapFault $e) will only catch \Processwire\SoapFault exceptions, instead of \SoapFault (the SoapFault class in the global namespace, as provided by the SOAP PHP extension). That would explain why it's working in your test file which doesn't have a namespace, but not in your module. In this case, you can fix it by type-hinting the global object: catch (\SoapFault $e). I tried both ways and there is no real difference. I tried both } catch (SoapFault $e) { print 111;exit; return $this->client = false; } } catch (\SoapFault $e) { print 111;exit; return $this->client = false; } It reaches the "111" part but even with exit; the error is still thrown Link to comment Share on other sites More sharing options...
flydev Posted November 12, 2018 Share Posted November 12, 2018 By default, SOAP trigger error with his custom php error handler. To catch exceptions, add array('exceptions'=> true) as an argument : try { $client = new \SoapClient('faulty url goes here', array('exceptions'=> true)); } catch (\SoapFault $e) { echo 'wrong'; } Link to comment Share on other sites More sharing options...
simonsays Posted November 12, 2018 Author Share Posted November 12, 2018 3 hours ago, flydev said: By default, SOAP trigger error with his custom php error handler. To catch exceptions, add array('exceptions'=> true) as an argument : try { $client = new \SoapClient('faulty url goes here', array('exceptions'=> true)); } catch (\SoapFault $e) { echo 'wrong'; } Same result, both with and without Link to comment Share on other sites More sharing options...
flydev Posted November 12, 2018 Share Posted November 12, 2018 Whats the PHP version on XAMP and server? (Cant reproduce the error, working fine here) Link to comment Share on other sites More sharing options...
simonsays Posted November 13, 2018 Author Share Posted November 13, 2018 18 hours ago, flydev said: Whats the PHP version on XAMP and server? (Cant reproduce the error, working fine here) That's the main issue, unable to reproduce it on my local computer ? My local XAMPP is 7.2.8 and server runs on 7.0.27 However, I did not believe that the php version could be the issue. Link to comment Share on other sites More sharing options...
flydev Posted November 13, 2018 Share Posted November 13, 2018 Ok this is the issue. Your problem is a known bug in SOAP extension and you should update the version running on your server to at least PHP 7.1.14 (known to me where the bug was fixed and its working). So just to be clear, PHP 7.0 branch contain the bug. To fix it, update the server PHP version. And FYI, a good habit is to develop with the same environment to avoid those head scratching moment ? - https://bugs.php.net/bug.php?id=70469 Submitted: 2015-09-10 13:50 UTC Modified: 2017-11-22 22:14 UTC https://github.com/php/php-src/pull/2899 Quote - Soap: . Fixed bug #70469 (SoapClient generates E_ERROR even if exceptions=1 is used). (Anton Artamonov) 2 Link to comment Share on other sites More sharing options...
simonsays Posted November 13, 2018 Author Share Posted November 13, 2018 25 minutes ago, flydev said: Ok this is the issue. Your problem is a known bug in SOAP extension and you should update the version running on your server to at least PHP 7.1.14 (known to me where the bug was fixed and its working). So just to be clear, PHP 7.0 branch contain the bug. To fix it, update the server PHP version. And FYI, a good habit is to develop with the same environment to avoid those head scratching moment ? - https://bugs.php.net/bug.php?id=70469 Submitted: 2015-09-10 13:50 UTC Modified: 2017-11-22 22:14 UTC https://github.com/php/php-src/pull/2899 Looks like this could be the thing. Thank you very much! Will get back shortly. 1 Link to comment Share on other sites More sharing options...
simonsays Posted November 13, 2018 Author Share Posted November 13, 2018 (edited) 5 hours ago, flydev said: Ok this is the issue. Your problem is a known bug in SOAP extension and you should update the version running on your server to at least PHP 7.1.14 (known to me where the bug was fixed and its working). So just to be clear, PHP 7.0 branch contain the bug. To fix it, update the server PHP version. And FYI, a good habit is to develop with the same environment to avoid those head scratching moment ? - https://bugs.php.net/bug.php?id=70469 Submitted: 2015-09-10 13:50 UTC Modified: 2017-11-22 22:14 UTC https://github.com/php/php-src/pull/2899 @flydev Thank you very much for your help! Solved the issue. Would have struggled if it had not been for your help. Edited November 13, 2018 by simonsays tag user Link to comment Share on other sites More sharing options...
simonsays Posted November 13, 2018 Author Share Posted November 13, 2018 5 hours ago, flydev said: Ok this is the issue. Your problem is a known bug in SOAP extension and you should update the version running on your server to at least PHP 7.1.14 (known to me where the bug was fixed and its working). So just to be clear, PHP 7.0 branch contain the bug. To fix it, update the server PHP version. And FYI, a good habit is to develop with the same environment to avoid those head scratching moment ? - https://bugs.php.net/bug.php?id=70469 Submitted: 2015-09-10 13:50 UTC Modified: 2017-11-22 22:14 UTC https://github.com/php/php-src/pull/2899 I am still unsure why this fatal error was not displayed when using pure PHP (outside of module scope). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now