Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 3650b3e

Browse files
committed
error_chain can now be read when an error happens
1 parent 5f78fcd commit 3650b3e

5 files changed

Lines changed: 105 additions & 10 deletions

File tree

CloudFlare/cloudflare.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,18 @@ def _call(self, method, headers,
187187
if response_data['success'] is False:
188188
code = response_data['errors'][0]['code']
189189
message = response_data['errors'][0]['message']
190-
if self.logger:
191-
self.logger.debug('Response: error %d %s' % (code, message))
192-
raise CloudFlareAPIError(code, message)
190+
if 'error_chain' in response_data['errors'][0]:
191+
error_chain = response_data['errors'][0]['error_chain']
192+
for error in error_chain:
193+
if self.logger:
194+
self.logger.debug('Response: error %d %s - chain' % (error['code'], error['message']))
195+
if self.logger:
196+
self.logger.debug('Response: error %d %s' % (code, message))
197+
raise CloudFlareAPIError(code, message, error_chain)
198+
else:
199+
if self.logger:
200+
self.logger.debug('Response: error %d %s' % (code, message))
201+
raise CloudFlareAPIError(code, message)
193202

194203
if self.logger:
195204
self.logger.debug('Response: %s' % (response_data['result']))

CloudFlare/exceptions.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,62 @@
33
class CloudFlareError(Exception):
44
""" errors for Cloudflare API"""
55

6-
def __init__(self, code, message):
6+
class _code_message(object):
7+
""" a small class to save away an interger and string (the code and the message)"""
8+
9+
def __init__(self, code, message):
10+
self.code = code
11+
self.message = message
12+
def __int__(self):
13+
return self.code
14+
def __str__(self):
15+
return self.message
16+
17+
def __init__(self, code, message, error_chain=None):
718
""" errors for Cloudflare API"""
819

9-
self.code = code
10-
self.message = message
20+
self.e = self._code_message(int(code), str(message))
21+
self.error_chain = None
22+
if error_chain != None:
23+
self.error_chain = []
24+
for e in error_chain:
25+
self.error_chain.append(self._code_message(int(e['code']), str(e['message'])))
26+
# self.error_chain.append({'code': self.code, 'message': str(self.message)})
1127

1228
def __int__(self):
13-
""" errors for Cloudflare API"""
29+
""" integer value for Cloudflare API errors"""
1430

15-
return self.code
31+
return int(self.e)
1632

1733
def __str__(self):
18-
""" errors for Cloudflare API"""
34+
""" string value for Cloudflare API errors"""
35+
36+
return str(self.e)
37+
38+
def __len__(self):
39+
""" Cloudflare API errors can contain a chain of errors"""
40+
41+
if self.error_chain == None:
42+
return 0
43+
else:
44+
return len(self.error_chain)
45+
46+
def __getitem__(self, ii):
47+
""" Cloudflare API errors can contain a chain of errors"""
48+
49+
return self.error_chain[ii]
50+
51+
def __iter__(self):
52+
""" Cloudflare API errors can contain a chain of errors"""
53+
54+
if self.error_chain == None:
55+
raise StopIteration
56+
for e in self.error_chain:
57+
yield e
1958

20-
return self.message
59+
def next(self):
60+
if self.error_chain == None:
61+
raise StopIteration()
2162

2263
class CloudFlareAPIError(CloudFlareError):
2364
""" errors for Cloudflare API"""

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,26 @@ import CloudFlare.exceptions
260260

261261
The other raised response is **CloudFlareInternalError** which can happen when calling an invalid method.
262262

263+
In some cases more than one error is returned. In this case the return value **e** is also an array.
264+
You can itterate over that array to see the additional error.
265+
266+
```python
267+
import sys
268+
import CloudFlare
269+
import CloudFlare.exceptions
270+
271+
...
272+
try
273+
r = ...
274+
except CloudFlare.exceptions.CloudFlareAPIError as e:
275+
if len(e) > 0:
276+
sys.stderr.write('api error - more than one error value returned!\n')
277+
for x in e:
278+
sys.stderr.write('api error: %d %s\n' % (x, x))
279+
exit('api error: %d %s' % (e, e))
280+
...
281+
```
282+
263283
## Included example code
264284

265285
The [examples](https://github.com/cloudflare/python-cloudflare/tree/master/examples) folder contains many examples in both simple and verbose formats.

README.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,27 @@ The exception returns both an integer and textual message in one value.
292292
The other raised response is **CloudFlareInternalError** which can
293293
happen when calling an invalid method.
294294

295+
In some cases more than one error is returned. In this case the return
296+
value **e** is also an array. You can itterate over that array to see
297+
the additional error.
298+
299+
.. code:: python
300+
301+
import sys
302+
import CloudFlare
303+
import CloudFlare.exceptions
304+
305+
...
306+
try
307+
r = ...
308+
except CloudFlare.exceptions.CloudFlareAPIError as e:
309+
if len(e) > 0:
310+
sys.stderr.write('api error - more than one error value returned!\n')
311+
for x in e:
312+
sys.stderr.write('api error: %d %s\n' % (x, x))
313+
exit('api error: %d %s' % (e, e))
314+
...
315+
295316
Included example code
296317
---------------------
297318

cli4/cli4.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ def cli4(args):
312312
else:
313313
pass
314314
except CloudFlare.exceptions.CloudFlareAPIError as e:
315+
if len(e) > 0:
316+
# more than one error returned by the API
317+
for x in e:
318+
sys.stderr.write('cli4: /%s - %d %s\n' % (command, x, x))
315319
exit('cli4: /%s - %d %s' % (command, e, e))
316320
except Exception as e:
317321
exit('cli4: /%s - %s - api error' % (command, e))

0 commit comments

Comments
 (0)