thecodingmachine / graphqlite-bundle

A Symfony bundle for thecodingmachine/graphqlite.
35 stars 37 forks source link

JSON Error : return readable error instead of RuntimeException #207

Open cvergne opened 5 months ago

cvergne commented 5 months ago

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.

mistraloz commented 5 months ago

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.