16

Guzzle http is truncating exceptions with more than 120 characters, but I need to log the full exception message. How can I do this?

I am using laravel 4.2.22.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
Parvez Alam
  • 358
  • 1
  • 3
  • 13

3 Answers3

24
try {
    // whatever
} catch (\GuzzleHttp\Exception\RequestException $ex) {
     return $ex->getResponse()->getBody()->getContents(); 
     // you can even json_decode the response like json_decode($ex->getResponse()->getBody()->getContents(), true)    
}
Yamen Ashraf
  • 2,637
  • 2
  • 20
  • 26
10

It is the same for Laravel 5 and 4

    try {
        $response = $client->post($path, $params);
    } catch (\GuzzleHttp\Exception\RequestException $ex) {
        \Log::debug('error');
        \Log::debug((string) $ex->getResponse()->getBody());
        throw $ex;
    }

if you just go to $ex->getMessage(), you will get (truncated...) at the end.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
2

Might be better solution:

try {
    // do request here like:
    // return $client->post($path, $params);
} catch (\GuzzleHttp\Exception\ServerException $ex) {
    $exFactoryWithFullBody = new class('', $ex->getRequest()) extends \GuzzleHttp\Exception\RequestException {
        public static function getResponseBodySummary(ResponseInterface $response)
        {
            return $response->getBody()->getContents();
        }
    };

    throw $exFactoryWithFullBody->create($ex->getRequest(), $ex->getResponse());
}
mvorisek
  • 3,290
  • 2
  • 18
  • 53