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

Unicode errors from server are improperly encoded in exceptions

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.8
    • Affects Version/s: 3.7.2
    • Component/s: Error Handling
    • None

      This only affects Python 2.

       

      When the mongo server returns an error string containing Unicode, pymongo creates an exception, and uses str() to store the message. Since the string contains non-ascii characters, the exception cannot be rendered correctly and looks like this:

      pymongo.errors.WriteError: <exception str() failed>

      This prevents understanding what the actual error is.

      Here's an example.

      >>> conn = pymongo.MongoClient()
      >>> test_col = conn.test.test_col
      >>> test_col.create_index('a', unique=True)
      

      Exception for an ASCII value:

      >>> test_col.insert(\{'a': 'A'*10000})
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 3161, in insert
       check_keys, manipulate, write_concern)
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 607, in _insert
       bypass_doc_val, session)
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 595, in _insert_one
       acknowledged, _insert_command, session)
       File "/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1248, in _retryable_write
       return self._retry_with_session(retryable, func, s, None)
       File "/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1201, in _retry_with_session
       return func(session, sock_info, retryable)
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 592, in _insert_command
       _check_write_command_response(result)
       File "/lib/python2.7/site-packages/pymongo/helpers.py", line 217, in _check_write_command_response
       _raise_last_write_error(write_errors)
       File "/lib/python2.7/site-packages/pymongo/helpers.py", line 199, in _raise_last_write_error
       raise WriteError(error.get("errmsg"), error.get("code"), error)
      pymongo.errors.WriteError: WiredTigerIndex::insert: key too large to index, failing 10012 \{ : "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..." }
      

      Exception for a Unicode value:

      >>> test_col.insert(\{'a': u'\U0001f40d'*10000})
      Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 3161, in insert
       check_keys, manipulate, write_concern)
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 607, in _insert
       bypass_doc_val, session)
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 595, in _insert_one
       acknowledged, _insert_command, session)
       File "/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1248, in _retryable_write
       return self._retry_with_session(retryable, func, s, None)
       File "/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1201, in _retry_with_session
       return func(session, sock_info, retryable)
       File "/lib/python2.7/site-packages/pymongo/collection.py", line 592, in _insert_command
       _check_write_command_response(result)
       File "/lib/python2.7/site-packages/pymongo/helpers.py", line 217, in _check_write_command_response
       _raise_last_write_error(write_errors)
       File "/lib/python2.7/site-packages/pymongo/helpers.py", line 199, in _raise_last_write_error
       raise WriteError(error.get("errmsg"), error.get("code"), error)
      pymongo.errors.WriteError: <exception str() failed>
      

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            salty-horse Ori Avtalion
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: