graphqlite-bundle icon indicating copy to clipboard operation
graphqlite-bundle copied to clipboard

Return GraphQL JSON response in case of JSON Input error instead of Internal Server Error

Open cvergne opened this issue 1 year ago • 1 comments

Fixes #207


The purpose of the PR is to return a standard GraphQL JSON error response in case of JSON error in given input (request body).

{
    "errors": [
        {
            "message": "Invalid JSON.",
            "extensions": {
                "reason": "Syntax error"
            }
        }
    ]
}

It adds an Exception Listener which set a JsonResponse in case of thrown GraphQLExceptionInterface (to catch any GraphQL Exception outside of the GraphQL Server context).

Also added the two exception cases of the controller into errors functional tests.

[!NOTE] Got an error when running test about DependencyInjection/Configuration.php not compatible with implemented interface, so fixed it but tell me if I should remove it and having it fixed in another PR.

cvergne avatar Apr 17 '24 17:04 cvergne

I will let @mistraloz review this as you discussed it.

About the implementation I'm okay with the current status. But I'm feeling that it may be too complex, this adds a lew listener, a new exception (which names conflict with a std one).

I think this is the right way to approach this as the end user will have enough control on it. But maybe a simple return new JsonResponse(..., 422) would have been sufficient.

Thank you for the tests too!

homersimpsons avatar May 05 '24 00:05 homersimpsons