Improve payload error handling

This commit is contained in:
Mads Marquart
2019-07-02 17:11:11 +02:00
parent 7345de149a
commit 794696d327
2 changed files with 49 additions and 11 deletions

View File

@@ -28,5 +28,30 @@ class FBchatFacebookError(FBchatException):
self.request_status_code = request_status_code
class FBchatInvalidParameters(FBchatFacebookError):
"""Raised by Facebook if:
- Some function supplied invalid parameters.
- Some content is not found.
- Some content is no longer available.
"""
class FBchatNotLoggedIn(FBchatFacebookError):
"""Raised by Facebook if the client has been logged out."""
fb_error_code = "1357001"
class FBchatPleaseRefresh(FBchatFacebookError):
"""Raised by Facebook if the client has been inactive for too long.
This error usually happens after 1-2 days of inactivity.
"""
fb_error_code = "1357004"
fb_error_message = "Please try closing and re-opening your browser window."
class FBchatUserError(FBchatException):
"""Thrown by fbchat when wrong values are entered"""

View File

@@ -11,7 +11,13 @@ from os.path import basename
import warnings
import logging
import requests
from ._exception import FBchatException, FBchatFacebookError
from ._exception import (
FBchatException,
FBchatFacebookError,
FBchatInvalidParameters,
FBchatNotLoggedIn,
FBchatPleaseRefresh,
)
try:
from urllib.parse import urlencode, parse_qs, urlparse
@@ -118,15 +124,22 @@ def handle_error_in_payload(j):
def handle_payload_error(j):
# TODO: Check known error codes, and raise more relevant errors!
if "error" not in j:
return
error = j["error"]
if j["error"] == 1357001:
error_cls = FBchatNotLoggedIn
elif j["error"] == 1357004:
error_cls = FBchatPleaseRefresh
elif j["error"] in (1357031, 1545010, 1545003):
error_cls = FBchatInvalidParameters
else:
error_cls = FBchatFacebookError
# TODO: Use j["errorSummary"]
if "error" in j and "errorDescription" in j:
# "errorDescription" is in the users own language!
raise FBchatFacebookError(
"Error #{} when sending request: {}".format(
j["error"], j["errorDescription"]
),
fb_error_code=j["error"],
raise error_cls(
"Error #{} when sending request: {}".format(error, j["errorDescription"]),
fb_error_code=error,
fb_error_message=j["errorDescription"],
)