|
46 | 46 | from six.moves import urllib |
47 | 47 | from requests import Response |
48 | 48 | from tqdm import tqdm |
49 | | -from requests.exceptions import HTTPError |
| 49 | +from requests.exceptions import HTTPError, ConnectionError |
| 50 | +from urllib3.exceptions import ProtocolError |
| 51 | +try: |
| 52 | + from http.client import RemoteDisconnected |
| 53 | +except ImportError: |
| 54 | + from httplib import BadStatusLine as RemoteDisconnected |
50 | 55 |
|
51 | 56 | from internetarchive.utils import IdentifierListAsItems, get_md5, chunk_generator, \ |
52 | 57 | IterableToFileAdapter, iter_directory, recursive_file_count, norm_filepath |
@@ -1096,6 +1101,21 @@ def _build_request(): |
1096 | 1101 | print(' error uploading {0}: {1}'.format(key, msg), file=sys.stderr) |
1097 | 1102 | # Raise HTTPError with error message. |
1098 | 1103 | raise type(exc)(error_msg, response=exc.response, request=exc.request) |
| 1104 | + except ConnectionError as exc: # from requests |
| 1105 | + exc = exc.args[0] |
| 1106 | + if isinstance(exc, ProtocolError): # from urllib3 |
| 1107 | + exc = exc.args[1] |
| 1108 | + if isinstance(exc, RemoteDisconnected): # from http.client |
| 1109 | + msg = ("The server closed the connection after the file was uploaded. " |
| 1110 | + "The upload might have succeeded anyway.") |
| 1111 | + error_msg = (' error uploading {0} to {1}, ' |
| 1112 | + '{2}'.format(key, self.identifier, msg)) |
| 1113 | + log.error(error_msg) |
| 1114 | + return Response() |
| 1115 | + else: |
| 1116 | + raise |
| 1117 | + else: |
| 1118 | + raise |
1099 | 1119 | finally: |
1100 | 1120 | body.close() |
1101 | 1121 |
|
|
0 commit comments