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

JSON Error : return readable error instead of RuntimeException

Open cvergne opened this issue 1 year ago • 1 comments

Hi,

I've noticed in the main Controller of the bundle, in case of JSON syntax error, a RuntimeException in thrown, which result to a 500 HTTP return code by Symfony with an "Internal Server Error" HTML response.

https://github.com/thecodingmachine/graphqlite-bundle/blob/9fc3c793541ae29a1b75665826759be9f0fdbca5/Controller/GraphQLiteController.php#L84-L89

In my point of view, both cases should return a JSON response formatted as GraphQL response with errors, with a 400 HTTP code like :

{
    "errors": [
        {
            "message": "Invalid JSON received in POST body",
            "extensions": {
                "reason": "Syntax error"
            }
        }
    ]
}

or

{
    "errors": [
        {
            "message": "Invalid JSON received in POST body",
            "extensions": {
                "reason": "Expecting associative array from request, got string"
            }
        }
    ]
}

The trick is that we are outside of the GraphQL Server context, so throwing a GraphQLException here doesn't have the desired effect. But the solution could be to create an GraphQLException Listener then create a JsonResponse from an ExecutionResult+Error from the GraphQLException.

What do you think ? Already tried it so I can work on it and submit a PR.

cvergne avatar Apr 17 '24 16:04 cvergne

I totaly agree with you, it's would be very more clear (and usefull to monitor the error log). But for error code I suggest to use 415 (Unsupported Media Type) if it's not parsable json, or 422 (Unprocessable Entity) if the json is not associative.

mistraloz avatar Apr 17 '24 16:04 mistraloz