From ad2142b72da862829c18ca981cddb581c189e6c2 Mon Sep 17 00:00:00 2001 From: KieranBrantnerMagee Date: Mon, 21 Sep 2020 16:00:32 -0700 Subject: [PATCH] [ServiceBus] mode (ReceiveMode) parameter needs better exception behavior (#13531) * mode parameter needs better exception behavior, as if it is mis-typed now it will return an AttributeError further down the stack without useful guidance (and only does so when creating the handler, as well). Will now return a TypeError at initialization. * Add note of AttributeError->TypeError behavior for receive_mode misalignment to changelog. --- sdk/servicebus/azure-servicebus/CHANGELOG.md | 2 ++ .../azure/servicebus/_common/receiver_mixins.py | 4 ++++ sdk/servicebus/azure-servicebus/tests/test_queues.py | 11 ++++++----- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/sdk/servicebus/azure-servicebus/CHANGELOG.md b/sdk/servicebus/azure-servicebus/CHANGELOG.md index 5e9f5001b5e1..a3415ad1f9da 100644 --- a/sdk/servicebus/azure-servicebus/CHANGELOG.md +++ b/sdk/servicebus/azure-servicebus/CHANGELOG.md @@ -2,6 +2,8 @@ ## 7.0.0b7 (Unreleased) +**Breaking Changes** +* Passing any type other than `ReceiveMode` as parameter `receive_mode` now throws a `TypeError` instead of `AttributeError`. ## 7.0.0b6 (2020-09-10) diff --git a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/receiver_mixins.py b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/receiver_mixins.py index 5987bf449b3b..8c9c9ff49e80 100644 --- a/sdk/servicebus/azure-servicebus/azure/servicebus/_common/receiver_mixins.py +++ b/sdk/servicebus/azure-servicebus/azure/servicebus/_common/receiver_mixins.py @@ -33,6 +33,10 @@ def _populate_attributes(self, **kwargs): self._auth_uri = "sb://{}/{}".format(self.fully_qualified_namespace, self.entity_path) self._entity_uri = "amqps://{}/{}".format(self.fully_qualified_namespace, self.entity_path) self._receive_mode = kwargs.get("receive_mode", ReceiveMode.PeekLock) + # While we try to leave failures to the service, in this case the errors lower down the stack are less clear. + if not isinstance(self._receive_mode, ReceiveMode): + raise TypeError("Parameter 'receive_mode' must be of type ReceiveMode") + self._error_policy = _ServiceBusErrorPolicy( max_retries=self._config.retry_total ) diff --git a/sdk/servicebus/azure-servicebus/tests/test_queues.py b/sdk/servicebus/azure-servicebus/tests/test_queues.py index 452490f0af45..c84909654b64 100644 --- a/sdk/servicebus/azure-servicebus/tests/test_queues.py +++ b/sdk/servicebus/azure-servicebus/tests/test_queues.py @@ -1857,11 +1857,12 @@ def test_queue_receiver_invalid_mode(self, servicebus_namespace_connection_strin with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, logging_enable=False) as sb_client: -# with pytest.raises(StopIteration): - with sb_client.get_queue_receiver(servicebus_queue.name, - max_wait_time="oij") as receiver: - - assert receiver + with pytest.raises(TypeError): + with sb_client.get_queue_receiver(servicebus_queue.name, + receive_mode=2) as receiver: + + raise Exception("Should not get here, should fail fast.") + @pytest.mark.liveTest @pytest.mark.live_test_only