0

I am using Guzzle with the following code:

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. Hoe can I make it so that I can catch the error and status code as well to return to the user?

Mathias Hillmann
  • 1,537
  • 2
  • 13
  • 25
  • can you remove try-catch block and check inside logs what kind of exception was thrown? – V-K Feb 19 '21 at 12:50

2 Answers2

4

Having tried your code, it seems to be throwing an instance of GuzzleHttp\Exception\ConnectException, so change the catch to

} 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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Same result, as RequestException already has ConnectException in. https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions – Mathias Hillmann Feb 19 '21 at 12:49
  • @MathiasHillmann Interesting as the source at https://github.com/guzzle/guzzle/blob/master/src/Exception/ConnectException.php has `class ConnectException extends TransferException` – Nigel Ren Feb 19 '21 at 12:53
  • After messing with the code it seems you are correct and the docs are incorrect (?), ConnectException does catch it, however I need the status code of the request. – Mathias Hillmann Feb 19 '21 at 13:07
  • @MathiasHillmann, what status code? The connection failed and so there is not status. – Nigel Ren Feb 19 '21 at 13:11
  • if you only want to catch RequestException, then once in catch use `$e->hasResponse()` if it is false, then no connection thus no status code & no `getResponse()` if true then log the `getResponse()` and `getStatusCode()` – bhucho Feb 19 '21 at 13:14
  • @bhucho `Call to undefined method GuzzleHttp\\Exception\\ConnectException::hasResponse()` – Mathias Hillmann Feb 19 '21 at 13:17
  • @NigelRen Connection refused is one of the outcomes of this request, I need to return the status code since this is for a form that checks connection for an external api/token, and the outcomes can be statuses 403, 401, 200 or Connection Refused (404 more or less). – Mathias Hillmann Feb 19 '21 at 13:19
  • I managed to use your `ConnectException` and use `RequestException` by checking which instanceof the exception is related, and build a response using that check, thank you so much for your help. – Mathias Hillmann Feb 19 '21 at 13:26
  • you can send 502 bad gateway it is close but not as close to no connection, also see this answer for how to use hasResponse() https://stackoverflow.com/a/25040600/10958249 – bhucho Feb 19 '21 at 14:49
0

Since Guzzle has so many Exceptions, i ended up checking which type of exception that guzzle has thrown and constructing a response accordingly:

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();
}
Mathias Hillmann
  • 1,537
  • 2
  • 13
  • 25