Skip to content

Why sys.excepthook for all (even "control flow" errors) ? #142

Closed
@elnygren

Description

@elnygren

Hi! I've got some testcases covering invalid mutations e.g mutating a non-existent object with an invalid UUIDv4. GraphQL and graphene handle these quite nicely (user gets a proper error message).

However, my stderr is still full of errors when I run my tests (that all pass). I did some digging with the debugger and found this line: https://github.com/graphql-python/graphql-core/blob/f8a6a8a27119f924c2fce863cca87c4219d8a12b/graphql/execution/base.py#L85

Why do we want to print everything to stderr?

manage.py test output

python manage.py test --settings=config.test_settings
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..Traceback (most recent call last):
  File "/Users/elnygren/Work/sumpli/sumpli-core/ENV/lib/python3.6/site-packages/graphql/execution/executor.py", line 215, in complete_value_catching_error
    exe_context, return_type, field_asts, info, result)
  File "/Users/elnygren/Work/sumpli/sumpli-core/ENV/lib/python3.6/site-packages/graphql/execution/executor.py", line 268, in complete_value
    raise GraphQLLocatedError(field_asts, original_error=result)
graphql.error.located_error.GraphQLLocatedError: No Business matches the given query.
.Traceback (most recent call last):
  File "/Users/elnygren/Work/sumpli/sumpli-core/ENV/lib/python3.6/site-packages/graphql/execution/executor.py", line 215, in complete_value_catching_error
    exe_context, return_type, field_asts, info, result)
  File "/Users/elnygren/Work/sumpli/sumpli-core/ENV/lib/python3.6/site-packages/graphql/execution/executor.py", line 268, in complete_value
    raise GraphQLLocatedError(field_asts, original_error=result)
graphql.error.located_error.GraphQLLocatedError: 'foo-bar-xyz-abc' is not a valid UUID.
.....................................

Note that:

No Business matches the given query.

and

'foo-bar-xyz-abc' is not a valid UUID.

are the messages shown to the users.

Code

class LikeABusiness(graphene.Mutation):
    """Mutation for liking/following a Business."""
    class Arguments:
        """Arguments of the Mutation"""
        uuid = graphene.String(required=True)

    # output
    ok = graphene.Boolean()

    def mutate(self, info, uuid, *args, **kwargs):
        """Perform the data mutation."""
        try:
            # raises the "No Business matches the given query." message due to 404
            return LikeABusiness(
                ok=get_object_or_404(Business, uuid=uuid).like(info.context.user)
            )
        except ValidationError as e:
            # passes along the "'foo-bar-xyz-abc' is not a valid UUID." message
            raise Exception(e.messages[0])

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