Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-2795

Improve error messages when parsing hostnames from URIs

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.12, 4.0
    • Affects Version/s: None
    • Component/s: None
    • None

      The following is first time user feedback where we should improve our error messaging:

      >>> client = pymongo.MongoClient("mongodb+srv://localhost/test?retryWrites=true&w=majority")
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/mongo_client.py", line 641, in __init__
          connect_timeout=timeout)
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/uri_parser.py", line 499, in parse_uri
          dns_resolver = _SrvResolver(fqdn, connect_timeout=connect_timeout)
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/srv_resolver.py", line 53, in __init__
          raise ConfigurationError("Invalid URI host: %s" % (fqdn,))
      pymongo.errors.ConfigurationError: Invalid URI host: localhost
      

      When the user passes a hostname that is invalid for SRV, because it doesn't have the correct number of parts, we should use an error message like "<hostname> is not a valid SRV hostname. Did you mean 'mongodb://<hostname>'?".

      >>> client = pymongo.MongoClient("mongodb+srv://127.0.0.1/test?retryWrites=true&w=majority")
      Traceback (most recent call last):
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymongo/srv_resolver.py", line 73, in _resolve_uri
          lifetime=self.__connect_timeout)
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/dns/resolver.py", line 1102, in query
          lifetime)
        File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/dns/resolver.py", line 1002, in query
          raise NXDOMAIN(qnames=qnames_to_try, responses=nxdomain_responses)
      dns.resolver.NXDOMAIN: None of DNS query names exist: _mongodb._tcp.127.0.0.1., _mongodb._tcp.127.0.0.1.home.
      

      I don't think IP addresses are valid for SRV, so we should use the "suggestion" message again. Something like "IP addresses are not valid SRV hostnames. Did you mean 'mongodb://127.0.0.1'?"

      >>> client = pymongo.MongoClient("//127.0.0.1/test?retryWrites=true&w=majority") 
      ...
      pymongo.errors.ServerSelectionTimeoutError: //127.0.0.1/test?retrywrites=true&w=majority:27017: [Errno 8] nodename nor servname provided, or not known, Timeout: 30s, Topology Description: <TopologyDescription id: 60ab77e081f7932b3ddc6c5f, topology_type: Single, servers: [<ServerDescription ('//127.0.0.1/test?retrywrites=true&w=majority', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('//127.0.0.1/test?retrywrites=true&w=majority:27017: [Errno 8] nodename nor servname provided, or not known')>]>
      

      This seems like a bug in our URI scheme parsing code. We should catch that and suggest "mongodb://" like the previous examples above.

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            bernie@mongodb.com Bernie Hackett
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: