From 3193d120e5a2e11906042482faa12a05a2d079bc Mon Sep 17 00:00:00 2001 From: Gael de Chalendar Date: Tue, 4 Jun 2019 18:53:58 +0200 Subject: [PATCH] Avoid to crash in case of HTTP 502 error Survive also on socket.error connections like on HTTPError or URLError. This should solve issue #110. --- bin/github-backup | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/bin/github-backup b/bin/github-backup index 88409de..d862641 100755 --- a/bin/github-backup +++ b/bin/github-backup @@ -1,6 +1,7 @@ #!/usr/bin/env python from __future__ import print_function +import socket import argparse import base64 @@ -404,6 +405,16 @@ def retrieve_data(args, template, query_args=None, single_request=False): status_code = int(r.getcode()) + retries = 0 + while retries < 3 and status_code == 502: + print('API request returned HTTP 502: Bad Gateway. Retrying in 5 seconds') + retries += 1 + time.sleep(5) + request = _construct_request(per_page, page, query_args, template, auth) # noqa + r, errors = _get_response(request, auth, template) + + status_code = int(r.getcode()) + if status_code != 200: template = 'API request returned HTTP {0}: {1}' errors.append(template.format(status_code, r.reason)) @@ -450,6 +461,11 @@ def _get_response(request, auth, template): should_continue = _request_url_error(template, retry_timeout) if not should_continue: raise + except socket.error as e: + log_warning(e.strerror) + should_continue = _request_url_error(template, retry_timeout) + if not should_continue: + raise if should_continue: continue