代码之家  ›  专栏  ›  技术社区  ›  hamidfzm AlexLordThorsen

Python graphql异常处理

  •  0
  • hamidfzm AlexLordThorsen  · 技术社区  · 8 年前

    https://www.howtographql.com/graphql-python/6-error-handling/ 我用 raise GraphQLError

    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 - 
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Tobe E    8 年前

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


    如果仍要模拟该行为,则可以查看此StackOverflow答案以消除堆栈跟踪: You can turn off the traceback by limiting its depth . 它应该还会出现在 但是请注意,这仍然会在控制台上打印错误消息,但不会打印堆栈跟踪。

    如果你想完全消除控制台上的错误和堆栈跟踪(我不建议这样做),你需要在应用程序的某个地方捕获异常 外部 为了使错误仍然显示在 阵列。例如,您可以在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
    
    推荐文章