在正确创建新的SoapClient时捕获异常 [英] Catching an exception when creating a new SoapClient properly
问题描述
我遇到了一个难以获得的SoapClient身份验证问题。当我的代码执行时,Laravel声明它抛出一个ErrorException,但我似乎无法捕获它,无论我使用什么代码。我正在标记Laravel,以防在某些地方出现某些魔法,因为App :: error()会在此错误中触发。
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');
}
根据Laravel,一个ErrorException被抛出,但上面的代码没有捕获
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
推荐答案
在异常类型之前添加反斜杠例如异常
变成 \Exception
)。
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.
这篇关于在正确创建新的SoapClient时捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!