March 2019
Beginner
490 pages
12h 40m
English
S3 embeds error messages in the XML, returned in the response body, and until now we've just been dumping the raw XML to the screen. We can improve on this and pull the text out of the XML. First, let's generate an error message so that we can see what the XML looks like. In s3_list_buckets.py, if we replace the access secret with an empty string, then it will produce an error.
This is the function we can use for handling errors:
def handle_error(response): output = 'Status code: {}\n'.format(response.status_code) root = ET.fromstring(response.text) code = root.find('Code').text output += 'Error code: {}\n'.format(code) message = root.find('Message').text output += 'Message: {}\n'.format(message) print(output) ...Read now
Unlock full access