File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 14
14
15
15
"""Exceptions raised by PyMongo."""
16
16
17
+ import sys
18
+
17
19
from bson .errors import *
18
20
19
21
try :
@@ -26,6 +28,7 @@ class PyMongoError(Exception):
26
28
"""Base class for all PyMongo exceptions."""
27
29
def __init__ (self , message = '' , error_labels = None ):
28
30
super (PyMongoError , self ).__init__ (message )
31
+ self ._message = message
29
32
self ._error_labels = set (error_labels or [])
30
33
31
34
def has_error_label (self , label ):
@@ -43,6 +46,11 @@ def _remove_error_label(self, label):
43
46
"""Remove the given label from this error."""
44
47
self ._error_labels .remove (label )
45
48
49
+ def __str__ (self ):
50
+ if sys .version_info [0 ] == 2 and isinstance (self ._message , unicode ):
51
+ return self ._message .encode ('utf-8' , errors = 'replace' )
52
+ return str (self ._message )
53
+
46
54
47
55
class ProtocolError (PyMongoError ):
48
56
"""Raised for failures related to the wire protocol."""
Original file line number Diff line number Diff line change @@ -1316,6 +1316,21 @@ def test_write_error_text_handling(self):
1316
1316
db .test .insert_many ,
1317
1317
[{"text" : text }])
1318
1318
1319
+ def test_write_error_unicode (self ):
1320
+ coll = self .db .test
1321
+ self .addCleanup (coll .drop )
1322
+
1323
+ coll .create_index ('a' , unique = True )
1324
+ coll .insert_one ({'a' : u'unicode \U0001f40d ' })
1325
+ with self .assertRaisesRegex (
1326
+ DuplicateKeyError ,
1327
+ 'E11000 duplicate key error' ) as ctx :
1328
+ coll .insert_one ({'a' : u'unicode \U0001f40d ' })
1329
+
1330
+ # Once more for good measure.
1331
+ self .assertIn ('E11000 duplicate key error' ,
1332
+ str (ctx .exception ))
1333
+
1319
1334
def test_wtimeout (self ):
1320
1335
# Ensure setting wtimeout doesn't disable write concern altogether.
1321
1336
# See SERVER-12596.
You can’t perform that action at this time.
0 commit comments