Added FBchatRedirectError

This commit is contained in:
Mads Marquart
2018-06-27 10:45:11 +02:00
parent 9b7a84ea45
commit 637b0ded09
3 changed files with 21 additions and 9 deletions

View File

@@ -96,13 +96,13 @@ class Client(object):
self.req_counter += 1 self.req_counter += 1
return payload return payload
def _fix_fb_errors(self, error_code): def _fix_fb_errors(self, error):
""" """
This fixes "Please try closing and re-opening your browser window" errors (1357004) This fixes "Please try closing and re-opening your browser window" errors (1357004)
This error usually happens after 1-2 days of inactivity This error usually happens after 1-2 days of inactivity
It may be a bad idea to do this in an exception handler, if you have a better method, please suggest it! It may be a bad idea to do this in an exception handler, if you have a better method, please suggest it!
""" """
if error_code == '1357004': if isinstanceof(error, FBchatFacebookError) and error.fb_error_code == '1357004':
log.warning('Got error #1357004. Doing a _postLogin, and resending request') log.warning('Got error #1357004. Doing a _postLogin, and resending request')
self._postLogin() self._postLogin()
return True return True
@@ -115,8 +115,8 @@ class Client(object):
return r return r
try: try:
return check_request(r, as_json=as_json) return check_request(r, as_json=as_json)
except FBchatFacebookError as e: except FBchatException as e:
if error_retries > 0 and self._fix_fb_errors(e.fb_error_code): if error_retries > 0 and self._fix_fb_errors(e):
return self._get(url, query=query, timeout=timeout, fix_request=fix_request, as_json=as_json, error_retries=error_retries-1) return self._get(url, query=query, timeout=timeout, fix_request=fix_request, as_json=as_json, error_retries=error_retries-1)
raise e raise e
@@ -127,8 +127,8 @@ class Client(object):
return r return r
try: try:
return check_request(r, as_json=as_json) return check_request(r, as_json=as_json)
except FBchatFacebookError as e: except FBchatException as e:
if error_retries > 0 and self._fix_fb_errors(e.fb_error_code): if error_retries > 0 and self._fix_fb_errors(e):
return self._post(url, query=query, timeout=timeout, fix_request=fix_request, as_json=as_json, error_retries=error_retries-1) return self._post(url, query=query, timeout=timeout, fix_request=fix_request, as_json=as_json, error_retries=error_retries-1)
raise e raise e
@@ -136,8 +136,8 @@ class Client(object):
content = self._post(self.req_url.GRAPHQL, payload, fix_request=True, as_json=False) content = self._post(self.req_url.GRAPHQL, payload, fix_request=True, as_json=False)
try: try:
return graphql_response_to_json(content) return graphql_response_to_json(content)
except FBchatFacebookError as e: except FBchatException as e:
if error_retries > 0 and self._fix_fb_errors(e.fb_error_code): if error_retries > 0 and self._fix_fb_errors(e):
return self._graphql(payload, error_retries=error_retries-1) return self._graphql(payload, error_retries=error_retries-1)
raise e raise e

View File

@@ -21,6 +21,15 @@ class FBchatFacebookError(FBchatException):
self.fb_error_message = fb_error_message self.fb_error_message = fb_error_message
self.request_status_code = request_status_code self.request_status_code = request_status_code
class FBchatRedirectError(Exception):
#: The url that Facebook redirected to
fb_redirect_url = None
def __init__(self, message, fb_redirect_url=None):
super(FBchatFacebookError, self).__init__(message)
"""Thrown by fbchat when Facebook returns an unexpected redirect"""
self.fb_redirect_url = str(fb_redirect_url)
class FBchatUserError(FBchatException): class FBchatUserError(FBchatException):
"""Thrown by fbchat when wrong values are entered""" """Thrown by fbchat when wrong values are entered"""

View File

@@ -187,6 +187,10 @@ def generateOfflineThreadingID():
return str(int(msgs, 2)) return str(int(msgs, 2))
def check_json(j): def check_json(j):
if j.get('jsmods') is not None and j['jsmods'].get('require') is not None:
for x in j['jsmods']['require']:
if len(x) > 3 and x[0] == 'ServerRedirect' and x[1] == 'redirectPageTo':
return FBchatRedirectError('Redicrect when sending request: {}'.format(j), fb_redirect_url=x[3][0])
if j.get('error') is None: if j.get('error') is None:
return return
if 'errorDescription' in j: if 'errorDescription' in j:
@@ -216,7 +220,6 @@ def check_request(r, as_json=True):
log.debug(j) log.debug(j)
return j return j
else: else:
log.debug(content)
return content return content
def get_jsmods_require(j, index): def get_jsmods_require(j, index):