-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpClient][MonologBridge] Add HttpClientProcessor to debug HttpClient errors #35659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
|
||
$record['context']['http_client'][] = | ||
$exception->getResponse()->getInfo() | ||
+ ['response_content' => mb_strimwidth($responseContent, 0, 10000)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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)