1515
1616logger = logging .getLogger (__name__ )
1717
18- # client errors to catch
19- client_errors = (client .err .InterfaceError , client .err .DatabaseError )
20-
2118
2219def translate_query_error (client_error , query ):
2320 """
@@ -27,39 +24,38 @@ def translate_query_error(client_error, query):
2724 :return: an instance of the corresponding subclass of datajoint.errors.DataJointError
2825 """
2926 logger .debug ('type: {}, args: {}' .format (type (client_error ), client_error .args ))
27+
28+ err , * args = client_error .args
29+
3030 # Loss of connection errors
31- if isinstance (client_error , client .err .InterfaceError ) and client_error .args [0 ] == 0 :
32- return errors .LostConnectionError ('Server connection lost due to an interface error.' , * client_error .args [1 :])
33- disconnect_codes = {
34- 2006 : "Connection timed out" ,
35- 2013 : "Server connection lost" }
36- if isinstance (client_error , client .err .OperationalError ) and client_error .args [0 ] in disconnect_codes :
37- 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 )
3837 # Access errors
39- if isinstance ( client_error , client . err . OperationalError ) and client_error . args [ 0 ] in (1044 , 1142 ):
40- 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 )
4140 # Integrity errors
42- if isinstance ( client_error , client . err . IntegrityError ) and client_error . args [ 0 ] == 1062 :
43- return errors .DuplicateError (* client_error . args [ 1 :] )
44- if isinstance ( client_error , client . err . IntegrityError ) and client_error . args [ 0 ] == 1452 :
45- 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 )
4645 # Syntax errors
47- if isinstance ( client_error , client . err . ProgrammingError ) and client_error . args [ 0 ] == 1064 :
48- return errors .QuerySyntaxError (client_error . args [1 ], query )
46+ if err == 1064 :
47+ return errors .QuerySyntaxError (args [0 ], query )
4948 # Existence errors
50- if isinstance ( client_error , client . err . ProgrammingError ) and client_error . args [ 0 ] == 1146 :
51- return errors .MissingTableError (client_error . args [1 ], query )
52- if isinstance ( client_error , client . err . OperationalError ) and client_error . args [ 0 ] == 1364 :
53- return errors .MissingAttributeError (* client_error . args [ 1 :] )
54- if isinstance ( client_error , client . err . OperationalError ) and client_error . args [ 0 ] == 1054 :
55- 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 )
5655 # all the other errors are re-raised in original form
5756 return client_error
5857
5958
60-
61-
62-
6359def conn (host = None , user = None , password = None , * , init_fun = None , reset = False , use_tls = None ):
6460 """
6561 Returns a persistent connection object to be shared by multiple modules.
@@ -196,7 +192,7 @@ def _execute_query(cursor, query, args, cursor_class, suppress_warnings):
196192 # suppress all warnings arising from underlying SQL library
197193 warnings .simplefilter ("ignore" )
198194 cursor .execute (query , args )
199- except client_errors as err :
195+ except client . err . Error as err :
200196 raise translate_query_error (err , query ) from None
201197
202198 def query (self , query , args = (), * , as_dict = False , suppress_warnings = True , reconnect = None ):
0 commit comments