I am in the early period of graphene-django
.
I have Mutation
like this
class DeleteObjection(graphene.Mutation):
"""
1. Authorized User only
2. His own `Objection` only
3. Be able to delete only `Hidden type Objection`
"""
ok = graphene.Boolean()
class Arguments:
id = graphene.ID()
@classmethod
def mutate(cls, root, info, **kwargs):
tmp = {
**kwargs,
'created_by': info.context.user,
'hidden': True
}
obj = Objection.objects.get(**tmp)
obj.delete()
return cls(ok=True)
And the res
is handle in like I expected. It is 200
with forwarded error right after the console
{
"errors": [
{
"message": "Objection matching query does not exist.",
"locations": [
{
"line": 131,
"column": 3
}
],
"path": [
"deleteObjection"
]
}
],
"data": {
"deleteObjection": null
}
}
Problem:
On my Python
server console. I see error has been raised
Testing started at 16:01 ...
/Users/sarit/.pyenv/versions/multy_herr/bin/python "/Users/sarit/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/193.5662.61/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_manage.py" test multy_herr.objections.tests_jwt.UsersTest.test_authorized_user_delete_non_exist_objection /Users/sarit/mein-codes/multy_herr
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Traceback (most recent call last):
File "/Users/sarit/.pyenv/versions/multy_herr/lib/python3.8/site-packages/promise/promise.py", line 489, in _resolve_from_executor
executor(resolve, reject)
File "/Users/sarit/.pyenv/versions/multy_herr/lib/python3.8/site-packages/promise/promise.py", line 756, in executor
return resolve(f(*args, **kwargs))
File "/Users/sarit/.pyenv/versions/multy_herr/lib/python3.8/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
return next(*args, **kwargs)
File "/Users/sarit/mein-codes/multy_herr/multy_herr/objections/grapheql/mutations.py", line 36, in mutate
obj = Objection.objects.get(**tmp)
File "/Users/sarit/.pyenv/versions/multy_herr/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/sarit/.pyenv/versions/multy_herr/lib/python3.8/site-packages/django/db/models/query.py", line 415, in get
raise self.model.DoesNotExist(
graphql.error.located_error.GraphQLLocatedError: Objection matching query does not exist.
Destroying test database for alias 'default'...
Process finished with exit code 0
Problem:
I hate error in the Python
console. Because it will alarm in the Crash Analytic and I considers it as a poor code
Attempts:
1. Add try - exception
and raise it again
@classmethod
def mutate(cls, root, info, **kwargs):
tmp = {
**kwargs,
'created_by': info.context.user,
'hidden': True
}
try:
obj = Objection.objects.get(**tmp)
except Exception as err:
raise Exception
else:
obj.delete()
return cls(ok=True)
I got unsatisfied result
res.data
Out[3]: OrderedDict([('deleteObjection', None)])
res.errors
Out[4]: [graphql.error.located_error.GraphQLLocatedError('')]
- Customized my own
class attribute
class DeleteObjection(graphene.Mutation):
"""
1. Authorized User only
2. His own `Objection` only
3. Be able to delete only `Hidden type Objection`
"""
ok = graphene.Boolean()
errors = graphene.List(graphene.String)
class Arguments:
id = graphene.ID()
@classmethod
def mutate(cls, root, info, **kwargs):
tmp = {
**kwargs,
'created_by': info.context.user,
'hidden': True
}
try:
obj = Objection.objects.get(**tmp)
except Exception as err:
return cls(ok=False, errors=[err])
else:
obj.delete()
return cls(ok=True)
errors
does not come to the response
res.data
Out[3]: OrderedDict([('deleteObjection', OrderedDict([('ok', False)]))])
res.errors
Question:
How do I suppress the error at my Python
console and follow the common Exception
in graphene-django
?