Add more explicit error handling
This commit is contained in:
@@ -115,24 +115,30 @@ class Client(object):
|
||||
def _get(self, url, query=None, error_retries=3):
|
||||
payload = self._generatePayload(query)
|
||||
r = self._state._session.get(prefix_url(url), params=payload)
|
||||
content = check_request(r)
|
||||
j = to_json(content)
|
||||
try:
|
||||
content = check_request(r)
|
||||
return to_json(content)
|
||||
handle_payload_error(j)
|
||||
except FBchatPleaseRefresh:
|
||||
if error_retries > 0:
|
||||
self._do_refresh()
|
||||
return self._get(url, query=query, error_retries=error_retries - 1)
|
||||
raise
|
||||
return j
|
||||
|
||||
def _post(self, url, query=None, files=None, as_graphql=False, error_retries=3):
|
||||
payload = self._generatePayload(query)
|
||||
r = self._state._session.post(prefix_url(url), data=payload, files=files)
|
||||
content = check_request(r)
|
||||
try:
|
||||
content = check_request(r)
|
||||
if as_graphql:
|
||||
return graphql_response_to_json(content)
|
||||
else:
|
||||
return to_json(content)
|
||||
j = to_json(content)
|
||||
# TODO: Remove this, and move it to _payload_post instead
|
||||
# We can't yet, since errors raised in here need to be caught below
|
||||
handle_payload_error(j)
|
||||
return j
|
||||
except FBchatPleaseRefresh:
|
||||
if error_retries > 0:
|
||||
self._do_refresh()
|
||||
@@ -147,6 +153,7 @@ class Client(object):
|
||||
|
||||
def _payload_post(self, url, data, files=None):
|
||||
j = self._post(url, data, files=files)
|
||||
handle_error_in_payload(j)
|
||||
try:
|
||||
return j["payload"]
|
||||
except (KeyError, TypeError):
|
||||
|
@@ -49,9 +49,9 @@ def graphql_response_to_json(content):
|
||||
if "error_results" in x:
|
||||
del rtn[-1]
|
||||
continue
|
||||
_util.check_json(x)
|
||||
_util.handle_payload_error(x)
|
||||
[(key, value)] = x.items()
|
||||
_util.check_json(value)
|
||||
_util.handle_graphql_error(value)
|
||||
if "response" in value:
|
||||
rtn[int(key[1:])] = value["response"]
|
||||
else:
|
||||
|
@@ -155,20 +155,6 @@ def handle_graphql_error(j):
|
||||
)
|
||||
|
||||
|
||||
def handle_generic_error(j):
|
||||
if j.get("error"):
|
||||
raise FBchatFacebookError(
|
||||
"Error {} when sending request".format(j["error"]), fb_error_code=j["error"]
|
||||
)
|
||||
|
||||
|
||||
def check_json(j):
|
||||
handle_error_in_payload(j)
|
||||
handle_payload_error(j)
|
||||
handle_graphql_error(j)
|
||||
handle_generic_error(j)
|
||||
|
||||
|
||||
def check_request(r):
|
||||
check_http_code(r.status_code)
|
||||
content = get_decoded_r(r)
|
||||
@@ -196,7 +182,6 @@ def check_content(content, as_json=True):
|
||||
def to_json(content):
|
||||
content = strip_json_cruft(content)
|
||||
j = parse_json(content)
|
||||
check_json(j)
|
||||
log.debug(j)
|
||||
return j
|
||||
|
||||
|
Reference in New Issue
Block a user