我正处于graphene-django
的早期。
我有这样的Mutation
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)
res
的处理方式与我预期的一样。控制台后紧接200
并转发错误
{
"errors": [
{
"message": "Objection matching query does not exist.",
"locations": [
{
"line": 131,
"column": 3
}
],
"path": [
"deleteObjection"
]
}
],
"data": {
"deleteObjection": null
}
}
问题:
在我的Python
服务器控制台上。我看到错误出现了
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
问题:
我讨厌Python
控制台中的错误。因为它会在Crash Analytic(崩溃分析)中发出警报,因此我认为它是错误的代码
尝试:
1.添加try - exception
并再次升高
@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)
我不满意结果
res.data
Out[3]: OrderedDict([('deleteObjection', None)])
res.errors
Out[4]: [graphql.error.located_error.GraphQLLocatedError('')]
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
没有得到答复
res.data
Out[3]: OrderedDict([('deleteObjection', OrderedDict([('ok', False)]))])
res.errors
问题:
如何在Python
控制台上抑制错误并遵循Exception
中常见的graphene-django
?
答案 0 :(得分:0)
这是我如何处理Graphene-Django中的错误。
class CreateBlog(graphene.Mutation):
class Arguments:
input = BlogInputType(required=True)
ok = graphene.Boolean()
blog = graphene.Field(BlogType)
errors = graphene.Field(BlogErrorsInputType)
@staticmethod
def mutate(root, info, input):
form = CreateBlogForm(data=input)
if form.is_valid():
blog = form.save()
return CreateBlog(ok=True, blog=blog, errors=form.errors)
return CreateBlog(ok=False, errors=form.errors)
从here中查看详细信息。