Handle Guzzle exception and get HTTP body

前端 未结 6 679
长发绾君心
长发绾君心 2020-12-07 08:32

I would like to handle errors from Guzzle when the server returns 4xx and 5xx status codes. I make a request like this:

$client = $this->getGuzzleClient()         


        
6条回答
  •  既然无缘
    2020-12-07 08:40

    As of 2019 here is what I elaborated from the answers above and Guzzle docs to handle the exception, get the response body, status code, message and the other sometimes valuable response items.

    try {
        /**
         * We use Guzzle to make an HTTP request somewhere in the
         * following theMethodMayThrowException().
         */
        $result = theMethodMayThrowException();
    } catch (\GuzzleHttp\Exception\RequestException $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's 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()); // Response message;
            var_dump((string) $response->getBody()); // Body, normally it is JSON;
            var_dump(json_decode((string) $response->getBody())); // Body as the decoded JSON;
            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 the response's information in conveniently separated items.

    Side Notes:

    With catch clause we catch the inheritance chain PHP root exception class \Exception as Guzzle custom exceptions extend it.

    This approach may be useful for use cases where Guzzle is used under the hood like in Laravel or AWS API PHP SDK so you cannot catch the genuine Guzzle exception.

    In this case, the exception class may not be the one mentioned in the Guzzle docs (e.g. GuzzleHttp\Exception\RequestException as the root exception for Guzzle).

    So you have to catch \Exception instead but bear in mind it is still the Guzzle exception class instance.

    Though use with care. Those wrappers may make Guzzle $e->getResponse() object's genuine methods not available. In this case, you will have to look at the wrapper's actual exception source code and find out how to get status, message, etc. instead of using Guzzle $response's methods.

    If you call Guzzle directly yourself you can catch GuzzleHttp\Exception\RequestException or any other one mentioned in their exceptions docs with respect to your use case conditions.

提交回复
热议问题