Skip to content

JSON Error : return readable error instead of RuntimeException #207

Closed
@cvergne

Description

@cvergne

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.

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Invalid JSON received in POST body: '.json_last_error_msg());
}
if (!is_array($parsedBody)){
throw new \RuntimeException('Expecting associative array from request, got ' . gettype($parsedBody));
}

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions