Skip to content

Strip excess custom exceptions from 'gcloud.streaming' #1232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 19, 2015
4 changes: 1 addition & 3 deletions gcloud/streaming/buffered_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
This class reads ahead to detect if we are at the end of the stream.
"""

from gcloud.streaming.exceptions import NotYetImplementedError


class BufferedStream(object):
"""Buffers a stream, reading ahead to determine if we're at the end.
Expand Down Expand Up @@ -65,7 +63,7 @@ def read(self, size=None):
:param size: How many bytes to read (defaults to all remaining bytes).
"""
if size is None or size < 0:
raise NotYetImplementedError(
raise ValueError(
'Illegal read of size %s requested on BufferedStream. '
'Wrapped stream %s is at position %s-%s, '
'%s bytes remaining.' %
Expand Down
28 changes: 0 additions & 28 deletions gcloud/streaming/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ class Error(Exception):
"""Base class for all exceptions."""


class NotFoundError(Error):
"""A specified resource could not be found."""


class UserError(Error):
"""Base class for errors related to user input."""


class InvalidDataError(Error):
"""Base class for any invalid data error."""


class CommunicationError(Error):
"""Any communication error talking to an API server."""

Expand Down Expand Up @@ -66,14 +54,6 @@ def from_response(cls, http_response):
http_response.request_url)


class InvalidUserInputError(InvalidDataError):
"""User-provided input is invalid."""


class ConfigurationValueError(UserError):
"""Some part of the user-specified client configuration is invalid."""


class TransferError(CommunicationError):
"""Errors related to transfers."""

Expand Down Expand Up @@ -124,11 +104,3 @@ def from_response(cls, http_response):

class BadStatusCodeError(HttpError):
"""The request completed but returned a bad status code."""


class NotYetImplementedError(Error):
"""This functionality is not yet implemented."""


class StreamExhausted(Error):
"""Attempted to read more bytes from a stream than were available."""
13 changes: 5 additions & 8 deletions gcloud/streaming/stream_slice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Small helper class to provide a small slice of a stream."""

from gcloud.streaming.exceptions import StreamExhausted
from six.moves import http_client


class StreamSlice(object):
Expand Down Expand Up @@ -48,26 +48,23 @@ def read(self, size=None):
unexpectedly raise an exception on read: if the underlying stream
is exhausted (i.e. returns no bytes on read), and the size of this
slice indicates we should still be able to read more bytes, we
raise :exc:`StreamExhausted`.
raise :exc:`IncompleteRead`.

:type size: integer or None
:param size: If provided, read no more than size bytes from the stream.

:rtype: bytes
:returns: bytes read from this slice.

:raises: :exc:`gcloud.streaming.exceptions.StreamExhausted`
:raises: :exc:`IncompleteRead`
"""
if size is not None:
read_size = min(size, self._remaining_bytes)
else:
read_size = self._remaining_bytes
data = self._stream.read(read_size)
if read_size > 0 and not data:
raise StreamExhausted(
'Not enough bytes in stream; expected %d, exhausted '
'after %d' % (
self._max_bytes,
self._max_bytes - self._remaining_bytes))
raise http_client.IncompleteRead(
self._max_bytes - self._remaining_bytes, self._max_bytes)
self._remaining_bytes -= len(data)
return data
6 changes: 2 additions & 4 deletions gcloud/streaming/test_buffered_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,22 @@ def test__bytes_remaining_start_zero_shorter_than_buffer(self):

def test_read_w_none(self):
from io import BytesIO
from gcloud.streaming.exceptions import NotYetImplementedError
CONTENT = b'CONTENT GOES HERE'
START = 0
BUFSIZE = 4
stream = BytesIO(CONTENT)
bufstream = self._makeOne(stream, START, BUFSIZE)
with self.assertRaises(NotYetImplementedError):
with self.assertRaises(ValueError):
bufstream.read(None)

def test_read_w_negative_size(self):
from io import BytesIO
from gcloud.streaming.exceptions import NotYetImplementedError
CONTENT = b'CONTENT GOES HERE'
START = 0
BUFSIZE = 4
stream = BytesIO(CONTENT)
bufstream = self._makeOne(stream, START, BUFSIZE)
with self.assertRaises(NotYetImplementedError):
with self.assertRaises(ValueError):
bufstream.read(-2)

def test_read_from_start(self):
Expand Down
4 changes: 2 additions & 2 deletions gcloud/streaming/test_stream_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def test___nonzero___nonempty(self):

def test_read_exhausted(self):
from io import BytesIO
from gcloud.streaming.exceptions import StreamExhausted
from six.moves import http_client
CONTENT = b''
MAXSIZE = 4
stream = BytesIO(CONTENT)
stream_slice = self._makeOne(stream, MAXSIZE)
with self.assertRaises(StreamExhausted):
with self.assertRaises(http_client.IncompleteRead):
stream_slice.read()

def test_read_implicit_size(self):
Expand Down
35 changes: 12 additions & 23 deletions gcloud/streaming/test_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,12 @@ def test_ctor_w_total_size(self):

def test_from_file_w_existing_file_no_override(self):
import os
from gcloud.streaming.exceptions import InvalidUserInputError
klass = self._getTargetClass()
with _tempdir() as tempdir:
filename = os.path.join(tempdir, 'file.out')
with open(filename, 'w') as fileobj:
fileobj.write('EXISTING FILE')
with self.assertRaises(InvalidUserInputError):
with self.assertRaises(ValueError):
klass.from_file(filename)

def test_from_file_w_existing_file_w_override_wo_auto_transfer(self):
Expand Down Expand Up @@ -815,22 +814,20 @@ def test_ctor_w_kwds(self):
self.assertEqual(upload.chunksize, CHUNK_SIZE)

def test_from_file_w_nonesuch_file(self):
from gcloud.streaming.exceptions import NotFoundError
klass = self._getTargetClass()
filename = '~nosuchuser/file.txt'
with self.assertRaises(NotFoundError):
with self.assertRaises(OSError):
klass.from_file(filename)

def test_from_file_wo_mimetype_w_unguessable_filename(self):
import os
from gcloud.streaming.exceptions import InvalidUserInputError
klass = self._getTargetClass()
CONTENT = b'EXISTING FILE W/ UNGUESSABLE MIMETYPE'
with _tempdir() as tempdir:
filename = os.path.join(tempdir, 'file.unguessable')
with open(filename, 'wb') as fileobj:
fileobj.write(CONTENT)
with self.assertRaises(InvalidUserInputError):
with self.assertRaises(ValueError):
klass.from_file(filename)

def test_from_file_wo_mimetype_w_guessable_filename(self):
Expand Down Expand Up @@ -866,10 +863,9 @@ def test_from_file_w_mimetype_w_auto_transfer_w_kwds(self):
self.assertEqual(upload.chunksize, CHUNK_SIZE)

def test_from_stream_wo_mimetype(self):
from gcloud.streaming.exceptions import InvalidUserInputError
klass = self._getTargetClass()
stream = _Stream()
with self.assertRaises(InvalidUserInputError):
with self.assertRaises(ValueError):
klass.from_stream(stream, mime_type=None)

def test_from_stream_defaults(self):
Expand Down Expand Up @@ -897,11 +893,10 @@ def test_from_stream_explicit(self):
self.assertEqual(upload.chunksize, CHUNK_SIZE)

def test_strategy_setter_invalid(self):
from gcloud.streaming.exceptions import UserError
upload = self._makeOne(_Stream())
with self.assertRaises(UserError):
with self.assertRaises(ValueError):
upload.strategy = object()
with self.assertRaises(UserError):
with self.assertRaises(ValueError):
upload.strategy = 'unknown'

def test_strategy_setter_SIMPLE_UPLOAD(self):
Expand Down Expand Up @@ -996,24 +991,22 @@ def test__set_default_strategy_w_body_w_multipart_w_simple_path(self):
self.assertEqual(upload.strategy, SIMPLE_UPLOAD)

def test_configure_request_w_total_size_gt_max_size(self):
from gcloud.streaming.exceptions import InvalidUserInputError
MAX_SIZE = 1000
config = _UploadConfig()
config.max_size = MAX_SIZE
request = _Request()
url_builder = _Dummy()
upload = self._makeOne(_Stream(), total_size=MAX_SIZE + 1)
with self.assertRaises(InvalidUserInputError):
with self.assertRaises(ValueError):
upload.configure_request(config, request, url_builder)

def test_configure_request_w_invalid_mimetype(self):
from gcloud.streaming.exceptions import InvalidUserInputError
config = _UploadConfig()
config.accept = ('text/*',)
request = _Request()
url_builder = _Dummy()
upload = self._makeOne(_Stream())
with self.assertRaises(InvalidUserInputError):
with self.assertRaises(ValueError):
upload.configure_request(config, request, url_builder)

def test_configure_request_w_simple_wo_body(self):
Expand Down Expand Up @@ -1272,10 +1265,9 @@ def test__get_range_header_w_range(self):
self.assertEqual(upload._get_range_header(response), '123')

def test_initialize_upload_no_strategy(self):
from gcloud.streaming.exceptions import UserError
request = _Request()
upload = self._makeOne(_Stream())
with self.assertRaises(UserError):
with self.assertRaises(ValueError):
upload.initialize_upload(request, http=object())

def test_initialize_upload_simple_w_http(self):
Expand Down Expand Up @@ -1373,10 +1365,9 @@ def test__validate_chunksize_wo__server_chunk_granularity(self):
upload._validate_chunksize(123) # no-op

def test__validate_chunksize_w__server_chunk_granularity_miss(self):
from gcloud.streaming.exceptions import ConfigurationValueError
upload = self._makeOne(_Stream())
upload._server_chunk_granularity = 100
with self.assertRaises(ConfigurationValueError):
with self.assertRaises(ValueError):
upload._validate_chunksize(123)

def test__validate_chunksize_w__server_chunk_granularity_hit(self):
Expand All @@ -1385,20 +1376,18 @@ def test__validate_chunksize_w__server_chunk_granularity_hit(self):
upload._validate_chunksize(400)

def test_stream_file_w_simple_strategy(self):
from gcloud.streaming.exceptions import InvalidUserInputError
from gcloud.streaming.transfer import SIMPLE_UPLOAD
upload = self._makeOne(_Stream())
upload.strategy = SIMPLE_UPLOAD
with self.assertRaises(InvalidUserInputError):
with self.assertRaises(ValueError):
upload.stream_file()

def test_stream_file_w_use_chunks_invalid_chunk_size(self):
from gcloud.streaming.exceptions import ConfigurationValueError
from gcloud.streaming.transfer import RESUMABLE_UPLOAD
upload = self._makeOne(_Stream(), chunksize=1024)
upload.strategy = RESUMABLE_UPLOAD
upload._server_chunk_granularity = 100
with self.assertRaises(ConfigurationValueError):
with self.assertRaises(ValueError):
upload.stream_file(use_chunks=True)

def test_stream_file_not_initialized(self):
Expand Down
6 changes: 2 additions & 4 deletions gcloud/streaming/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ def _callFUT(self, *args, **kw):
return acceptable_mime_type(*args, **kw)

def test_pattern_wo_slash(self):
from gcloud.streaming.exceptions import InvalidUserInputError
with self.assertRaises(InvalidUserInputError) as err:
with self.assertRaises(ValueError) as err:
self._callFUT(['text/*'], 'BOGUS')
self.assertEqual(
err.exception.args,
('Invalid MIME type: "BOGUS"',))

def test_accept_pattern_w_semicolon(self):
from gcloud.streaming.exceptions import ConfigurationValueError
with self.assertRaises(ConfigurationValueError) as err:
with self.assertRaises(ValueError) as err:
self._callFUT(['text/*;charset=utf-8'], 'text/plain')
self.assertEqual(
err.exception.args,
Expand Down
Loading