Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Symfony/Bridge/Monolog/Processor/HttpClientProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <benoit.galati@gmail.com>
*
* @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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must be wrapped in a try/catch(TransportExceptionInterface)


$record['context']['http_client'][] =
$exception->getResponse()->getInfo()
+ ['response_content' => mb_strimwidth($responseContent, 0, 10000)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mb_strimwidth is random to me (all mb_* without encoding arguments are, relying on global state).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it should be removed? What would be the alternative otherwise, specify the $encoding argument?

Copy link
Contributor Author

@B-Galati B-Galati Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it can be removed if the response size is checked before trying to get the content. WDYT?

;
}
$exception = $exception->getPrevious();
}

return $record;
}

}