1212from . import errors
1313from .dependencies import Dependencies
1414
15- # client errors to catch
16- client_errors = ( client . err . InterfaceError , client . err . DatabaseError )
15+ logger = logging . getLogger ( __name__ )
16+ query_log_max_length = 300
1717
1818
1919def translate_query_error (client_error , query ):
@@ -23,39 +23,39 @@ def translate_query_error(client_error, query):
2323 :param query: sql query with placeholders
2424 :return: an instance of the corresponding subclass of datajoint.errors.DataJointError
2525 """
26+ logger .debug ('type: {}, args: {}' .format (type (client_error ), client_error .args ))
27+
28+ err , * args = client_error .args
29+
2630 # 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 :])
31+ if err in (0 , "(0, '')" ):
32+ return errors .LostConnectionError ('Server connection lost due to an interface error.' , * args )
33+ if err == 2006 :
34+ return errors .LostConnectionError ("Connection timed out" , * args )
35+ if err == 2013 :
36+ return errors .LostConnectionError ("Server connection lost" , * args )
3437 # 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 )
38+ if err in (1044 , 1142 ):
39+ return errors .AccessError ('Insufficient privileges.' , args [0 ], query )
3740 # 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 :] )
41+ if err == 1062 :
42+ return errors .DuplicateError (* args )
43+ if err == 1452 :
44+ return errors .IntegrityError (* args )
4245 # 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 )
46+ if err == 1064 :
47+ return errors .QuerySyntaxError (args [0 ], query )
4548 # 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 :] )
49+ if err == 1146 :
50+ return errors .MissingTableError (args [0 ], query )
51+ if err == 1364 :
52+ return errors .MissingAttributeError (* args )
53+ if err == 1054 :
54+ return errors .UnknownAttributeError (* args )
5255 # all the other errors are re-raised in original form
5356 return client_error
5457
5558
56- logger = logging .getLogger (__name__ )
57-
58-
5959def conn (host = None , user = None , password = None , * , init_fun = None , reset = False , use_tls = None ):
6060 """
6161 Returns a persistent connection object to be shared by multiple modules.
@@ -192,7 +192,7 @@ def _execute_query(cursor, query, args, cursor_class, suppress_warnings):
192192 # suppress all warnings arising from underlying SQL library
193193 warnings .simplefilter ("ignore" )
194194 cursor .execute (query , args )
195- except client_errors as err :
195+ except client . err . Error as err :
196196 raise translate_query_error (err , query ) from None
197197
198198 def query (self , query , args = (), * , as_dict = False , suppress_warnings = True , reconnect = None ):
@@ -207,7 +207,7 @@ def query(self, query, args=(), *, as_dict=False, suppress_warnings=True, reconn
207207 """
208208 if reconnect is None :
209209 reconnect = config ['database.reconnect' ]
210- logger .debug ("Executing SQL:" + query [0 : 300 ])
210+ logger .debug ("Executing SQL:" + query [: query_log_max_length ])
211211 cursor_class = client .cursors .DictCursor if as_dict else client .cursors .Cursor
212212 cursor = self ._conn .cursor (cursor = cursor_class )
213213 try :
0 commit comments