【问题标题】:Guzzle Curl error not catche by try catch statement (Laravel)Guzzle Curl 错误未通过 try catch 语句捕获(Laravel)
【发布时间】:2015-11-22 00:22:30
【问题描述】:

在 Laravel 项目中,我需要调用 API REST 来删除远程数据。

我的问题是当我遇到错误时,我的 catch 语句没有捕获 Guzzle 异常。我的代码如下:

try {
    $client = new \GuzzleHttp\Client();
    $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
    $status = $request->getStatusCode();
} catch (Exception $e) {
    var_dump($e);exit();
}

Laravel 捕获了异常,但我的 catch 语句中没有。 Guzzle 抛出的异常是:

GuzzleHttp\Ring\Exception\ConnectException

在我的脚本第 3 行中提出,但没有在我的脚本中捕获。你能给我一种捕捉 Guzzle Exception 的方法吗?

我应该表明我已经看过这些帖子,但我没有得到很好的回应: How to resolve cURL Error (7): couldn't connect to host?Catching cURL errors from Guzzle

【问题讨论】:

    标签: php laravel curl laravel-4


    【解决方案1】:

    当我使用时它对我有用

    \GuzzleHttp\Exception\ConnectException

    而不是

    \Guzzle\Http\Exception\ConnectException

    【讨论】:

    • 就我而言,我只是错过了第一个反斜杠。谢谢。
    【解决方案2】:

    只是更新新 Guzzle 异常命名空间的答案

    try {
        $client = new \GuzzleHttp\Client();
        $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
        $status = $request->getStatusCode();
    } catch (\GuzzleHttp\Exception\ConnectException $e) {
        var_dump($e);exit();
    }
    

    【讨论】:

      【解决方案3】:

      您可能是想通过在 catch 语句或 use Exception 语句中添加反斜杠来捕获 \Exception(在根命名空间中)。

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题并使用以下内容解决了它。我使用了您的示例并给出了内联 cmets 供您理解。

        try {
            $client = new \GuzzleHttp\Client();
            $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
            $status = $request->getStatusCode();
            if($status == 200){
                $response = $response->json();
        
            }else{
                // The server responded with some error. You can throw back your exception
                // to the calling function or decide to handle it here
        
                throw new \Exception('Failed');
            }
        
        } catch (\Guzzle\Http\Exception\ConnectException $e) {
            //Catch the guzzle connection errors over here.These errors are something 
            // like the connection failed or some other network error
        
            $response = json_encode((string)$e->getResponse()->getBody());
        }
        

        希望这会有所帮助!

        【讨论】:

        • $response = json_encode((string)$e->getResponse()->getBody());对我不起作用,使用: $e->gethandlerContext()['error'] 获取错误消息部分:例如'Could not resolve host: xxxxx' 并使用 \GuzzleHttp\Exception\ConnectException,而不是 catch (\Guzzle\Http\Exception\ConnectException $e) {
        【解决方案5】:

        也许该异常没有扩展Exception 类。您可以尝试像这样抓住它:

        try {
            $client = new \GuzzleHttp\Client();
            $request = $client->delete(Config::get('REST_API').'/order-product/'.$id);
            $status = $request->getStatusCode();
        } catch (\GuzzleHttp\Ring\Exception\ConnectException $e) {
            var_dump($e);exit();
        } catch (Exception $e) {
            // ...
        }
        

        【讨论】:

        • 对不起,我已经尝试过了。我不知道为什么它不起作用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-21
        • 2014-03-04
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        • 2016-11-27
        • 2022-12-21
        相关资源
        最近更新 更多