From 6ef1c2e27b6218de317c4a2604cb8e39091e3864 Mon Sep 17 00:00:00 2001 From: Benoit Galati Date: Mon, 10 Feb 2020 09:29:11 +0100 Subject: [PATCH] [HttpClient][MonologBridge] Add HttpClientProcessor to debug HttpClient errors --- .../Monolog/Processor/HttpClientProcessor.php | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Symfony/Bridge/Monolog/Processor/HttpClientProcessor.php diff --git a/src/Symfony/Bridge/Monolog/Processor/HttpClientProcessor.php b/src/Symfony/Bridge/Monolog/Processor/HttpClientProcessor.php new file mode 100644 index 0000000000000..94c5653cc9e74 --- /dev/null +++ b/src/Symfony/Bridge/Monolog/Processor/HttpClientProcessor.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Monolog\Processor; + +use Symfony\Component\VarDumper\VarDumper; +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; + +/** + * Add debugging info (Response headers, response content, etc.) about failing HttpClient requests. + * + * @author Benoit Galati + * + * @final + */ +class HttpClientProcessor +{ + public function __invoke(array $record): array + { + $exception = $record['context']['exception'] ?? null; + + if ($exception === null) { + return $record; + } + + while ($exception instanceof \Throwable) { + if ($exception instanceof HttpExceptionInterface) { + // It needs to be the 1st statement in order to fulfil the response info + $responseContent = $exception->getResponse()->getContent(false); + + $record['context']['http_client'][] = + $exception->getResponse()->getInfo() + + ['response_content' => mb_strimwidth($responseContent, 0, 10000)] + ; + } + $exception = $exception->getPrevious(); + } + + return $record; + } + +}