Improve payload error handling
This commit is contained in:
@@ -28,5 +28,30 @@ class FBchatFacebookError(FBchatException):
|
|||||||
self.request_status_code = request_status_code
|
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):
|
class FBchatUserError(FBchatException):
|
||||||
"""Thrown by fbchat when wrong values are entered"""
|
"""Thrown by fbchat when wrong values are entered"""
|
||||||
|
@@ -11,7 +11,13 @@ from os.path import basename
|
|||||||
import warnings
|
import warnings
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
from ._exception import FBchatException, FBchatFacebookError
|
from ._exception import (
|
||||||
|
FBchatException,
|
||||||
|
FBchatFacebookError,
|
||||||
|
FBchatInvalidParameters,
|
||||||
|
FBchatNotLoggedIn,
|
||||||
|
FBchatPleaseRefresh,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from urllib.parse import urlencode, parse_qs, urlparse
|
from urllib.parse import urlencode, parse_qs, urlparse
|
||||||
@@ -118,17 +124,24 @@ def handle_error_in_payload(j):
|
|||||||
|
|
||||||
|
|
||||||
def handle_payload_error(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"]
|
# TODO: Use j["errorSummary"]
|
||||||
if "error" in j and "errorDescription" in j:
|
# "errorDescription" is in the users own language!
|
||||||
# "errorDescription" is in the users own language!
|
raise error_cls(
|
||||||
raise FBchatFacebookError(
|
"Error #{} when sending request: {}".format(error, j["errorDescription"]),
|
||||||
"Error #{} when sending request: {}".format(
|
fb_error_code=error,
|
||||||
j["error"], j["errorDescription"]
|
fb_error_message=j["errorDescription"],
|
||||||
),
|
)
|
||||||
fb_error_code=j["error"],
|
|
||||||
fb_error_message=j["errorDescription"],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def handle_graphql_error(j):
|
def handle_graphql_error(j):
|
||||||
|
Reference in New Issue
Block a user