from facebook_business.exceptions import FacebookRequestError from service.tasks.api_service_handler.error_handler.service_error_handler import ( ApiErrorHandler, ) from service.utils.appsync_communication.message_types import ServiceUserTokenError class FbErrorHandler(ApiErrorHandler): # more on fb-error codes: https://developers.facebook.com/docs/graph-api/using-graph-api/error-handling/ # structure: status -> code -> subcode(not always present and becomes 'None') params = { # error_statuses "400": { # error_codes "100": { # error_subcodes "33": { # Attempting to delete previously deleted/non existing object_id from FB "action": "ignore", "message": True, }, "1885325": { # Attempting to share audience with Recipient ad accounts that is not owned by business "action": "raise", "message": "Attempting to share audience with Recipient ad accounts that is not owned by business", }, "None": { # (#100) Param origin_audience_id must be a valid custom audience id # happens when you try to create lookalike audience from audince that was actually deleted from FB UI "action": "handle", "message": "Seems that this audience was deleted. Please create new audience.", "handler": ServiceUserTokenError, }, }, "2654": { "1713007": { # Attempting to create a duplicate Lookalike (same source, country and size) "action": "raise", "message": "Attempting to create a duplicate Lookalike (same source, country and size)", }, "1713008": { # Attempting to create Lookalike from source audience that includes less than 100 people from picked country "action": "raise", "message": "Source is too small. You are attempting to create Lookalike from source audience that includes less than 100 people in the same selected country", }, }, "2652": { "None": { # (#2652) Failed to share with an ad account that doesn't have a business "action": "raise", "message": "Cannot share with ad account that doesn't have a business.", } }, "190": { "463": { # Error validating access token: Session has expired on Friday, 18-Dec-20 08:50:03 PST. The current time is Saturday, 19-Dec-20 02:10:12 PST. "action": "handle", "message": "Seems that your Facebook account got disconnected. Please reconnect.", "handler": ServiceUserTokenError, }, "458": { # Error validating access token: The user has not authorized application 383084632861746. "action": "handle", "message": "Seems that your Facebook account got disconnected. Please reconnect.", "handler": ServiceUserTokenError, }, "460": { # Error validating access token. "action": "handle", "message": "Seems that your Facebook account got disconnected. Please reconnect.", "handler": ServiceUserTokenError, }, "467": { # Error validating access token. "action": "handle", "message": "Seems that your Facebook account got disconnected. Please reconnect.", "handler": ServiceUserTokenError, }, "464": { # Unconfirmed User. "action": "raise", "message": "Seems there is an issue with your Facebook account. Please log in at Facebook official website or at your mobile app to correct an issue.", }, "459": { # User Checkpointed. "action": "raise", "message": "Seems there is an issue with your Facebook account. Please log in at Facebook official website or at your mobile app to correct an issue.", }, }, "2656": { "None": { # Failed to delete custom audience because associated lookalikes exist "action": "raise", "message": "Can not delete custom audience because associated lookalikes exist", } }, }, "404": { "803": { "None": { # (#803) Some of the aliases you requested do not exist: 1 "action": "forward", "message": True, } } }, } def __init__(self, func): """Handling error responses. Allows to catch specific errors and format them for proper Front End handling""" super().__init__(func=func, error_obj=FacebookRequestError) def _digest_error(self, e: FacebookRequestError): status = str(e.http_status()) code = str(e.api_error_code()) subcode = str(e.api_error_subcode()) result = self._deep_get([status, code, subcode]) if result.get("action", None) == "ignore": return result["message"] elif result.get("action", None) == "forward": raise e elif result.get("action", None) == "raise": raise self.default_error(result.get("message", "")) elif result.get("action", None) == "handle": raise result["handler"](result.get("message", "")) raise e