Catching an exception when creating a new SoapClient properly

不打扰是莪最后的温柔 提交于 2019-12-11 19:42:25

问题


I'm having a difficult time catching a SoapClient authentication issue. When my code executes, Laravel declares it's throwing an ErrorException but I can't seem to catch it no matter what code I use. I'm tagging Laravel in case there's some magic going on somewhere I don't know about because App::error() will trigger on this error still.

try {
    $client = new SoapClient(
        $this->serviceUrl . $this->clients[$clientName],
        array(
            'login'      => $this->username,
            'password'   => $this->password,
            'exceptions' => true,
        )
    );
} catch (SoapFault $e) {
    die('soapfault never fires!');
} catch (Exception $e) {
    die('exception won\t t');
} catch (ErrorException $e) {
    die('error exception also doesn\'t error');
}

According to Laravel an ErrorException is being thrown but the above code doesn't catch it.

ErrorException

SoapClient::SoapClient(https://control.akamai.com/nmrws/services/RealtimeReports?wsdl) [<a href='soapclient.soapclient'>soapclient.soapclient</a>]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

回答1:


Add backslash before exception types (e.g. Exception becomes \Exception).

They belong to global namespace. Your code tries to catch exceptions in currently used namespace which doesn't have to be the same as global.



来源:https://stackoverflow.com/questions/19301984/catching-an-exception-when-creating-a-new-soapclient-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!