From c22db3e34977451c87e92b9de581888e191d42c8 Mon Sep 17 00:00:00 2001 From: Martin Levy Date: Wed, 10 Aug 2022 14:20:19 -0700 Subject: [PATCH] cleanup of error handling for adding api calls --- CloudFlare/cloudflare.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/CloudFlare/cloudflare.py b/CloudFlare/cloudflare.py index 845680e..2f61bee 100644 --- a/CloudFlare/cloudflare.py +++ b/CloudFlare/cloudflare.py @@ -857,17 +857,21 @@ def add(self, t, p1, p2=None, p3=None, p4=None, p5=None): else: branch = getattr(branch, element) except: - # should never happen - raise CloudFlareAPIError(0, 'api load name failed') - name = a[-1] + # missing path - should never happen unless api_v4 is a busted file + branch = None + break + if not branch: + raise CloudFlareAPIError(0, 'api load: element **%s** missing when adding path /%s' % (element, '/'.join(a))) + + name = a[-1] try: if '-' in name: f = getattr(branch, name.replace('-','_')) else: f = getattr(branch, name) - # already exists - don't let it overwrite - raise CloudFlareAPIError(0, 'api duplicate name found: %s/**%s**' % ('/'.join(a[0:-1]), name)) + # already exists - don't let it overwrite - should never happen unless api_v4 is a busted file + raise CloudFlareAPIError(0, 'api load: duplicate name found: %s/**%s**' % ('/'.join(a[0:-1]), name)) except AttributeError: # this is the required behavior - i.e. it's a new node to create pass @@ -968,9 +972,12 @@ def __init__(self, email=None, token=None, certtoken=None, debug=False, raw=Fals self._base = self._v4base(config) # add the API calls - api_v4(self) - if 'extras' in config and config['extras']: - api_extras(self, config['extras']) + try: + api_v4(self) + if 'extras' in config and config['extras']: + api_extras(self, config['extras']) + except Exception as e: + raise CloudFlareAPIError(0, str(e)) def __call__(self): """ Cloudflare v4 API"""