diff --git a/b2sdk/_internal/b2http.py b/b2sdk/_internal/b2http.py index 4044a1014..2f0df66a7 100644 --- a/b2sdk/_internal/b2http.py +++ b/b2sdk/_internal/b2http.py @@ -545,14 +545,16 @@ def _translate_errors(cls, fcn, post_params=None): raise UnknownHost() elif isinstance(e1, requests.packages.urllib3.exceptions.ProtocolError): e2 = e1.args[1] + + if isinstance(e2, TimeoutError): + raise B2RequestTimeout(str(e0)) + if isinstance(e2, socket.error): if len(e2.args) >= 2 and e2.args[1] == 'Broken pipe': # Broken pipes are usually caused by the service rejecting # an upload request for cause, so we use a 400 Bad Request # code. raise BrokenPipe() - elif isinstance(e2, TimeoutError): - raise B2RequestTimeout(str(e0)) raise B2ConnectionError(str(e0)) except requests.Timeout as e: diff --git a/changelog.d/+timeout_error.fixed.md b/changelog.d/+timeout_error.fixed.md new file mode 100644 index 000000000..6423e4a5f --- /dev/null +++ b/changelog.d/+timeout_error.fixed.md @@ -0,0 +1 @@ +Fix TimeoutError handling in `b2http`. diff --git a/test/unit/b2http/test_b2http.py b/test/unit/b2http/test_b2http.py index 7f3020c51..28ab4dad5 100644 --- a/test/unit/b2http/test_b2http.py +++ b/test/unit/b2http/test_b2http.py @@ -20,6 +20,7 @@ from apiver_deps import USER_AGENT, B2Http, B2HttpApiConfig, ClockSkewHook from apiver_deps_exception import ( B2ConnectionError, + B2RequestTimeout, BadDateFormat, BadJson, BadRequest, @@ -80,6 +81,17 @@ def fcn(): with pytest.raises(UnknownHost): B2Http._translate_errors(fcn) + def test_request_timeout(self): + def fcn(): + raise requests.ConnectionError( + requests.packages.urllib3.exceptions.ProtocolError( + 'dummy', TimeoutError('The write operation timed out') + ) + ) + + with pytest.raises(B2RequestTimeout): + B2Http._translate_errors(fcn) + def test_connection_error(self): def fcn(): raise requests.ConnectionError('a message')