我有一个连接到第三方API的脚本。它应该是24/7不间断循环运行(我在重新启动循环之前使用了一个休眠)。
问题在于,有时候第三方API会被处理掉,或者连接只是因为这个错误而丢失:
致命错误:未捕获的异常 'guzzleHttp \ Ring \ Exception \ ConnectException',消息'cURL错误 7:无法连接到xxx.com端口443
有没有办法在这个致命错误上“中断”以确保代码重新启动并在可以执行操作时继续执行,或者每次出现此错误时都必须手动重启?
答案 0 :(得分:6)
像这样:看起来你可以抓住GuzzleHttp \ Ring \ Exception \ ConnectException异常
use GuzzleHttp\Ring\Exception\ConnectException;
try {
// the code which throws the error
} catch( ConnectException $ex ) {
switch ( $ex->getMessage() ) {
case '7': // to be verified
// handle your exception in the way you want,
// maybe with a graceful fallback
break;
}
}
它似乎是ConnectException extends some classes,最终扩展了php Exception,因此您可以安全地使用getCode ()方法,允许您捕获一个标识符,您可以根据需要做出相应的反应。