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

Commit 2da9a7b

Browse files
committed
handle 404 errors with invalid API error structure
1 parent f39b3c1 commit 2da9a7b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

CloudFlare/cloudflare.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,20 @@ def _call_network(self, method, headers, parts, identifiers, params, data_str, d
339339
# lets check and convert if able
340340
try:
341341
j = json.loads(response_data)
342-
if 'success' not in j and 'errors' not in j:
343-
# no go - it's not a Cloudflare error format
344-
pass
345-
else:
342+
if len(j) == 2 and 'code' in j and 'error' in j:
343+
# This is an incorrect response from the API (happens on 404's) - but we can handle it cleanly here
344+
# {\n "code": 1000,\n "error": "not_found"\n}
345+
response_data = '{"errors": [{"code": %d, "message": "%s"}], "success": false, "result": null}' % (j['code'], j['error'])
346+
response_data = response_data.encode()
347+
response_code = 200
348+
elif 'success' in j and 'errors' in j:
346349
# yippe - try to continue by allowing to process fully
347350
response_code = 200
351+
else:
352+
# no go - it's not a Cloudflare error format
353+
pass
348354
except (ValueError, json.decoder.JSONDecodeError):
349-
# ignore - maybe a real error, let proceed!
355+
# ignore - maybe a real error that's not json, let proceed!
350356
pass
351357

352358
if 500 <= response_code <= 599:
@@ -506,6 +512,8 @@ def _call(self, method, parts, identifiers, params, data_str, data_json, files):
506512

507513
# Sanatize the returned results - just in case API is messed up
508514
if 'success' not in response_data:
515+
# { "data": null, "errors": [ { "message": "request must be a POST", "path": null, "extensions": { "timestamp": "20...
516+
# XXX/TODO should be retested and aybe recoded/deleted
509517
if 'errors' in response_data:
510518
if response_data['errors'] is None:
511519
# Only happens on /graphql call

0 commit comments

Comments
 (0)