【问题标题】:Python graphql exception handling: Is it expected to get errors array with 200 OK response?Python graphql 异常处理:是否期望得到具有 200 OK 响应的错误数组?
【发布时间】:2019-03-21 13:03:59
【问题描述】:

根据https://www.howtographql.com/graphql-python/6-error-handling/ 中的文档,我使用raise GraphQLError 在我的 Flask GraphQL 应用程序变异函数中显示错误,如下所示:

import graphene
from graphql import GraphQLError

from ...extensions import db
from ...models import User as UserModel
from ..types import User as UserType

class Update(graphene.Mutation):
    class Input:
        id = graphene.ID(required=True)
        # phone = graphene.String()
        name = graphene.String(required=False, default_value=None)
        # active = graphene.Boolean()

    Output = UserType

    @staticmethod
    def mutate(root, info, **kwargs):
        user = graphene.Node.get_node_from_global_id(info, kwargs.pop('id'))
        # print(info.context)
        # if not user:
        raise GraphQLError('eeee')
        # user.update(**kwargs)
        # db.session.commit()

        return user

我期望得到类似 400 状态码的东西,带有 graphql 错误 json 模式。但我得到 200 并且异常打印在带有回溯的控制台中。我在这里做错了吗?

An error occurred while resolving field Mutation.updateUser
Traceback (most recent call last):
  File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executor.py", line 447, in resolve_or_error
    return executor.execute(resolve_fn, source, info, **args)
  File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executors/sync.py", line 16, in execute
    return fn(*args, **kwargs)
  File "/application/schema/mutation/user.py", line 40, in mutate
    raise GraphQLError('eeee')
graphql.error.base.GraphQLError: eeee
Traceback (most recent call last):
  File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executor.py", line 447, in resolve_or_error
    return executor.execute(resolve_fn, source, info, **args)
  File "/.local/share/virtualenvs/Server-CvYlbWSB/lib/python3.7/site-packages/graphql/execution/executors/sync.py", line 16, in execute
    return fn(*args, **kwargs)
  File "/application/schema/mutation/user.py", line 40, in mutate
    raise GraphQLError('eeee')
graphql.error.located_error.GraphQLLocatedError: eeee

127.0.0.1 - - [17/Oct/2018 01:46:54] "POST /graphql? HTTP/1.1" 200 - 

【问题讨论】:

    标签: graphene-python flask-graphql graphql-python graphene-sqlalchemy


    【解决方案1】:

    显示堆栈跟踪似乎是有意的。您可以查看discussion on GitHub。以防万一链接失效,讨论的基础是graphql-core 库基本上会吃掉石墨烯抛出的所有错误并将它们放在results.errors 数组中,而不会将堆栈跟踪打印到sys.stderr。通常,这是不受欢迎的行为,因此它似乎在拉取请求中被更改。


    如果您仍想模仿该行为,可以查看此 StackOverflow 答案以摆脱堆栈跟踪:You can turn off the traceback by limiting its depth。它应该仍然以这种方式显示在results.errors;但是请注意,这仍然会在控制台上打印错误消息,但不会打印堆栈跟踪。

    如果您想完全摆脱控制台上的错误和堆栈跟踪(我不推荐这样做),您需要在应用程序中的某处捕获异常 > 突变解析器,以便错误仍然显示在results.errors 数组中。例如,您可以在 Flask 应用程序最初运行时执行此操作(尽管在这种情况下范围可能太大)。

    try:
        app = Flask(__name__)
    except GraphQLError as gqle:
        pass # ignore the error
    except OtherErrorYouManuallyCall as oeymc:
        pass 
    # Any other error will be thrown and show the stack trace
    

    【讨论】:

    • graphql 似乎没有区分预期的客户端错误和意外的服务器错误。我还注意到 Sentry 发现了平庸的 graphql 错误,即使是用户未通过身份验证之类的错误,并将它们报告为崩溃。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2020-10-09
    • 2018-11-05
    • 2021-06-12
    • 2020-02-09
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多