fix: Catch ECONNRESET and other errors more reliably (#1147)
Fixes #1146 🦕
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index e842bb4..5e582cf 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -28,9 +28,7 @@
from six import BytesIO, StringIO
from six.moves.urllib.parse import urlparse, urlunparse, quote, unquote
-import base64
import copy
-import gzip
import httplib2
import json
import logging
@@ -38,7 +36,6 @@
import os
import random
import socket
-import sys
import time
import uuid
@@ -190,11 +187,16 @@
exception = connection_error
except socket.error as socket_error:
# errno's contents differ by platform, so we have to match by name.
+ # Some of these same errors may have been caught above, e.g. ECONNRESET *should* be
+ # raised as a ConnectionError, but some libraries will raise it as a socket.error
+ # with an errno corresponding to ECONNRESET
if socket.errno.errorcode.get(socket_error.errno) not in {
"WSAETIMEDOUT",
"ETIMEDOUT",
"EPIPE",
"ECONNABORTED",
+ "ECONNREFUSED",
+ "ECONNRESET",
}:
raise
exception = socket_error
diff --git a/tests/test_http.py b/tests/test_http.py
index 9bfae93..f87d7e1 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -174,10 +174,10 @@
# set errno to a non-retriable value
try:
# For Windows:
- ex.errno = socket.errno.WSAECONNREFUSED
+ ex.errno = socket.errno.WSAEHOSTUNREACH
except AttributeError:
# For Linux/Mac:
- ex.errno = socket.errno.ECONNREFUSED
+ ex.errno = socket.errno.EHOSTUNREACH
# Now raise the correct timeout error.
raise ex