Reputation: 1017
I am using Guzzle inside the laravel framework but when the host is offline I get a exception instead of a statuscode which you can get with getStatusCode()
. I have now made a try catch around the request, but outside this method I have a method which checks on the statuscode. My question is how can I return in the catch the correct response so I could call outside this method getStatusCode()
.
My code to make the request looks like this:
public function makeRequest($method, $requestUrl, $queryParams = [])
{
try{
$client = new Client(['http_errors' => false]);
return $client->request($method, $requestUrl, [
'query' => $queryParams
]);
}catch(RequestException $exception){
LOG::info($exception->getMessage());
return $exception->getResponse();
}
}
Upvotes: 0
Views: 282
Reputation: 5010
You idea (always return Response
object from the method) doesn't work by design. I mean that you see it already, there is no Response
object is some cases (when a connection to the host cannot be established, for example, so HTTP flow doesn't even start in this case, that why you don't have any HTTP status code).
IMO the best way to make your code aware of the exception. Don't try to handle it inside your makeRequest()
, just let it flow further, to the point where you actually can handle it.
Upvotes: 1