1313from .dependencies import Dependencies
1414from .plugin import connection_plugins
1515
16- # client errors to catch
17- client_errors = ( client . err . InterfaceError , client . err . DatabaseError )
16+ logger = logging . getLogger ( __name__ )
17+ query_log_max_length = 300
1818
1919
2020def get_host_hook (host_input ):
@@ -48,39 +48,39 @@ def translate_query_error(client_error, query):
4848 :param query: sql query with placeholders
4949 :return: an instance of the corresponding subclass of datajoint.errors.DataJointError
5050 """
51+ logger .debug ('type: {}, args: {}' .format (type (client_error ), client_error .args ))
52+
53+ err , * args = client_error .args
54+
5155 # Loss of connection errors
52- if isinstance (client_error , client .err .InterfaceError ) and client_error .args [0 ] == "(0, '')" :
53- return errors .LostConnectionError ('Server connection lost due to an interface error.' , * client_error .args [1 :])
54- disconnect_codes = {
55- 2006 : "Connection timed out" ,
56- 2013 : "Server connection lost" }
57- if isinstance (client_error , client .err .OperationalError ) and client_error .args [0 ] in disconnect_codes :
58- return errors .LostConnectionError (disconnect_codes [client_error .args [0 ]], * client_error .args [1 :])
56+ if err in (0 , "(0, '')" ):
57+ return errors .LostConnectionError ('Server connection lost due to an interface error.' , * args )
58+ if err == 2006 :
59+ return errors .LostConnectionError ("Connection timed out" , * args )
60+ if err == 2013 :
61+ return errors .LostConnectionError ("Server connection lost" , * args )
5962 # Access errors
60- if isinstance ( client_error , client . err . OperationalError ) and client_error . args [ 0 ] in (1044 , 1142 ):
61- return errors .AccessError ('Insufficient privileges.' , client_error . args [1 ], query )
63+ if err in (1044 , 1142 ):
64+ return errors .AccessError ('Insufficient privileges.' , args [0 ], query )
6265 # Integrity errors
63- if isinstance ( client_error , client . err . IntegrityError ) and client_error . args [ 0 ] == 1062 :
64- return errors .DuplicateError (* client_error . args [ 1 :] )
65- if isinstance ( client_error , client . err . IntegrityError ) and client_error . args [ 0 ] == 1452 :
66- return errors .IntegrityError (* client_error . args [ 1 :] )
66+ if err == 1062 :
67+ return errors .DuplicateError (* args )
68+ if err == 1452 :
69+ return errors .IntegrityError (* args )
6770 # Syntax errors
68- if isinstance ( client_error , client . err . ProgrammingError ) and client_error . args [ 0 ] == 1064 :
69- return errors .QuerySyntaxError (client_error . args [1 ], query )
71+ if err == 1064 :
72+ return errors .QuerySyntaxError (args [0 ], query )
7073 # Existence errors
71- if isinstance ( client_error , client . err . ProgrammingError ) and client_error . args [ 0 ] == 1146 :
72- return errors .MissingTableError (client_error . args [1 ], query )
73- if isinstance ( client_error , client . err . InternalError ) and client_error . args [ 0 ] == 1364 :
74- return errors .MissingAttributeError (* client_error . args [ 1 :] )
75- if isinstance ( client_error , client . err . InternalError ) and client_error . args [ 0 ] == 1054 :
76- return errors .UnknownAttributeError (* client_error . args [ 1 :] )
74+ if err == 1146 :
75+ return errors .MissingTableError (args [0 ], query )
76+ if err == 1364 :
77+ return errors .MissingAttributeError (* args )
78+ if err == 1054 :
79+ return errors .UnknownAttributeError (* args )
7780 # all the other errors are re-raised in original form
7881 return client_error
7982
8083
81- logger = logging .getLogger (__name__ )
82-
83-
8484def conn (host = None , user = None , password = None , * , init_fun = None , reset = False , use_tls = None ):
8585 """
8686 Returns a persistent connection object to be shared by multiple modules.
@@ -221,7 +221,7 @@ def _execute_query(cursor, query, args, cursor_class, suppress_warnings):
221221 # suppress all warnings arising from underlying SQL library
222222 warnings .simplefilter ("ignore" )
223223 cursor .execute (query , args )
224- except client_errors as err :
224+ except client . err . Error as err :
225225 raise translate_query_error (err , query )
226226
227227 def query (self , query , args = (), * , as_dict = False , suppress_warnings = True , reconnect = None ):
@@ -236,7 +236,7 @@ def query(self, query, args=(), *, as_dict=False, suppress_warnings=True, reconn
236236 """
237237 if reconnect is None :
238238 reconnect = config ['database.reconnect' ]
239- logger .debug ("Executing SQL:" + query [0 : 300 ])
239+ logger .debug ("Executing SQL:" + query [: query_log_max_length ])
240240 cursor_class = client .cursors .DictCursor if as_dict else client .cursors .Cursor
241241 cursor = self ._conn .cursor (cursor = cursor_class )
242242 try :
0 commit comments