@@ -101,9 +101,10 @@ def panel(trans_id):
101101 s = Server .query .filter_by (id = int (params ['sid' ])).first ()
102102 if s :
103103 data = _get_database_role (params ['sid' ], params ['did' ])
104- params ['db' ] = underscore_escape (data ['db_name' ]) \
105- if 'db_name' in data else 'postgres'
106- params ['role' ] = underscore_escape (data ['role' ])
104+ if data :
105+ params ['db' ] = underscore_escape (data ['db_name' ]) \
106+ if 'db_name' in data else 'postgres'
107+ params ['role' ] = underscore_escape (data ['role' ])
107108 set_env_variables (is_win = _platform == 'win32' )
108109 return render_template ("psql/index.html" ,
109110 params = json .dumps (params ))
@@ -303,7 +304,9 @@ def read_and_forward_pty_output(sid, data):
303304 # Check user is authenticated and PSQL is enabled in config.
304305 if current_user .is_authenticated and config .ENABLE_PSQL :
305306 connection_data = []
307+ connection_successful = False
306308 try :
309+ print ('In first try' )
307310 db = ''
308311 if data ['db' ]:
309312 db = underscore_unescape (data ['db' ])
@@ -326,33 +329,36 @@ def read_and_forward_pty_output(sid, data):
326329 return
327330 connection_data = get_connection_str (psql_utility , db ,
328331 manager )
332+ connection_successful = True
329333 except Exception as e :
330334 # If any error raised during the start the PSQL emit error to UI.
331335 # request.sid: This sid is socket id.
332- sio .emit (
333- 'conn_error' ,
334- {
335- 'error' : 'Error while running psql command: {0}' .format (e ),
336- }, namespace = '/pty' , room = request .sid )
336+ error_msg = 'Error while running psql command: {0}' .format (e )
337+ if str (e ) == 'Server is not connected.' :
338+ error_msg = 'Error while opening psql tool: {0}' .format (e )
337339
338- try :
339- if str (data ['sid' ]) not in app .config ['sid_soid_mapping' ]:
340- # request.sid: refer request.sid as socket id.
341- app .config ['sid_soid_mapping' ][str (data ['sid' ])] = list ()
342- app .config ['sid_soid_mapping' ][str (data ['sid' ])].append (
343- request .sid )
344- else :
345- app .config ['sid_soid_mapping' ][str (data ['sid' ])].append (
346- request .sid )
340+ sio .emit ( 'conn_error' , {'error' : error_msg },
341+ namespace = '/pty' , room = request .sid )
347342
348- sio .start_background_task (read_and_forward_pty_output ,
349- request .sid , data )
350- except Exception as e :
351- sio .emit (
352- 'conn_error' ,
353- {
354- 'error' : 'Error while running psql command: {0}' .format (e ),
355- }, namespace = '/pty' , room = request .sid )
343+ if connection_successful :
344+ try :
345+ if str (data ['sid' ]) not in app .config ['sid_soid_mapping' ]:
346+ # request.sid: refer request.sid as socket id.
347+ app .config ['sid_soid_mapping' ][str (data ['sid' ])] = list ()
348+ app .config ['sid_soid_mapping' ][str (data ['sid' ])].append (
349+ request .sid )
350+ else :
351+ app .config ['sid_soid_mapping' ][str (data ['sid' ])].append (
352+ request .sid )
353+
354+ sio .start_background_task (read_and_forward_pty_output ,
355+ request .sid , data )
356+ except Exception as e :
357+ sio .emit (
358+ 'conn_error' ,
359+ {
360+ 'error' : 'Error while running psql command: {0}' .format (e ),
361+ }, namespace = '/pty' , room = request .sid )
356362 else :
357363 # Show error if user is not authenticated.
358364 sio .emit ('conn_not_allow' , {'sid' : request .sid }, namespace = '/pty' ,
@@ -378,12 +384,6 @@ def _get_connection(sid, data):
378384 status , msg = conn .connect ()
379385 if not status :
380386 app .logger .error (msg )
381- sio .emit (sio .emit (
382- 'conn_error' ,
383- {
384- 'error' : 'Error while running psql command: {0}'
385- '' .format ('Server connection not present.' ),
386- }, namespace = '/pty' , room = request .sid ))
387387 raise RuntimeError ('Server is not connected.' )
388388
389389 return conn , manager
0 commit comments