Skip to content

Commit 8c0b11d

Browse files
committed
intermediate commit to getting tests passing with new exceptions
1 parent e880c69 commit 8c0b11d

16 files changed

+68
-120
lines changed

README.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ reason, please let us know through the Github issues system. Thank you.
5050
Exception Handling
5151
~~~~~~~~~~~~~~~~~~
5252

53-
In Python Storage 3.x, the dependency `google-resumable-media` was integrated.
53+
In Python Storage 3.0, the dependency `google-resumable-media` was integrated.
5454
The `google-resumable-media` dependency included exceptions
5555
`google.resumable_media.common.InvalidResponse` and
5656
`google.resumable_media.common.DataCorruption`, which were often imported
@@ -71,7 +71,6 @@ as not to break user applications following this pattern,
7171
setup.py file. Applications which do not import directly from
7272
`google-resumable-media` can safely disregard this dependency.
7373

74-
7574
Quick Start
7675
-----------
7776

google/cloud/storage/_media/_download.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from google.cloud.storage._media import _helpers
2222
from google.cloud.storage._media import common
23+
from google.cloud.storage.exceptions import InvalidResponse
2324

2425

2526
_CONTENT_RANGE_RE = re.compile(
@@ -361,7 +362,7 @@ def _process_response(self, response):
361362
response (object): The HTTP response object (need headers).
362363
363364
Raises:
364-
~google.cloud.storage._media.common.InvalidResponse: If the number
365+
~google.cloud.storage.exceptions.InvalidResponse: If the number
365366
of bytes in the body doesn't match the content length header.
366367
367368
.. _sans-I/O: https://sans-io.readthedocs.io/
@@ -398,7 +399,7 @@ def _process_response(self, response):
398399
num_bytes = int(content_length)
399400
if len(response_body) != num_bytes:
400401
self._make_invalid()
401-
raise common.InvalidResponse(
402+
raise InvalidResponse(
402403
response,
403404
"Response is different size than content-length",
404405
"Expected",
@@ -508,7 +509,7 @@ def get_range_info(response, get_headers, callback=_helpers.do_nothing):
508509
Tuple[int, int, int]: The start byte, end byte and total bytes.
509510
510511
Raises:
511-
~google.cloud.storage._media.common.InvalidResponse: If the
512+
~google.cloud.storage.exceptions.InvalidResponse: If the
512513
``Content-Range`` header is not of the form
513514
``bytes {start}-{end}/{total}``.
514515
"""
@@ -518,7 +519,7 @@ def get_range_info(response, get_headers, callback=_helpers.do_nothing):
518519
match = _CONTENT_RANGE_RE.match(content_range)
519520
if match is None:
520521
callback()
521-
raise common.InvalidResponse(
522+
raise InvalidResponse(
522523
response,
523524
"Unexpected content-range header",
524525
content_range,

google/cloud/storage/_media/_helpers.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from urllib.parse import urlunsplit
2929

3030
from google.cloud.storage._media import common
31+
from google.cloud.storage.exceptions import InvalidResponse
3132

3233

3334
RANGE_HEADER = "range"
@@ -70,13 +71,13 @@ def header_required(response, name, get_headers, callback=do_nothing):
7071
str: The desired header.
7172
7273
Raises:
73-
~google.cloud.storage._media.common.InvalidResponse: If the header
74+
~google.cloud.storage.exceptions.InvalidResponse: If the header
7475
is missing.
7576
"""
7677
headers = get_headers(response)
7778
if name not in headers:
7879
callback()
79-
raise common.InvalidResponse(
80+
raise InvalidResponse(
8081
response, "Response headers must contain header", name
8182
)
8283

@@ -98,14 +99,14 @@ def require_status_code(response, status_codes, get_status_code, callback=do_not
9899
int: The status code.
99100
100101
Raises:
101-
~google.cloud.storage._media.common.InvalidResponse: If the status code
102+
~google.cloud.storage.exceptions.InvalidResponse: If the status code
102103
is not one of the values in ``status_codes``.
103104
"""
104105
status_code = get_status_code(response)
105106
if status_code not in status_codes:
106107
if status_code not in common.RETRYABLE:
107108
callback()
108-
raise common.InvalidResponse(
109+
raise InvalidResponse(
109110
response,
110111
"Request failed with status code",
111112
status_code,
@@ -298,7 +299,7 @@ def _parse_checksum_header(header_value, response, checksum_label):
298299
can be detected from the ``X-Goog-Hash`` header; otherwise, None.
299300
300301
Raises:
301-
~google.cloud.storage._media.common.InvalidResponse: If there are
302+
~google.cloud.storage.exceptions.InvalidResponse: If there are
302303
multiple checksums of the requested type in ``header_value``.
303304
"""
304305
if header_value is None:
@@ -316,7 +317,7 @@ def _parse_checksum_header(header_value, response, checksum_label):
316317
elif len(matches) == 1:
317318
return matches[0]
318319
else:
319-
raise common.InvalidResponse(
320+
raise InvalidResponse(
320321
response,
321322
"X-Goog-Hash header had multiple ``{}`` values.".format(checksum_label),
322323
header_value,

google/cloud/storage/_media/_upload.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from google.cloud.storage._media import _helpers
3333
from google.cloud.storage._media import common
3434
from google.cloud.storage._media import UPLOAD_CHUNK_SIZE
35+
from google.cloud.storage.exceptions import InvalidResponse
3536

3637
from xml.etree import ElementTree
3738

@@ -114,7 +115,7 @@ def _process_response(self, response):
114115
response (object): The HTTP response object.
115116
116117
Raises:
117-
~google.cloud.storage._media.common.InvalidResponse: If the status
118+
~google.cloud.storage.exceptions.InvalidResponse: If the status
118119
code is not 200.
119120
120121
.. _sans-I/O: https://sans-io.readthedocs.io/
@@ -679,10 +680,10 @@ def _process_resumable_response(self, response, bytes_sent):
679680
``response`` was returned for.
680681
681682
Raises:
682-
~google.cloud.storage._media.common.InvalidResponse: If the status
683+
~google.cloud.storage.exceptions.InvalidResponse: If the status
683684
code is 308 and the ``range`` header is not of the form
684685
``bytes 0-{end}``.
685-
~google.cloud.storage._media.common.InvalidResponse: If the status
686+
~google.cloud.storage.exceptions.InvalidResponse: If the status
686687
code is not 200 or 308.
687688
688689
.. _sans-I/O: https://sans-io.readthedocs.io/
@@ -717,7 +718,7 @@ def _process_resumable_response(self, response, bytes_sent):
717718
match = _BYTES_RANGE_RE.match(bytes_range)
718719
if match is None:
719720
self._make_invalid()
720-
raise common.InvalidResponse(
721+
raise InvalidResponse(
721722
response,
722723
'Unexpected "range" header',
723724
bytes_range,
@@ -742,7 +743,7 @@ def _validate_checksum(self, response):
742743
metadata = response.json()
743744
remote_checksum = metadata.get(metadata_key)
744745
if remote_checksum is None:
745-
raise common.InvalidResponse(
746+
raise InvalidResponse(
746747
response,
747748
_UPLOAD_METADATA_NO_APPROPRIATE_CHECKSUM_MESSAGE.format(metadata_key),
748749
self._get_headers(response),
@@ -818,9 +819,9 @@ def _process_recover_response(self, response):
818819
response (object): The HTTP response object.
819820
820821
Raises:
821-
~google.cloud.storage._media.common.InvalidResponse: If the status
822+
~google.cloud.storage.exceptions.InvalidResponse: If the status
822823
code is not 308.
823-
~google.cloud.storage._media.common.InvalidResponse: If the status
824+
~google.cloud.storage.exceptions.InvalidResponse: If the status
824825
code is 308 and the ``range`` header is not of the form
825826
``bytes 0-{end}``.
826827
@@ -834,7 +835,7 @@ def _process_recover_response(self, response):
834835
bytes_range = headers[_helpers.RANGE_HEADER]
835836
match = _BYTES_RANGE_RE.match(bytes_range)
836837
if match is None:
837-
raise common.InvalidResponse(
838+
raise InvalidResponse(
838839
response,
839840
'Unexpected "range" header',
840841
bytes_range,
@@ -980,7 +981,7 @@ def _process_initiate_response(self, response):
980981
response (object): The HTTP response object.
981982
982983
Raises:
983-
~google.cloud.storage._media.common.InvalidResponse: If the status
984+
~google.cloud.storage.exceptions.InvalidResponse: If the status
984985
code is not 200.
985986
986987
.. _sans-I/O: https://sans-io.readthedocs.io/
@@ -1055,7 +1056,7 @@ def _process_finalize_response(self, response):
10551056
response (object): The HTTP response object.
10561057
10571058
Raises:
1058-
~google.cloud.storage._media.common.InvalidResponse: If the status
1059+
~google.cloud.storage.exceptions.InvalidResponse: If the status
10591060
code is not 200.
10601061
10611062
.. _sans-I/O: https://sans-io.readthedocs.io/
@@ -1119,7 +1120,7 @@ def _process_cancel_response(self, response):
11191120
response (object): The HTTP response object.
11201121
11211122
Raises:
1122-
~google.cloud.storage._media.common.InvalidResponse: If the status
1123+
~google.cloud.storage.exceptions.InvalidResponse: If the status
11231124
code is not 204.
11241125
11251126
.. _sans-I/O: https://sans-io.readthedocs.io/
@@ -1297,7 +1298,7 @@ def _process_upload_response(self, response):
12971298
response (object): The HTTP response object.
12981299
12991300
Raises:
1300-
~google.cloud.storage._media.common.InvalidResponse: If the status
1301+
~google.cloud.storage.exceptions.InvalidResponse: If the status
13011302
code is not 200 or the response is missing data.
13021303
13031304
.. _sans-I/O: https://sans-io.readthedocs.io/
@@ -1357,7 +1358,7 @@ def _validate_checksum(self, response):
13571358

13581359
if remote_checksum is None:
13591360
metadata_key = _helpers._get_metadata_key(self._checksum_type)
1360-
raise common.InvalidResponse(
1361+
raise InvalidResponse(
13611362
response,
13621363
_UPLOAD_METADATA_NO_APPROPRIATE_CHECKSUM_MESSAGE.format(metadata_key),
13631364
self._get_headers(response),

google/cloud/storage/_media/common.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,6 @@
2626
UPLOAD_CHUNK_SIZE = 262144 # 256 * 1024
2727
"""int: Chunks in a resumable upload must come in multiples of 256 KB."""
2828

29-
PERMANENT_REDIRECT = http.client.PERMANENT_REDIRECT # type: ignore
30-
"""int: Permanent redirect status code.
31-
32-
.. note::
33-
This is a backward-compatibility alias.
34-
35-
It is used by Google services to indicate some (but not all) of
36-
a resumable upload has been completed.
37-
38-
For more information, see `RFC 7238`_.
39-
40-
.. _RFC 7238: https://tools.ietf.org/html/rfc7238
41-
"""
42-
43-
TOO_MANY_REQUESTS = http.client.TOO_MANY_REQUESTS
44-
"""int: Status code indicating rate-limiting.
45-
46-
.. note::
47-
This is a backward-compatibility alias.
48-
49-
For more information, see `RFC 6585`_.
50-
51-
.. _RFC 6585: https://tools.ietf.org/html/rfc6585#section-4
52-
"""
53-
5429
MAX_SLEEP = 64.0
5530
"""float: Maximum amount of time allowed between requests.
5631
@@ -80,36 +55,6 @@
8055
"""
8156

8257

83-
class InvalidResponse(Exception):
84-
"""Error class for responses which are not in the correct state.
85-
86-
Args:
87-
response (object): The HTTP response which caused the failure.
88-
args (tuple): The positional arguments typically passed to an
89-
exception class.
90-
"""
91-
92-
def __init__(self, response, *args):
93-
super(InvalidResponse, self).__init__(*args)
94-
self.response = response
95-
"""object: The HTTP response object that caused the failure."""
96-
97-
98-
class DataCorruption(Exception):
99-
"""Error class for corrupt media transfers.
100-
101-
Args:
102-
response (object): The HTTP response which caused the failure.
103-
args (tuple): The positional arguments typically passed to an
104-
exception class.
105-
"""
106-
107-
def __init__(self, response, *args):
108-
super(DataCorruption, self).__init__(*args)
109-
self.response = response
110-
"""object: The HTTP response object that caused the failure."""
111-
112-
11358
class RetryStrategy(object):
11459
"""Configuration class for retrying failed requests.
11560

google/cloud/storage/_media/requests/_request_helpers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import time
2525

26+
from google.cloud.storage.exceptions import InvalidResponse
2627
from google.cloud.storage._media import common
2728
from google.cloud.storage._media import _helpers
2829

@@ -120,8 +121,8 @@ def wait_and_retry(func, get_status_code, retry_strategy):
120121
Expects ``func`` to return an HTTP response and uses ``get_status_code``
121122
to check if the response is retry-able.
122123
123-
``func`` is expected to raise a failure status code as a
124-
common.InvalidResponse, at which point this method will check the code
124+
``func`` is expected to raise a failure status code as an
125+
InvalidResponse, at which point this method will check the code
125126
against the common.RETRIABLE list of retriable status codes.
126127
127128
Will retry until :meth:`~.RetryStrategy.retry_allowed` (on the current
@@ -155,7 +156,7 @@ def wait_and_retry(func, get_status_code, retry_strategy):
155156
response = func()
156157
except _CONNECTION_ERROR_CLASSES as e:
157158
error = e # Fall through to retry, if there are retries left.
158-
except common.InvalidResponse as e:
159+
except InvalidResponse as e:
159160
# An InvalidResponse is only retriable if its status code matches.
160161
# The `process_response()` method on a Download or Upload method
161162
# will convert the status code into an exception.

google/cloud/storage/_media/requests/upload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def transmit_next_chunk(
494494
~requests.Response: The HTTP response returned by ``transport``.
495495
496496
Raises:
497-
~google.cloud.storage._media.common.InvalidResponse: If the status
497+
~google.cloud.storage.exceptions.InvalidResponse: If the status
498498
code is not 200 or http.client.PERMANENT_REDIRECT.
499499
~google.cloud.storage._media.common.DataCorruption: If this is the final
500500
chunk, a checksum validation was requested, and the checksum

0 commit comments

Comments
 (0)