From a23ce28942c8e404e6a0cadb7dccc24567851ce3 Mon Sep 17 00:00:00 2001 From: Bernie Hackett Date: Tue, 16 Jul 2019 11:01:00 -0700 Subject: [PATCH] PYTHON-1761 Include fqdn or srv hosts in srv errors --- pymongo/srv_resolver.py | 8 ++++---- test/test_dns.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pymongo/srv_resolver.py b/pymongo/srv_resolver.py index a5e4b9bb4..5ed944d2d 100644 --- a/pymongo/srv_resolver.py +++ b/pymongo/srv_resolver.py @@ -45,10 +45,10 @@ class _SrvResolver(object): try: self.__plist = self.__fqdn.split(".")[1:] except Exception: - raise ConfigurationError("Invalid URI host") + raise ConfigurationError("Invalid URI host: %s" % (fqdn,)) self.__slen = len(self.__plist) if self.__slen < 2: - raise ConfigurationError("Invalid URI host") + raise ConfigurationError("Invalid URI host: %s" % (fqdn,)) def get_options(self): try: @@ -88,9 +88,9 @@ class _SrvResolver(object): try: nlist = node[0].split(".")[1:][-self.__slen:] except Exception: - raise ConfigurationError("Invalid SRV host") + raise ConfigurationError("Invalid SRV host: %s" % (node[0],)) if self.__plist != nlist: - raise ConfigurationError("Invalid SRV host") + raise ConfigurationError("Invalid SRV host: %s" % (node[0],)) return results, nodes diff --git a/test/test_dns.py b/test/test_dns.py index a80664442..01395c9f2 100644 --- a/test/test_dns.py +++ b/test/test_dns.py @@ -106,6 +106,18 @@ def create_tests(): create_tests() +class TestParsingErrors(unittest.TestCase): + + def test_invalid_host(self): + self.assertRaisesRegex( + ConfigurationError, + "Invalid URI host: mongodb", + MongoClient, "mongodb+srv://mongodb") + self.assertRaisesRegex( + ConfigurationError, + "Invalid URI host: mongodb.com", + MongoClient, "mongodb+srv://mongodb.com") + if __name__ == '__main__': unittest.main()