tomcodes's avatar

Sporadic Unable to JSON encode payload. Error code: 5 errors

Hello,

Since a few weeks I have seen repeated but very sporadic Unable to JSON encode payload. Error code: 5 errors in my logs. This happens when I add a notification to Laravel Queue.

I understand that this happens when the models passed to the notification can not be properly JSON enoded, and that it might be linked to bad character encoding.

However whenever I try to say trigger the notifiction from Artisan Tinker, I don't have any problems (yes, with the exact same data).

I have no idea how I can debug / log things to identify the source of this issue. Any input would be greatly appreciated!

Current setup:

  • Laravel v9.25.1
  • Horizon v5.10.0
0 likes
7 replies
Sinnbeck's avatar

Can you show the code perhaps ? It might be a good idea to send only the data you need, and not a complete model (Which is very rarely needed)

tomcodes's avatar

Yes sorry about the code but there's nothing really particular about it:

Here is my trigger:

$user->notify((new SendAppointmentReminder($event, true, false))->delay(now()->addMinutes($delayInMinutes)));

I find it useful to pass the full $event model because then I can use the relationships of this model inside the notification itself.

Namvari's avatar

@tomcodes you found a way to do it?

I just created 2 job, one without ShouldQueue.

When i encounter InvalidPayloadException trying to process job...

tomcodes's avatar

@Namvari I had a pull request merged into laravel/framework that removes this kind of characters in the sides, in tests/Http/Middleware/TrimStringsTest.php directly:

https://github.com/laravel/framework/pull/44906

However this was not enough so I added my own middleware in my project:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Http\Middleware\TransformsRequest;
use Illuminate\Http\Request;

class RemoveZeroWidthSpaces extends TransformsRequest
{
    /**
     * The names of the attributes that should not be trimmed.
     *
     * @var array
     */
    protected $except = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Transform the given value.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return mixed
     */
    protected function transform($key, $value)
    {
        if (in_array($key, $this->except, true) || ! is_string($value)) {
            return $value;
        }

        // Zero-width non-breakabke space
        // See: https://en.wikipedia.org/wiki/Word_joiner
        $value = str_replace("\xEF\xBB\xBF", "", $value);

        // Zero-width space
        // See: https://en.wikipedia.org/wiki/Zero-width_space
        $value = str_replace("\xE2\x80\x8B", "", $value);

        return $value;
    }
}

Don't forget to reference it in your app/Http/Kernel.php ;)

Please note that you have to then clean your data from your database. This middleware only works for records created after you implement it.

Namvari's avatar

@tomcodes Thanks, i see that.

But my problem is i'm processing EXCEL file. When processing .XLSX file, it's ok, but if i convert it to .XLS (Excel 97-2003 Workbook) file, error happen.

I'm not sure why... because i can't find one of ["\xEF\xBB\xBF" , "\xE2\x80\x8B" ] in file with opening in editors...

Please or to participate in this conversation.