我在laravel 5.3日志中有这个条目
2016-12-22 17:23:37] local.ERROR:
GuzzleHttp\Exception\ClientException: Client error:POST
resulted in a
https://api.sparkpost.com/api/v1/transmissions400 Bad
response: { “errors”: [ { “message”: “Message generation
Request
rejected”,“description”: “recipient address suppressed due to
customer p (truncated…)
为什么它会截断一条重要的错误信息?现在我无法弄清楚出了什么问题……
解决方法
截断是由Guzzle库完成的.它仅显示响应的前120个字符.我假设这是因为回复可能会很长.
如果您想查看完整的消息,您应该能够自定义如何处理guzzle异常.
将app / Exceptions / Handler.PHP中的report()方法更新为:
public function report(Exception $exception) { // this is from the parent method if ($this->shouldntReport($exception)) { return; } // this is from the parent method try { $logger = $this->container->make(\Psr\Log\LoggerInterface::class); } catch (Exception $ex) { throw $exception; // throw the original exception } // this is the new custom handling of guzzle exceptions if ($exception instanceof \GuzzleHttp\Exception\RequestException) { // get the full text of the exception (including stack trace),// and replace the original message (possibly truncated),// with the full text of the entire response body. $message = str_replace( rtrim($exception->getMessage()),(string) $exception->getResponse()->getBody(),(string) $exception ); // log your new custom guzzle error message return $logger->error($message); } // make sure to still log non-guzzle exceptions $logger->error($exception); }
注意:这是在report方法中完成的,因此它只会影响写入日志的内容.如果将异常转储到终端或浏览器,它仍将显示截断的消息.