Home » Php » php – error log truncated in laravel 5.3

php – error log truncated in laravel 5.3

Posted by: admin July 12, 2020 Leave a comment

Questions:

i have this entry in my laravel 5.3 log

2016-12-22 17:23:37] local.ERROR:
GuzzleHttp\Exception\ClientException: Client error: POST
https://api.sparkpost.com/api/v1/transmissions
resulted in a 400 Bad
Request
response: { “errors”: [ { “message”: “Message generation
rejected”, “description”: “recipient address suppressed due to
customer p (truncated…)

why does it truncate an important error message? now i cannot figure out what is going wrong…

How to&Answers:

The truncating is done by the Guzzle library. It only shows the first 120 characters of the response. I am assuming this is because responses could potentially be very long.

If you would like to see the full message, you should be able to customize how guzzle exceptions are handled.

Update the report() method in your app/Exceptions/Handler.php to something like:

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

Note: this is done in the report method, so it only affects what is written to the log. If the exception is dumped to the terminal or to the browser, it will still show the truncated message.

Answer:

Because your request throws a Guzzle Exception, as a workaround, instead of calling $e->getMessage(), You can simply try:

$e->getResponse()->getBody()->getContents();

If you don’t want to modify the report() method.

Worked nice for me.

Answer:

as alternative solution:

hotfix RequestException.php

ta_integration/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php

replace

$size = $body->getSize();
$summary = $body->read(120);
$body->rewind();

if ($size > 120) {

with for example:

    $size = $body->getSize();
    $summary = $body->read(999);
    $body->rewind();

    if ($size > 999) {

function

getResponseBodySummary

Answer:

None of these solutions here helped me. I found a solution here that helped. By the user sidk2020. Here is his solution in case the link breaks:

I did something very adventurous. I modified guzzel’s exception handler

guzzel on purpose only reads up 120 bytes of info and prints truncated next to it.

The file is located at : /vendor/guzzlehttp/guzzle/src/Exception/RequestException.php

So I modified that function and below is what my function looks like:

public static function getResponseBodySummary(ResponseInterface $response) { 
    $body = $response->getBody();

    if (!$body->isSeekable() || !$body->isReadable()) {
        return null;
    }

    $size = $body->getSize();

    if ($size === 0) {
        return null;
    }

    // Matches any printable character, including unicode characters:
    // letters, marks, numbers, punctuation, spacing, and separators.
    if (preg_match('/[^\pL\pM\pN\pP\pS\pZ\n\r\t]/', $body)) {
        return null;
    }

    return $body;

}