1

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!

nikUoM
  • 639
  • 1
  • 8
  • 18
  • Are you sure that: `test = Ad(ad).get_insights()` raise the exception in your case ? Can you try to accept all exception to see which one is raised if any ? – CyDevos Jul 06 '21 at 09:56
  • sorry -- the exception is thrown once the API limit is reached, not necessarily for that particular call – nikUoM Jul 06 '21 at 12:16

1 Answers1

1

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.

Kemp
  • 3,467
  • 1
  • 18
  • 27
  • many thanks. this fixed it for me: `from facebook_business.exceptions import *` and then later: `except FacebookRequestError as e:` – nikUoM Jul 06 '21 at 12:28