简体   繁体   English

在连接被拒绝时捕获 Guzzle 异常

[英]Catch Guzzle exceptions on connections refused

I am using Guzzle with the following code:我正在使用带有以下代码的 Guzzle:

try {
    $client = new Client();
    $result = $client->get("http://{$request->ES_HOST}:{$request->ES_PORT}", [
            'headers' => ['Content-Type' => 'application/json'],
            'auth' => [$request->ES_LOGIN, $request->ES_PASSWORD],
            'allow_redirects' => false,
        ]);
    $response['status'] = $result->getStatusCode();
    $response['message'] = json_decode($result->getBody()->getContents());
} catch (RequestException $e) {
    $response['status'] = $e->getResponse()->getStatusCode();
    $response['message'] = $e->getMessage();
}

And it works well however when an user gives the wrong URL for guzzle to process it instead of getting catched in RequestException it gives an 500 error in the server and returns a regular Exception with the message of cURL error 7: Failed to connect to [host] port [port]: Connection refused .它运行良好,但是当用户为 guzzle 提供错误的 URL 来处理它而不是在RequestException中被捕获时,它会在服务器中给出 500 错误并返回带有 cURL 错误消息的常规Exception cURL error 7: Failed to connect to [host] port [port]: Connection refused Hoe can I make it so that I can catch the error and status code as well to return to the user?我可以做到这一点,以便我可以捕获错误和状态代码并返回给用户吗?

Having tried your code, it seems to be throwing an instance of GuzzleHttp\Exception\ConnectException , so change the catch to尝试过您的代码后,它似乎正在抛出GuzzleHttp\Exception\ConnectException的实例,因此将 catch 更改为

} catch (ConnectException $e) {
    $response['status'] = 'Connect Exception';
    $response['message'] = $e->getMessage();
}

( Adding the appropriate use statement... (添加适当的使用语句...

use GuzzleHttp\Exception\ConnectException;

) )

Also noticed that it doesn't have $e->getResponse()->getStatusCode() , which is why I've just set it to a fixed string.还注意到它没有$e->getResponse()->getStatusCode() ,这就是我刚刚将它设置为固定字符串的原因。

Since Guzzle has so many Exceptions, i ended up checking which type of exception that guzzle has thrown and constructing a response accordingly:由于 Guzzle 有这么多异常,我最终检查了 guzzle 抛出了哪种类型的异常并相应地构建了响应:

try {
    $client = new Client();
    $result = $client->get("http://{$request->ES_HOST}:{$request->ES_PORT}", [
            'headers' => ['Content-Type' => 'application/json'],
            'auth' => [$request->ES_LOGIN, $request->ES_PASSWORD],
            'allow_redirects' => false,
        ]);
    $response['status'] = $result->getStatusCode();
    $response['message'] = json_decode($result->getBody()->getContents());
} catch (ConnectException $e) {
    $response['status'] = 404;
    $response['message'] = $e->getMessage();
} catch (RequestException $e) {
    $response['status'] = $e->getResponse()->getStatusCode();
    $response['message'] = $e->getMessage();
} catch (\Exception $e) {
    $response['status'] = 0;
    $response['message'] = $e->getMessage();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM