diff --git a/src/momento/_cache_service_errors_converter.py b/src/momento/_cache_service_errors_converter.py index f4dafb00..d4d82535 100644 --- a/src/momento/_cache_service_errors_converter.py +++ b/src/momento/_cache_service_errors_converter.py @@ -3,10 +3,22 @@ from . import _momento_logger __rpc_to_error = { - grpc.StatusCode.ALREADY_EXISTS: errors.CacheExistsError, - grpc.StatusCode.INVALID_ARGUMENT: errors.CacheValueError, - grpc.StatusCode.NOT_FOUND: errors.CacheNotFoundError, + grpc.StatusCode.INVALID_ARGUMENT: errors.BadRequestError, + grpc.StatusCode.OUT_OF_RANGE: errors.BadRequestError, + grpc.StatusCode.UNIMPLEMENTED: errors.BadRequestError, + grpc.StatusCode.FAILED_PRECONDITION: errors.BadRequestError, + grpc.StatusCode.CANCELLED: errors.CancelledError, + grpc.StatusCode.DEADLINE_EXCEEDED: errors.TimeoutError, grpc.StatusCode.PERMISSION_DENIED: errors.PermissionError, + grpc.StatusCode.UNAUTHENTICATED: errors.AuthenticationError, + grpc.StatusCode.RESOURCE_EXHAUSTED: errors.LimitExceededError, + grpc.StatusCode.ALREADY_EXISTS: errors.AlreadyExistsError, + grpc.StatusCode.NOT_FOUND: errors.NotFoundError, + grpc.StatusCode.UNKNOWN: errors.InternalServerError, + grpc.StatusCode.ABORTED: errors.InternalServerError, + grpc.StatusCode.INTERNAL: errors.InternalServerError, + grpc.StatusCode.UNAVAILABLE: errors.InternalServerError, + grpc.StatusCode.DATA_LOSS: errors.InternalServerError, } diff --git a/src/momento/_momento_endpoint_resolver.py b/src/momento/_momento_endpoint_resolver.py index 81cb4e73..c0e629e1 100644 --- a/src/momento/_momento_endpoint_resolver.py +++ b/src/momento/_momento_endpoint_resolver.py @@ -28,4 +28,4 @@ def _getEndpointFromToken(auth_token): return _Endpoints(claims[_CONTROL_ENDPOINT_CLAIM_ID], claims[_CACHE_ENDPOINT_CLAIM_ID]) except (DecodeError, KeyError): - raise errors.InvalidInputError('Invalid Auth token.') from None + raise errors.InvalidArgumentError('Invalid Auth token.') from None diff --git a/src/momento/_scs_control_client.py b/src/momento/_scs_control_client.py index 51c29ff4..6186a743 100644 --- a/src/momento/_scs_control_client.py +++ b/src/momento/_scs_control_client.py @@ -11,6 +11,8 @@ from . import _momento_logger from . import _scs_grpc_manager +from ._utilities._data_validation import _validate_cache_name + class _ScsControlClient: """Momento Internal.""" @@ -56,8 +58,3 @@ def _getStub(self): def close(self): self._grpc_manager.close() - - -def _validate_cache_name(cache_name): - if (cache_name is None): - raise errors.InvalidInputError('Cache Name cannot be None') diff --git a/src/momento/_scs_data_client.py b/src/momento/_scs_data_client.py index d3210f81..b38c225e 100644 --- a/src/momento/_scs_data_client.py +++ b/src/momento/_scs_data_client.py @@ -6,6 +6,8 @@ from . import _momento_logger from . import _scs_grpc_manager +from ._utilities._data_validation import _as_bytes, _validate_ttl, _make_metadata, _validate_cache_name + class _ScsDataClient: """Internal""" @@ -22,8 +24,8 @@ def set(self, cache_name, key, value, ttl_seconds): item_ttl_seconds = self._default_ttlSeconds if ttl_seconds is None else ttl_seconds _validate_ttl(item_ttl_seconds) set_request = cache_client_types.SetRequest() - set_request.cache_key = _asBytes(key, 'Unsupported type for key: ') - set_request.cache_body = _asBytes(value, + set_request.cache_key = _as_bytes(key, 'Unsupported type for key: ') + set_request.cache_body = _as_bytes(value, 'Unsupported type for value: ') set_request.ttl_milliseconds = item_ttl_seconds * 1000 response = self._getStub().Set(set_request, @@ -40,7 +42,7 @@ def get(self, cache_name, key): try: _momento_logger.debug(f'Issuing a get request with key {key}') get_request = cache_client_types.GetRequest() - get_request.cache_key = _asBytes(key, 'Unsupported type for key: ') + get_request.cache_key = _as_bytes(key, 'Unsupported type for key: ') response = self._getStub().Get(get_request, metadata=_make_metadata(cache_name)) _momento_logger.debug(f'Received a get response for {key}') @@ -54,26 +56,3 @@ def _getStub(self): def close(self): self._grpc_manager.close() - - -def _make_metadata(cache_name): - return (('cache', cache_name), ) - - -def _validate_cache_name(cache_name): - if (cache_name is None): - raise errors.InvalidInputError('Cache Name cannot be None') - - -def _asBytes(data, errorMessage): - if (isinstance(data, str)): - return data.encode('utf-8') - if (isinstance(data, bytes)): - return data - raise errors.InvalidInputError(errorMessage + str(type(data))) - - -def _validate_ttl(ttl_seconds): - if (not isinstance(ttl_seconds, int) or ttl_seconds < 0): - raise errors.InvalidInputError( - 'TTL Seconds must be a non-negative integer') diff --git a/src/momento/_utilities/_data_validation.py b/src/momento/_utilities/_data_validation.py index 61dc1c42..33e471e0 100644 --- a/src/momento/_utilities/_data_validation.py +++ b/src/momento/_utilities/_data_validation.py @@ -8,8 +8,8 @@ def _make_metadata(cache_name) -> Metadata: def _validate_cache_name(cache_name): - if cache_name is None: - raise errors.InvalidInputError('Cache Name cannot be None') + if cache_name is None or not isinstance(cache_name, str): + raise errors.InvalidArgumentError('Cache name must be a non-empty string') def _as_bytes(data, error_message): @@ -17,10 +17,10 @@ def _as_bytes(data, error_message): return data.encode('utf-8') if isinstance(data, bytes): return data - raise errors.InvalidInputError(error_message + str(type(data))) + raise errors.InvalidArgumentError(error_message + str(type(data))) def _validate_ttl(ttl_seconds): if not isinstance(ttl_seconds, int) or ttl_seconds < 0: - raise errors.InvalidInputError( + raise errors.InvalidArgumentError( 'TTL Seconds must be a non-negative integer') diff --git a/src/momento/aio/simple_cache_client.py b/src/momento/aio/simple_cache_client.py index 37a9d247..323497c3 100644 --- a/src/momento/aio/simple_cache_client.py +++ b/src/momento/aio/simple_cache_client.py @@ -33,11 +33,11 @@ async def create_cache(self, cache_name) -> CreateCacheResponse: CreateCacheResponse Raises: - InvalidInputError: If cache name is None. + InvalidArgumentError: If provided cache_name None. + BadRequestError: If the cache name provided doesn't follow the naming conventions + ExistsError: If cache with the given name already exists. + AuthenticationError: If the provided Momento Auth Token is invalid. ClientSdkError: For any SDK checks that fail. - CacheValueError: If provided cache_name is empty. - CacheExistsError: If cache with the given name already exists. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. """ return await self._control_client.create_cache(cache_name) @@ -51,11 +51,11 @@ async def delete_cache(self, cache_name) -> DeleteCacheResponse: DeleteCacheResponse Raises: - CacheNotFoundError: If an attempt is made to delete a MomentoCache that doesn't exits. - InvalidInputError: If cache name is None. + InvalidArgumentError: If provided cache_name is None. + BadRequestError: If the cache name provided doesn't follow the naming conventions + NotFoundError: If an attempt is made to delete a MomentoCache that doesn't exits. + AuthenticationError: If the provided Momento Auth Token is invalid. ClientSdkError: For any SDK checks that fail. - CacheValueError: If provided cache name is empty - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. """ return await self._control_client.delete_cache(cache_name) @@ -69,8 +69,7 @@ async def list_caches(self, next_token=None) -> ListCachesResponse: ListCachesResponse Raises: - Exception to notify either sdk, grpc, or operation error. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. + AuthenticationError: If the provided Momento Auth Token is invalid. """ return await self._control_client.list_caches(next_token) @@ -87,10 +86,10 @@ async def set(self, cache_name, key, value, ttl_seconds=None) -> CacheSetRespons CacheSetResponse Raises: - InvalidInputError: If service validation fails for provided values. - ClientSdkError: If cache name is invalid type. - CacheNotFoundError: If an attempt is made to store an item in a cache that doesn't exist. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. + InvalidArgumentError: If validation fails for provided method arguments. + BadRequestError: If the provided inputs are rejected by server because they are invalid + NotFoundError: If the cache with the given name doesn't exist. + AuthenticationError: If the provided Momento Auth Token is invalid. InternalServerError: If server encountered an unknown error while trying to store the item. """ return await self._data_client.set(cache_name, key, value, ttl_seconds) @@ -106,10 +105,10 @@ async def get(self, cache_name, key) -> CacheGetResponse: CacheGetResponse Raises: - InvalidInputError: If service validation fails for provided values. - ClientSdkError: If cache name is invalid type. - CacheNotFoundError: If an attempt is made to retrieve an item in a cache that doesn't exist. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. + InvalidArgumentError: If validation fails for provided method arguments. + BadRequestError: If the provided inputs are rejected by server because they are invalid + NotFoundError: If the cache with the given name doesn't exist. + AuthenticationError: If the provided Momento Auth Token is invalid. InternalServerError: If server encountered an unknown error while trying to retrieve the item. """ return await self._data_client.get(cache_name, key) @@ -124,7 +123,6 @@ def init(auth_token, item_default_ttl_seconds) -> SimpleCacheClient: Returns: SimpleCacheClient Raises: - InvalidInputError: If service validation fails for provided values - InternalServerError: If server encountered an unknown error. + IllegalArgumentError: If the provided auth token and/or item_default_ttl_seconds is invalid """ return SimpleCacheClient(auth_token, item_default_ttl_seconds) diff --git a/src/momento/errors.py b/src/momento/errors.py index 32bab896..4f5fe8e6 100644 --- a/src/momento/errors.py +++ b/src/momento/errors.py @@ -15,7 +15,7 @@ def __init__(self, message): super().__init__(message) -class InvalidInputError(ClientSdkError): +class InvalidArgumentError(ClientSdkError): """Error raised when provided input values to the SDK are invalid Some examples - missing required parameters, incorrect parameter @@ -37,30 +37,54 @@ def __init__(self, message): super().__init__(message) -class CacheNotFoundError(CacheServiceError): +class NotFoundError(CacheServiceError): """Error raised for operations performed on non-existent cache""" def __init__(self, message): super().__init__(message) -class CacheExistsError(CacheServiceError): +class AlreadyExistsError(CacheServiceError): """Error raised when attempting to create a cache with same name""" def __init__(self, message): super().__init__(message) -class CacheValueError(CacheServiceError): +class BadRequestError(CacheServiceError): """Error raised when service validation fails for provided values""" def __init__(self, message): super().__init__(message) class PermissionError(CacheServiceError): + """Error for insufficient permissions to perform an operation with Cache Service.""" + def __init__(self, message): + super().__init__(message) + + +class AuthenticationError(CacheServiceError): """Error when authentication with Cache Service fails""" def __init__(self, message): super().__init__(message) +class CancelledError(CacheServiceError): + """Error when an operation with Cache Service was cancelled""" + def __init__(self, message): + super().__init__(message) + + +class TimeoutError(CacheServiceError): + """Error when an operation did not complete in time""" + def __init__(self, message): + super().__init__(message) + + +class LimitExceededError(CacheServiceError): + """Error when calls are throttled due to request limit rate""" + def __init__(self, message): + super().__init__(message) + + class InternalServerError(CacheServiceError): """Operation failed on the server with an unknown error""" def __init__(self, message): diff --git a/src/momento/simple_cache_client.py b/src/momento/simple_cache_client.py index 9f85597e..a4598fa0 100644 --- a/src/momento/simple_cache_client.py +++ b/src/momento/simple_cache_client.py @@ -31,11 +31,11 @@ def create_cache(self, cache_name) -> CreateCacheResponse: CreateCacheResponse Raises: - InvalidInputError: If cache name is None. + InvalidArgumentError: If provided cache_name is None. + BadRequestError: If the cache name provided doesn't follow the naming conventions + AlreadyExistsError: If cache with the given name already exists. + AuthenticationError: If the provided Momento Auth Token is invalid. ClientSdkError: For any SDK checks that fail. - CacheValueError: If provided cache_name is empty. - CacheExistsError: If cache with the given name already exists. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. """ return self._control_client.create_cache(cache_name) @@ -49,11 +49,11 @@ def delete_cache(self, cache_name) -> DeleteCacheResponse: DeleteCacheResponse Raises: - CacheNotFoundError: If an attempt is made to delete a MomentoCache that doesn't exits. - InvalidInputError: If cache name is None. + InvalidArgumentError: If provided cache_name is None. + BadRequestError: If the cache name provided doesn't follow the naming conventions + NotFoundError: If an attempt is made to delete a MomentoCache that doesn't exits. + AuthenticationError: If the provided Momento Auth Token is invalid. ClientSdkError: For any SDK checks that fail. - CacheValueError: If provided cache name is empty - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. """ return self._control_client.delete_cache(cache_name) @@ -67,8 +67,7 @@ def list_caches(self, next_token=None) -> ListCachesResponse: ListCachesResponse Raises: - Exception to notify either sdk, grpc, or operation error. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. + AuthenticationError: If the provided Momento Auth Token is invalid. """ return self._control_client.list_caches(next_token) @@ -85,10 +84,10 @@ def set(self, cache_name, key, value, ttl_seconds=None) -> CacheSetResponse: CacheSetResponse Raises: - InvalidInputError: If service validation fails for provided values. - ClientSdkError: If cache name is invalid type. - CacheNotFoundError: If an attempt is made to store an item in a cache that doesn't exist. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. + InvalidArgumentError: If validation fails for the provided method arguments. + BadRequestError: If the provided inputs are rejected by server because they are invalid + NotFoundError: If the cache with the given name doesn't exist. + AuthenticationError: If the provided Momento Auth Token is invalid. InternalServerError: If server encountered an unknown error while trying to store the item. """ return self._data_client.set(cache_name, key, value, ttl_seconds) @@ -104,10 +103,10 @@ def get(self, cache_name, key) -> CacheGetResponse: CacheGetResponse Raises: - InvalidInputError: If service validation fails for provided values. - ClientSdkError: If cache name is invalid type. - CacheNotFoundError: If an attempt is made to retrieve an item in a cache that doesn't exist. - PermissionError: If the provided Momento Auth Token is invalid to perform the requested operation. + InvalidArgumentError: If validation fails for the provided method arguments. + BadRequestError: If the provided inputs are rejected by server because they are invalid + NotFoundError: If the cache with the given name doesn't exist. + AuthenticationError: If the provided Momento Auth Token is invalid. InternalServerError: If server encountered an unknown error while trying to retrieve the item. """ return self._data_client.get(cache_name, key) @@ -122,7 +121,6 @@ def init(auth_token, item_default_ttl_seconds) -> SimpleCacheClient: Returns: SimpleCacheClient Raises: - InvalidInputError: If service validation fails for provided values - InternalServerError: If server encountered an unknown error. + IllegalArgumentError: If the provided auth token and/or item_default_ttl_seconds is invalid """ return SimpleCacheClient(auth_token, item_default_ttl_seconds) diff --git a/tests/test_momento.py b/tests/test_momento.py index 1f4c70ad..bd0ac54b 100644 --- a/tests/test_momento.py +++ b/tests/test_momento.py @@ -31,7 +31,7 @@ def setUpClass(cls): # ensure test cache exists try: cls.client.create_cache(_TEST_CACHE_NAME) - except errors.CacheExistsError: + except errors.AlreadyExistsError: # do nothing, cache already exists pass @@ -64,35 +64,34 @@ def test_create_cache_get_set_values_and_delete_cache(self): # init def test_init_throws_exception_when_client_uses_negative_default_ttl(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: simple_cache_client.init(_AUTH_TOKEN, -1) self.assertEqual('{}'.format(cm.exception), "TTL Seconds must be a non-negative integer") def test_init_throws_exception_for_non_jwt_token(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: simple_cache_client.init("notanauthtoken", _DEFAULT_TTL_SECONDS) self.assertEqual('{}'.format(cm.exception), "Invalid Auth token.") # create_cache def test_create_cache_throws_already_exists_when_creating_existing_cache(self): - with self.assertRaises(errors.CacheExistsError): + with self.assertRaises(errors.AlreadyExistsError): self.client.create_cache(_TEST_CACHE_NAME) def test_create_cache_throws_exception_for_empty_cache_name(self): - with self.assertRaises(errors.CacheValueError): + with self.assertRaises(errors.BadRequestError): self.client.create_cache("") def test_create_cache_throws_validation_exception_for_null_cache_name(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.create_cache(None) - self.assertEqual('{}'.format(cm.exception), "Cache Name cannot be None") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") def test_create_cache_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.create_cache(1) - self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: 1 has type int, but expected one of: bytes, unicode") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") def test_create_cache_throws_permission_exception_for_bad_token(self): with simple_cache_client.init(_BAD_AUTH_TOKEN, @@ -105,30 +104,29 @@ def test_delete_cache_succeeds(self): cache_name = str(uuid.uuid4()) self.client.create_cache(cache_name) - with self.assertRaises(errors.CacheExistsError): + with self.assertRaises(errors.AlreadyExistsError): self.client.create_cache(cache_name) self.client.delete_cache(cache_name) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): self.client.delete_cache(cache_name) def test_delete_cache_throws_not_found_when_deleting_unknown_cache(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): self.client.delete_cache(cache_name) def test_delete_cache_throws_invalid_input_for_null_cache_name(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): self.client.delete_cache(None) def test_delete_cache_throws_exception_for_empty_cache_name(self): - with self.assertRaises(errors.CacheValueError): + with self.assertRaises(errors.BadRequestError): self.client.delete_cache("") def test_delete_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.delete_cache(1) - self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: 1 has type int, but expected one of: bytes, unicode") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") def test_delete_cache_throws_permission_exception_for_bad_token(self): with simple_cache_client.init(_BAD_AUTH_TOKEN, @@ -231,47 +229,46 @@ def test_set_with_different_ttl(self): def test_set_with_non_existent_cache_name_throws_not_found(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): self.client.set(cache_name, "foo", "bar") def test_set_with_null_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.set(None, "foo", "bar") - self.assertEqual('{}'.format(cm.exception), "Cache Name cannot be None") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") def test_set_with_empty_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheValueError) as cm: + with self.assertRaises(errors.BadRequestError) as cm: self.client.set("", "foo", "bar") self.assertEqual('{}'.format(cm.exception), "Cache header is empty") def test_set_with_null_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): self.client.set(_TEST_CACHE_NAME, None, "bar") def test_set_with_null_value_throws_exception(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): self.client.set(_TEST_CACHE_NAME, "foo", None) def test_set_negative_ttl_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.set(_TEST_CACHE_NAME, "foo", "bar", -1) self.assertEqual('{}'.format(cm.exception), "TTL Seconds must be a non-negative integer") def test_set_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.set(1, "foo", "bar") - self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: Expected str, not ") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") def test_set_with_bad_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.set(_TEST_CACHE_NAME, 1, "bar") self.assertEqual('{}'.format(cm.exception), "Unsupported type for key: ") def test_set_with_bad_value_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.set(_TEST_CACHE_NAME, "foo", 1) self.assertEqual('{}'.format(cm.exception), "Unsupported type for value: ") @@ -285,33 +282,32 @@ def test_set_throws_permission_exception_for_bad_token(self): def test_get_with_non_existent_cache_name_throws_not_found(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): self.client.get(cache_name, "foo") def test_get_with_null_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.get(None, "foo") - self.assertEqual('{}'.format(cm.exception), "Cache Name cannot be None") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") def test_get_with_empty_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheValueError) as cm: + with self.assertRaises(errors.BadRequestError) as cm: self.client.get("", "foo") self.assertEqual('{}'.format(cm.exception), "Cache header is empty") def test_get_with_null_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): self.client.get(_TEST_CACHE_NAME, None) def test_get_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.get(1, "foo") - self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: Expected str, not ") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") def test_get_with_bad_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: self.client.get(_TEST_CACHE_NAME, 1) self.assertEqual('{}'.format(cm.exception), "Unsupported type for key: ") diff --git a/tests/test_momento_async.py b/tests/test_momento_async.py index 3c4ef241..de934c33 100644 --- a/tests/test_momento_async.py +++ b/tests/test_momento_async.py @@ -31,7 +31,7 @@ async def asyncSetUp(self) -> None: # ensure test cache exists try: await self.client.create_cache(_TEST_CACHE_NAME) - except errors.CacheExistsError: + except errors.AlreadyExistsError: # do nothing, cache already exists pass @@ -65,35 +65,35 @@ async def test_create_cache_get_set_values_and_delete_cache(self): # init async def test_init_throws_exception_when_client_uses_negative_default_ttl(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: simple_cache_client.init(_AUTH_TOKEN, -1) self.assertEqual('{}'.format(cm.exception), "TTL Seconds must be a non-negative integer") async def test_init_throws_exception_for_non_jwt_token(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: simple_cache_client.init("notanauthtoken", _DEFAULT_TTL_SECONDS) self.assertEqual('{}'.format(cm.exception), "Invalid Auth token.") # create_cache async def test_create_cache_throws_already_exists_when_creating_existing_cache(self): - with self.assertRaises(errors.CacheExistsError): + with self.assertRaises(errors.AlreadyExistsError): await self.client.create_cache(_TEST_CACHE_NAME) async def test_create_cache_throws_exception_for_empty_cache_name(self): - with self.assertRaises(errors.CacheValueError): + with self.assertRaises(errors.BadRequestError): await self.client.create_cache("") async def test_create_cache_throws_validation_exception_for_null_cache_name(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.create_cache(None) - self.assertEqual('{}'.format(cm.exception), "Cache Name cannot be None") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") async def test_create_cache_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.create_cache(1) self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: 1 has type int, but expected one of: bytes, unicode") + "Cache name must be a non-empty string") async def test_create_cache_throws_permission_exception_for_bad_token(self): async with simple_cache_client.init(_BAD_AUTH_TOKEN, _DEFAULT_TTL_SECONDS) as simple_cache: @@ -105,30 +105,29 @@ async def test_delete_cache_succeeds(self): cache_name = str(uuid.uuid4()) await self.client.create_cache(cache_name) - with self.assertRaises(errors.CacheExistsError): + with self.assertRaises(errors.AlreadyExistsError): await self.client.create_cache(cache_name) await self.client.delete_cache(cache_name) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): await self.client.delete_cache(cache_name) async def test_delete_cache_throws_not_found_when_deleting_unknown_cache(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): await self.client.delete_cache(cache_name) async def test_delete_cache_throws_invalid_input_for_null_cache_name(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): await self.client.delete_cache(None) async def test_delete_cache_throws_exception_for_empty_cache_name(self): - with self.assertRaises(errors.CacheValueError): + with self.assertRaises(errors.BadRequestError): await self.client.delete_cache("") async def test_delete_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.delete_cache(1) - self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: 1 has type int, but expected one of: bytes, unicode") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") async def test_delete_cache_throws_permission_exception_for_bad_token(self): async with simple_cache_client.init(_BAD_AUTH_TOKEN, _DEFAULT_TTL_SECONDS) as simple_cache: @@ -228,47 +227,46 @@ async def test_set_with_different_ttl(self): async def test_set_with_non_existent_cache_name_throws_not_found(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): await self.client.set(cache_name, "foo", "bar") async def test_set_with_null_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.set(None, "foo", "bar") - self.assertEqual('{}'.format(cm.exception), "Cache Name cannot be None") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") async def test_set_with_empty_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheValueError) as cm: + with self.assertRaises(errors.BadRequestError) as cm: await self.client.set("", "foo", "bar") self.assertEqual('{}'.format(cm.exception), "Cache header is empty") async def test_set_with_null_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): await self.client.set(_TEST_CACHE_NAME, None, "bar") async def test_set_with_null_value_throws_exception(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): await self.client.set(_TEST_CACHE_NAME, "foo", None) async def test_set_negative_ttl_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.set(_TEST_CACHE_NAME, "foo", "bar", -1) self.assertEqual('{}'.format(cm.exception), "TTL Seconds must be a non-negative integer") async def test_set_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.set(1, "foo", "bar") - self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: Expected str, not ") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") async def test_set_with_bad_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.set(_TEST_CACHE_NAME, 1, "bar") self.assertEqual('{}'.format(cm.exception), "Unsupported type for key: ") async def test_set_with_bad_value_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.set(_TEST_CACHE_NAME, "foo", 1) self.assertEqual('{}'.format(cm.exception), "Unsupported type for value: ") @@ -281,33 +279,32 @@ async def test_set_throws_permission_exception_for_bad_token(self): async def test_get_with_non_existent_cache_name_throws_not_found(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheNotFoundError): + with self.assertRaises(errors.NotFoundError): await self.client.get(cache_name, "foo") async def test_get_with_null_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.get(None, "foo") - self.assertEqual('{}'.format(cm.exception), "Cache Name cannot be None") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") async def test_get_with_empty_cache_name_throws_exception(self): cache_name = str(uuid.uuid4()) - with self.assertRaises(errors.CacheValueError) as cm: + with self.assertRaises(errors.BadRequestError) as cm: await self.client.get("", "foo") self.assertEqual('{}'.format(cm.exception), "Cache header is empty") async def test_get_with_null_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError): + with self.assertRaises(errors.InvalidArgumentError): await self.client.get(_TEST_CACHE_NAME, None) async def test_get_with_bad_cache_name_throws_exception(self): - with self.assertRaises(errors.ClientSdkError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.get(1, "foo") - self.assertEqual('{}'.format(cm.exception), - "Operation failed with error: Expected str, not ") + self.assertEqual('{}'.format(cm.exception), "Cache name must be a non-empty string") async def test_get_with_bad_key_throws_exception(self): - with self.assertRaises(errors.InvalidInputError) as cm: + with self.assertRaises(errors.InvalidArgumentError) as cm: await self.client.get(_TEST_CACHE_NAME, 1) self.assertEqual('{}'.format(cm.exception), "Unsupported type for key: ")