1212from . import errors
1313from .dependencies import Dependencies
1414
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
1719
1820
1921def translate_query_error (client_error , query ):
@@ -23,39 +25,39 @@ def translate_query_error(client_error, query):
2325 :param query: sql query with placeholders
2426 :return: an instance of the corresponding subclass of datajoint.errors.DataJointError
2527 """
28+ logger .debug ('type: {}, args: {}' .format (type (client_error ), client_error .args ))
29+
30+ err , * args = client_error .args
31+
2632 # 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 )
3439 # 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 )
3742 # 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 )
4247 # 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 )
4550 # 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 )
5257 # all the other errors are re-raised in original form
5358 return client_error
5459
5560
56- logger = logging .getLogger (__name__ )
57-
58-
5961def conn (host = None , user = None , password = None , * , init_fun = None , reset = False , use_tls = None ):
6062 """
6163 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):
192194 # suppress all warnings arising from underlying SQL library
193195 warnings .simplefilter ("ignore" )
194196 cursor .execute (query , args )
195- except client_errors as err :
197+ except client . err . Error as err :
196198 raise translate_query_error (err , query ) from None
197199
198200 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
207209 """
208210 if reconnect is None :
209211 reconnect = config ['database.reconnect' ]
210- logger .debug ("Executing SQL:" + query [0 : 300 ])
212+ logger .debug ("Executing SQL:" + query [: query_log_max_length ])
211213 cursor_class = client .cursors .DictCursor if as_dict else client .cursors .Cursor
212214 cursor = self ._conn .cursor (cursor = cursor_class )
213215 try :
0 commit comments