@@ -68,8 +68,8 @@ if __name__ == '__main__':
6868A more complex example follows.
6969
7070``` python
71- import sys
7271import CloudFlare
72+ import CloudFlare.exceptions
7373
7474def main ():
7575 zone_name = ' example.com'
@@ -79,19 +79,22 @@ def main():
7979 # query for the zone name and expect only one value back
8080 try :
8181 zones = cf.zones.get(params = {' name' :zone_name,' per_page' :1 })
82- except CloudFlare.CloudFlareAPIError as e:
82+ except CloudFlare.exceptions. CloudFlareAPIError as e:
8383 exit (' /zones.get %d %s - api call failed' % (e, e))
8484 except Exception as e:
8585 exit (' /zones.get - %s - api call failed' % (e))
8686
87+ if len (zones) == 0 :
88+ exit (' No zones found' )
89+
8790 # extract the zone_id which is needed to process that zone
8891 zone = zones[0 ]
8992 zone_id = zone[' id' ]
9093
9194 # request the DNS records from that zone
9295 try :
9396 dns_records = cf.zones.dns_records.get(zone_id)
94- except CloudFlare.CloudFlareAPIError as e:
97+ except CloudFlare.exceptions. CloudFlareAPIError as e:
9598 exit (' /zones/dns_records.get %d %s - api call failed' % (e, e))
9699
97100 # print the results - first the zone name
@@ -171,7 +174,9 @@ You can leave *extras* in the configuration with a blank value (or omit the opti
171174
172175## Exceptions and return values
173176
174- The response is build from the JSON in the API call.
177+ ### Response data
178+
179+ The response is build from the JSON in the API call.
175180It contains the ** results** values; but does not contain the paging values.
176181
177182You can return all the paging values by calling the class with raw=True. Here's an example without paging.
@@ -235,6 +240,26 @@ This produces.
235240
236241A full example of paging is provided below.
237242
243+
244+ ### Exceptions
245+
246+ The library will raise ** CloudFlareAPIError** when the API call fails.
247+ The exception returns both an integer and textual message in one value.
248+
249+ ``` python
250+ import CloudFlare
251+ import CloudFlare.exceptions
252+
253+ ...
254+ try
255+ r = ...
256+ except CloudFlare.exceptions.CloudFlareAPIError as e:
257+ exit (' api error: %d %s ' % (e, e))
258+ ...
259+ ```
260+
261+ The other raised response is ** CloudFlareInternalError** which can happen when calling an invalid method.
262+
238263## Included example code
239264
240265The [ examples] ( https://github.com/cloudflare/python-cloudflare/tree/master/examples ) folder contains many examples in both simple and verbose formats.
0 commit comments