Laravel中Guzzle http的错误日志被截断

17

Guzzle Http在异常信息超过120个字符时会截断,但我需要记录完整的异常信息。我该如何做?

我正在使用laravel 4.2.22。

3个回答

26
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)    
}

11

对于 Laravel 5 和 4,情况是一样的。

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

如果你直接访问$ex->getMessage(),最后会得到(truncated...)


1
请问,什么是“正确自动完成”,以及“上述方法可能失败”是如何发生的? - Yevgeniy Afanasyev
1
谢谢。你是对的,这段代码会创建意外的异常,让人沮丧。我已经修改了代码。 - Yevgeniy Afanasyev

3
可能有更好的解决方案:
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());
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接