Handle Guzzle Exception and Receive HTTP Tags

I would like to handle errors from Guzzle when the server returns 4xx and 5xx status codes. I am making a request as follows:

$client = $this->getGuzzleClient(); $request = $client->post($url, $headers, $value); try { $response = $request->send(); return $response->getBody(); } catch (\Exception $e) { // How can I get the response body? } 

$e->getMessage returns code information, but not the HTTP response body. How can I get the response body?

+98
php guzzle
Nov 02 '13 at 23:29
source share
4 answers

Guzzle 3.x

Per, you can catch the appropriate exception type ( ClientErrorResponseException for 4xx errors) and call its getResponse() method to get the response object, then call getBody() on this:

 use Guzzle\Http\Exception\ClientErrorResponseException; ... try { $response = $request->send(); } catch (ClientErrorResponseException $exception) { $responseBody = $exception->getResponse()->getBody(true); } 

Passing true to the getBody function means that you want to receive the response body as a string. Otherwise, you will receive it as an instance of the Guzzle\Http\EntityBody .

+62
Nov 02 '13 at 23:52
source

Guzzle 6.x

Per docs , the types of exceptions you may need are as follows:

  • GuzzleHttp\Exception\ClientException for 400-level errors
  • GuzzleHttp\Exception\ServerException for 500-level errors
  • GuzzleHttp\Exception\BadResponseException for both (this is their superclass)

The code for handling such errors now looks something like this:

 $client = new GuzzleHttp\Client; try { $client->get('http://google.com/nosuchpage'); } catch (GuzzleHttp\Exception\ClientException $e) { $response = $e->getResponse(); $responseBodyAsString = $response->getBody()->getContents(); } 
+197
Jun 20 '15 at 18:20
source

While the answers above are good, they will not deal with network errors, as Mark mentions BadResponseException - this is just a superclass for ClientException and ServerException. But RequestException is also a superclass of BadRequestException. This will lead not only to 400 and 500 errors, but also to network errors. So, let's say you request the page below, but your network plays up and your catch expects a BadResponseException. Well, your application will throw an error.

It is better in this case to expect a RequestException and check the response.

 try { $client->get('http://123123123.com') } catch (RequestException $e) { // If there are network errors, we need to ensure the application doesn't crash. // if $e->hasResponse is not null we can attempt to get the message // Otherwise, we'll just pass a network unavailable message. if ($e->hasResponse()) { $exception = (string) $e->getResponse()->getBody(); $exception = json_decode($exception); return new JsonResponse($exception, $e->getCode()); } else { return new JsonResponse($e->getMessage(), 503); } } 
+44
Nov 17 '16 at 0:06
source

Since 2019, here's what I've developed from the above answers and Guzzle documents to handle exceptions, get the response body, status code, message, and other, sometimes useful, response elements.

 try { /** * We use Guzzle to make an HTTP request somewhere in the * following theMethodMayThrowException(). */ $result = theMethodMayThorwException(); } catch (\Exception $e) { /** * Here we actually catch the instance of GuzzleHttp\Psr7\Response * (find it in ./vendor/guzzlehttp/psr7/src/Response.php) with all * its own and its 'Message' trait methods. See more explanations below. * * So you can have: HTTP status code, message, headers and body. * Just check the exception object has the response before. */ if ($e->hasResponse()) { $response = $e->getResponse(); var_dump($response->getStatusCode()); // HTTP status code var_dump($response->getReasonPhrase()); // Message var_dump((string) $response->getBody()); // Body var_dump($response->getHeaders()); // Headers array var_dump($response->hasHeader('Content-Type')); // Is the header presented? var_dump($response->getHeader('Content-Type')[0]); // Concrete header value } } // process $result etc. ... 

Voila. You get response information in conveniently divided paragraphs.

Side notes:

Using the catch clause, we catch the exception class of the root exception of the PHP chain \Exception , as custom exceptions in Guzzle extend it.

This approach can be useful in cases where Guzzle is used under the hood, as in the Laravel or AWS API PHP SDK, so you cannot catch a genuine Guzzle exception.

In this case, the exception class may not coincide with the class mentioned in the Guzzle documentation (for example, GuzzleHttp\Exception\RequestException as the root exception for Guzzle).

Therefore, you should catch \Exception , but remember that this is still an instance of the Guzzle exception class.

Use with caution though. These wrappers can make genuine Guzzle $e->getResponse() object methods inaccessible. In this case, you will have to take a look at the actual source code of the shell and figure out how to get the status, message, etc. Instead of using Guzzle $response methods.

If you call Guzzle yourself, you can catch GuzzleHttp\Exception\RequestException or any other mentioned in their exception documents regarding the terms of use.

+5
Jun 30 '19 at 7:24
source



All Articles