pythonexceptiontry-catchfacebook-marketing-apifacebook-business-sdk

How to catch *specific* exceptions in Facebook Business Python SDK?


a very simple question, but nonetheless there doesn't appear to be any info on it.

I want to wrap a call to Facebook's Marketing API in a try-except statement. As is good practice (I hear), I want to catch specific exceptions, rather than everything. I know that the main exception I'm dealing with is the FacebookRequestError. However, I don't quite understand how i can try-except for this.

If I do...

try:
    test = Ad(ad).get_insights()
except FacebookRequestError as e:
    print('Limit exceeded, wait!')
    time.sleep(600)

The error-catching doesn't work, because obviously the object 'FacebookRequestError' isn't assigned to anything. I've imported the following at the top of my script:

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount # account-level info
from facebook_business.adobjects.campaign import Campaign # campaign-level info
from facebook_business.adobjects.adset import AdSet # ad-set level info
from facebook_business.adobjects.ad import Ad # ad-level info

So, will I need to import the exceptions part specifically in order to catch for them by subsetting a given module I imported for the exceptions attribute? I tried this, but can't find them anywhere.

Any help would be greatly appreciated, thanks!


Solution

  • The readme in the GitHub repository for that package (which acts as the documentation for the package as far as I can tell) has a very brief section on exceptions and refers to the facebook_business.exceptions module, so that'd be a good place to start.