From bc551a63c2249477f11545f75260bca6b0198c67 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 2 Jul 2019 17:50:33 +0200 Subject: [PATCH] Improve GraphQL error handling --- fbchat/_client.py | 1 + fbchat/_graphql.py | 2 +- fbchat/_util.py | 19 +++++++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 9b04913..2aae8d4 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -1735,6 +1735,7 @@ class Client(object): } data = {"doc_id": 1491398900900362, "variables": json.dumps({"data": data})} j = self._payload_post("/webgraphql/mutation", data) + handle_graphql_errors(j) def createPlan(self, plan, thread_id=None): """ diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index c03577c..50eaea4 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -51,7 +51,7 @@ def graphql_response_to_json(content): continue _util.handle_payload_error(x) [(key, value)] = x.items() - _util.handle_graphql_error(value) + _util.handle_graphql_errors(value) if "response" in value: rtn[int(key[1:])] = value["response"] else: diff --git a/fbchat/_util.py b/fbchat/_util.py index a6ab6f7..56ddf14 100644 --- a/fbchat/_util.py +++ b/fbchat/_util.py @@ -144,14 +144,21 @@ def handle_payload_error(j): ) -def handle_graphql_error(j): - if j.get("error") and "debug_info" in j["error"] and "code" in j["error"]: +def handle_graphql_errors(j): + errors = [] + if "error" in j: + errors = [j["error"]] + if "errors" in j: + errors = j["errors"] + if errors: + error = errors[0] # TODO: Handle multiple errors + # TODO: Use `summary`, `severity` and `description` raise FBchatFacebookError( - "Error #{} when sending request: {!r}".format( - j["error"]["code"], j["error"]["debug_info"] + "GraphQL error #{}: {} / {!r}".format( + error.get("code"), error.get("message"), error.get("debug_info") ), - fb_error_code=j["error"]["code"], - fb_error_message=j["error"]["debug_info"], + fb_error_code=error.get("code"), + fb_error_message=error.get("message"), )