12
12
from . import errors
13
13
from .dependencies import Dependencies
14
14
15
- # client errors to catch
16
- client_errors = (client .err .InterfaceError , client .err .DatabaseError )
15
+
16
+
17
+ logger = logging .getLogger (__name__ )
18
+ query_log_max_length = 300
17
19
18
20
19
21
def translate_query_error (client_error , query ):
@@ -23,39 +25,39 @@ def translate_query_error(client_error, query):
23
25
:param query: sql query with placeholders
24
26
:return: an instance of the corresponding subclass of datajoint.errors.DataJointError
25
27
"""
28
+ logger .debug ('type: {}, args: {}' .format (type (client_error ), client_error .args ))
29
+
30
+ err , * args = client_error .args
31
+
26
32
# Loss of connection errors
27
- if isinstance (client_error , client .err .InterfaceError ) and client_error .args [0 ] == "(0, '')" :
28
- return errors .LostConnectionError ('Server connection lost due to an interface error.' , * client_error .args [1 :])
29
- disconnect_codes = {
30
- 2006 : "Connection timed out" ,
31
- 2013 : "Server connection lost" }
32
- if isinstance (client_error , client .err .OperationalError ) and client_error .args [0 ] in disconnect_codes :
33
- return errors .LostConnectionError (disconnect_codes [client_error .args [0 ]], * client_error .args [1 :])
33
+ if err in (0 , "(0, '')" ):
34
+ return errors .LostConnectionError ('Server connection lost due to an interface error.' , * args )
35
+ if err == 2006 :
36
+ return errors .LostConnectionError ("Connection timed out" , * args )
37
+ if err == 2013 :
38
+ return errors .LostConnectionError ("Server connection lost" , * args )
34
39
# Access errors
35
- if isinstance ( client_error , client . err . OperationalError ) and client_error . args [ 0 ] in (1044 , 1142 ):
36
- return errors .AccessError ('Insufficient privileges.' , client_error . args [1 ], query )
40
+ if err in (1044 , 1142 ):
41
+ return errors .AccessError ('Insufficient privileges.' , args [0 ], query )
37
42
# Integrity errors
38
- if isinstance ( client_error , client . err . IntegrityError ) and client_error . args [ 0 ] == 1062 :
39
- return errors .DuplicateError (* client_error . args [ 1 :] )
40
- if isinstance ( client_error , client . err . IntegrityError ) and client_error . args [ 0 ] == 1452 :
41
- return errors .IntegrityError (* client_error . args [ 1 :] )
43
+ if err == 1062 :
44
+ return errors .DuplicateError (* args )
45
+ if err == 1452 :
46
+ return errors .IntegrityError (* args )
42
47
# Syntax errors
43
- if isinstance ( client_error , client . err . ProgrammingError ) and client_error . args [ 0 ] == 1064 :
44
- return errors .QuerySyntaxError (client_error . args [1 ], query )
48
+ if err == 1064 :
49
+ return errors .QuerySyntaxError (args [0 ], query )
45
50
# Existence errors
46
- if isinstance ( client_error , client . err . ProgrammingError ) and client_error . args [ 0 ] == 1146 :
47
- return errors .MissingTableError (client_error . args [1 ], query )
48
- if isinstance ( client_error , client . err . InternalError ) and client_error . args [ 0 ] == 1364 :
49
- return errors .MissingAttributeError (* client_error . args [ 1 :] )
50
- if isinstance ( client_error , client . err . InternalError ) and client_error . args [ 0 ] == 1054 :
51
- return errors .UnknownAttributeError (* client_error . args [ 1 :] )
51
+ if err == 1146 :
52
+ return errors .MissingTableError (args [0 ], query )
53
+ if err == 1364 :
54
+ return errors .MissingAttributeError (* args )
55
+ if err == 1054 :
56
+ return errors .UnknownAttributeError (* args )
52
57
# all the other errors are re-raised in original form
53
58
return client_error
54
59
55
60
56
- logger = logging .getLogger (__name__ )
57
-
58
-
59
61
def conn (host = None , user = None , password = None , * , init_fun = None , reset = False , use_tls = None ):
60
62
"""
61
63
Returns a persistent connection object to be shared by multiple modules.
@@ -192,7 +194,7 @@ def _execute_query(cursor, query, args, cursor_class, suppress_warnings):
192
194
# suppress all warnings arising from underlying SQL library
193
195
warnings .simplefilter ("ignore" )
194
196
cursor .execute (query , args )
195
- except client_errors as err :
197
+ except client . err . Error as err :
196
198
raise translate_query_error (err , query ) from None
197
199
198
200
def query (self , query , args = (), * , as_dict = False , suppress_warnings = True , reconnect = None ):
@@ -207,7 +209,7 @@ def query(self, query, args=(), *, as_dict=False, suppress_warnings=True, reconn
207
209
"""
208
210
if reconnect is None :
209
211
reconnect = config ['database.reconnect' ]
210
- logger .debug ("Executing SQL:" + query [0 : 300 ])
212
+ logger .debug ("Executing SQL:" + query [: query_log_max_length ])
211
213
cursor_class = client .cursors .DictCursor if as_dict else client .cursors .Cursor
212
214
cursor = self ._conn .cursor (cursor = cursor_class )
213
215
try :
0 commit comments