I might be approaching this the wrong way, but I've got a POST request going out:
response = requests.post(full_url, json.dumps(data))
Which could potentially fail for a number of reasons, some being related to the data, some being temporary failures, which due to a poorly designed endpoint may well return as the same error (server does unpredictable things with invalid data). To catch these temporary failures and let others pass I thought the best way to go about this would be to retry once and then continue if the error is raised again. I believe I could do it with a nested try/except, but it seems like bad practice to me (what if I want to try twice before giving up?)
That solution would be:
try:
    response = requests.post(full_url, json.dumps(data))
except RequestException:
    try:
        response = requests.post(full_url, json.dumps(data))
    except:
        continue
Is there a better way to do this? Alternately is there a better way in general to deal with potentially faulty HTTP responses?
By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.
for _ in range(2):
    try:
        response = requests.post(full_url, json.dumps(data))
        break
    except RequestException:
        pass
else:
    raise # both tries failed
If you need a function for this:
def multiple_tries(func, times, exceptions):
    for _ in range(times):
        try:
            return func()
        except Exception as e:
            if not isinstance(e, exceptions):
                raise # reraises unexpected exceptions 
    raise # reraises if attempts are unsuccessful
Use like this:
func = lambda:requests.post(full_url, json.dumps(data))
response = multiple_tries(func, 2, RequestException)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With