Content-Length: 321852 | pFad | https://www.github.com/googleapis/python-storage/commit/318a286d709427bfe9f3a37e933c255ac51b3033

F75 fix: check response code in batch.finish (#609) · googleapis/python-storage@318a286 · GitHub
Skip to content

Commit 318a286

Browse files
authored
fix: check response code in batch.finish (#609)
* add top-level response status code check to batch.finish * test exception is raised when batch request fails
1 parent 283a419 commit 318a286

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

google/cloud/storage/batch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ def finish(self):
276276
response = self._client._base_connection._make_request(
277277
"POST", url, data=body, headers=headers, timeout=timeout
278278
)
279+
280+
# Raise exception if the top-level batch request fails
281+
if not 200 <= response.status_code < 300:
282+
raise exceptions.from_http_response(response)
283+
279284
responses = list(_unpack_batch_response(response))
280285
self._finish_futures(responses)
281286
return responses

tests/unit/test_batch.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,24 @@ def test_finish_nonempty_non_multipart_response(self):
447447
with self.assertRaises(ValueError):
448448
batch.finish()
449449

450+
def test_finish_multipart_response_with_status_failure(self):
451+
from google.cloud.exceptions import ServiceUnavailable
452+
453+
url = "http://api.example.com/other_api"
454+
expected_response = _make_response(
455+
status=http_client.SERVICE_UNAVAILABLE,
456+
headers={"content-type": 'multipart/mixed; boundary="DEADBEEF="'},
457+
)
458+
http = _make_requests_session([expected_response])
459+
connection = _Connection(http=http)
460+
client = _Client(connection)
461+
batch = self._make_one(client)
462+
batch.API_BASE_URL = "http://api.example.com"
463+
batch._requests.append(("POST", url, {}, {"foo": 1, "bar": 2}, None))
464+
465+
with self.assertRaises(ServiceUnavailable):
466+
batch.finish()
467+
450468
def test_as_context_mgr_wo_error(self):
451469
from google.cloud.storage.client import Client
452470

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.github.com/googleapis/python-storage/commit/318a286d709427bfe9f3a37e933c255ac51b3033

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy