diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index ca1427a75ab2..3e2a871b735d 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -6,6 +6,7 @@ ### Bug fixes - `AzureKeyCredentialPolicy` will now accept (and ignore) passed in kwargs #11963 +- Better error messages if passed endpoint is incorrect #12106 ## 1.6.0 (2020-06-03) diff --git a/sdk/core/azure-core/azure/core/pipeline/transport/_base.py b/sdk/core/azure-core/azure/core/pipeline/transport/_base.py index d6e14b4f6876..f7e83aed4460 100644 --- a/sdk/core/azure-core/azure/core/pipeline/transport/_base.py +++ b/sdk/core/azure-core/azure/core/pipeline/transport/_base.py @@ -112,6 +112,18 @@ def _case_insensitive_dict(*args, **kwargs): def _format_url_section(template, **kwargs): + """String format the template with the kwargs, auto-skip sections of the template that are NOT in the kwargs. + + By default in Python, "format" will raise a KeyError if a template element is not found. Here the section between + the slashes will be removed from the template instead. + + This is used for API like Storage, where when Swagger has template section not defined as parameter. + + :param str template: a string template to fill + :param dict[str,str] kwargs: Template values as string + :rtype: str + :returns: Template completed + """ components = template.split("/") while components: try: @@ -718,7 +730,12 @@ def format_url(self, url_template, **kwargs): parsed = urlparse(url) if not parsed.scheme or not parsed.netloc: url = url.lstrip("/") - base = self._base_url.format(**kwargs).rstrip("/") + try: + base = self._base_url.format(**kwargs).rstrip("/") + except KeyError as key: + err_msg = "The value provided for the url part {} was incorrect, and resulted in an invalid url" + raise ValueError(err_msg.format(key.args[0])) + url = _urljoin(base, url) else: url = self._base_url.format(**kwargs) diff --git a/sdk/core/azure-core/tests/test_pipeline.py b/sdk/core/azure-core/tests/test_pipeline.py index c2fa56b0379e..bbbd37c09f5e 100644 --- a/sdk/core/azure-core/tests/test_pipeline.py +++ b/sdk/core/azure-core/tests/test_pipeline.py @@ -204,6 +204,12 @@ def test_format_url_no_base_url(self): formatted = client.format_url("https://google.com/subpath/{foo}", foo="bar") assert formatted == "https://google.com/subpath/bar" + def test_format_incorrect_endpoint(self): + # https://github.com/Azure/azure-sdk-for-python/pull/12106 + client = PipelineClientBase('{Endpoint}/text/analytics/v3.0') + with pytest.raises(ValueError) as exp: + client.format_url("foo/bar") + assert str(exp.value) == "The value provided for the url part Endpoint was incorrect, and resulted in an invalid url" class TestClientRequest(unittest.TestCase): def test_request_json(self):