Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

EfficcientUuid and SQL errors: Malformed UTF-8 characters #52

Open
ppalmeida opened this issue Jan 22, 2022 · 1 comment
Open

EfficcientUuid and SQL errors: Malformed UTF-8 characters #52

ppalmeida opened this issue Jan 22, 2022 · 1 comment

Comments

@ppalmeida
Copy link

Hey.
Thank you for your package. Really nice.

I faced a problem that was getting me crazy for a few hours because the error I was getting was somewhat unexpected. And I believe it was caused by efficient-uuid.

My "Organization" table/model has a field called "slug". For some other reason, my trait that should automatically generate the slug value was not working. Anyway, when I was saving my model, I was just doing this:

public function store(CreateOrganizationRequest $request)
    {
        $params = $request->all();
        $params['owner_id'] = $request->user()->id;

        $organization = Organization::create($params);

        return response()->json($organization, 201, [], JSON_UNESCAPED_UNICODE);
    }

So, this should fail because "slug" is a required field and it's not being informed anywhere. Something like "slug is a required column and there is no default value" or something like that from the SQL or Laravel. But the error I got was:

{
    "message": "Malformed UTF-8 characters, possibly incorrectly encoded",
    "exception": "InvalidArgumentException",
    "file": "/path-to/project/backend-laravel/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php",
    "line": 88,
    "trace": [
        {
            "file": "/path-to/project/backend-laravel/vendor/symfony/http-foundation/JsonResponse.php",
            "line": 54,
            "function": "setData",
            "class": "Illuminate\\Http\\JsonResponse",
            "type": "->"
        },
        {
            "file": "/path-to/project/backend-laravel/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php",
            "line": 32,
            "function": "__construct",
            "class": "Symfony\\Component\\HttpFoundation\\JsonResponse",
            "type": "->"
        },

So, the problem here is: when the model fails to be saved, I believe that Laravel is trying to convert the binary error with json_encode() and. because of the binary column, another exception is thrown, regarding the bad encoding. Is that anything we could do here? If not, could we update the README file so other users would be aware that this "Malformed UTF-8 characters, possibly incorrectly encoded" may be some missing required fields when saving a model to DB?

I'm also not very sure if this issue should be here or in laravel-model repo.

Cheers.

@schmoove
Copy link

I've battled with this issue also. My best solution was to wrap the Eloquent action in a try/catch and decode the error correctly within the catch before outputting it to the JsonResponse.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants