Guzzle Curl 错误没有被 try catch 语句捕获(Laravel)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32252420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:16:12  来源:igfitidea点击:

Guzzle Curl error not catche by try catch statement (Laravel)

phplaravelcurllaravel-4

提问by Samuel Dauzon

In a Laravel project I need to call API REST to delete remote data.

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

My problem is that my catch statement dont catch Guzzle exceptionwhen I got an error. My code is the following :

我的问题是当我遇到错误时,我的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();
}

The exception is catched by Laravel but not in my catchstatement. The exception throwed by Guzzle is :

该异常被 Laravel 捕获,但不在我的catch语句中。Guzzle 抛出的异常是:

GuzzleHttp\Ring\Exception\ConnectException

It raised in my line 3of script and it doesn't catched in my script. Could you give me a way to catch the Guzzle Exception?

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

I should indicate that I already seen these posts but I not get a good response : How to resolve cURL Error (7): couldn't connect to host?and Catching cURL errors from Guzzle

我应该指出我已经看过这些帖子,但没有得到很好的回应: 如何解决 cURL 错误 (7):无法连接到主机?从狂饮捕获卷曲的错误

采纳答案by Ymartin

I had a similar problem and solved it using the following thing.I have used your example and given the inline comments for you to understand.

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

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());
}

Hope this helps!

希望这可以帮助!

回答by Muhammad

It worked with me when I used

当我使用时它对我有用

\GuzzleHttp\Exception\ConnectException

\GuzzleHttp\Exception\ConnectException

instead of

代替

\Guzzle\Http\Exception\ConnectException

\Guzzle\Http\Exception\ConnectException

回答by Mihai Matei

Maybe that exception is not extending the Exceptionclass. You may try to catch it like:

也许那个例外没有扩展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) {
    // ...
}

回答by Peter

You probably mean to catch \Exception(in the root namespace), either by adding the backslash in the catch statement or a use Exceptionstatement.

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

回答by Eslam Salem Mahmoud

Just Updating the answer to the new Guzzle exception namespace

只是更新新 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();
}