How can I test output of graphene when error is thrown?
See original GitHub issueResolver:
def resolve_entity(self, info, **kwargs):
id = kwargs.get('id')
if id is None:
raise GraphQLError('ID not provided')
try:
return Entity.objects.get(pk=id)
except Entity.DoesNotExist:
raise GraphQLError('Entity not found')
Entity:
def test_single_athlete_no_id(self):
athlete = Entity.objects.create(name='John Doe', weight=23.45,
height=180)
try:
self.client.execute(
'{ entity { id, name, weight, height } }')
except:
print('test')
I have been trying different ways (assertRaises, using try/except) but nothing is raised when there is definitely a stack trace like the following:
…An error occurred while resolving field Query.entity Traceback: … raise GraphQLError(‘ID not provided’) graphql.error.base.GraphQLError: ID not provided Traceback: … graphql.error.located_error.GraphQLLocatedError: ID not provided
Why am I not able to catch these exceptions? Am I not supposed to use exceptions for error handling?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:10 (5 by maintainers)
Top Results From Across the Web
How can exceptions in graphene-django be conditionally ...
Try this. First ensure that intended exceptions are GraphQLError or descendants of it. Then create a log filter like so:
Read more >Full Stack Error Handling with GraphQL and Apollo
graphQLErrors : Any error that is thrown within your resolvers. Since we have multiple resolvers executing that are all potential sources of ......
Read more >Error Handling - graphql-python
Graphene Errors On the application level, you can use the GraphQLError class or the good and old Python exceptions. You already used...
Read more >ObjectType - Graphene-Python
Each ObjectType is a Python class that inherits from graphene.ObjectType . Each attribute of the ObjectType represents a Field ... An error will...
Read more >Graphene Documentation - Read the Docs
5 Testing in Graphene ... Compare Graphene's code-first approach to building a GraphQL API with schema-first ... An error will be thrown:.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dvndrsn My issue is that I am trying to test the error cases. And I want my error cases to catch the error and “pass” the test. Instead they are throwing errors. I think it would be logical to use logger for errors instead of printing them to the console.
@iraj-jelo - that’s an interesting point, but maybe a separate discussion. One of the neat things about the GraphQL spec is that it allows for a partial response to a query where only the successfully resolved fields are returned!
There are a couple of things about exception handling in graphene that can be surprising (like how exceptions are automatically caught for resolvers as I mentioned above). I do wonder though why the server might encounter a missing required argument when submitting a query from your frontend - it may make sense for the frontend to ensure that the parameter is always passed through before sending the request.
Feel free to submit an issue with more details on this to chat through the concern in more depth.