From 317a04eafcd4e19e6e47a5c29614ada05a074b2d Mon Sep 17 00:00:00 2001 From: Murtuza Zabuawala Date: Wed, 30 Jan 2019 16:39:34 +0530 Subject: [PATCH] Handle backend errors properly and display them correctly on GUI for Grant Wizard --- web/pgadmin/static/scss/_alert.scss | 3 +- web/pgadmin/tools/grant_wizard/__init__.py | 59 ++++++++++++------- .../grant_wizard/static/js/grant_wizard.js | 21 ++++++- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/web/pgadmin/static/scss/_alert.scss b/web/pgadmin/static/scss/_alert.scss index e8676b8c9..f5082c37b 100644 --- a/web/pgadmin/static/scss/_alert.scss +++ b/web/pgadmin/static/scss/_alert.scss @@ -21,7 +21,8 @@ display: inline-block; } -.alert.alert-info { +.alert.alert-info, +.alert.alert-danger { padding: 0.5rem; } diff --git a/web/pgadmin/tools/grant_wizard/__init__.py b/web/pgadmin/tools/grant_wizard/__init__.py index 6ef374b10..8b6bb644e 100644 --- a/web/pgadmin/tools/grant_wizard/__init__.py +++ b/web/pgadmin/tools/grant_wizard/__init__.py @@ -191,6 +191,7 @@ def properties(sid, did, node_id, node_type): server_prop = server_info res_data = [] + failed_objects = [] manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) conn = manager.connection(did=did) @@ -231,11 +232,11 @@ def properties(sid, did, node_id, node_type): node_id=node_id, type='function') status, res = conn.execute_dict(SQL) - if not status: - return internal_server_error(errormsg=res) - - res_data.extend(res['rows']) + current_app.logger.error(res) + failed_objects.append('function') + else: + res_data.extend(res['rows']) # Fetch procedures only if server type is EPAS or PG >= 11 if (len(server_prop) > 0 and @@ -250,9 +251,10 @@ def properties(sid, did, node_id, node_type): status, res = conn.execute_dict(SQL) if not status: - return internal_server_error(errormsg=res) - - res_data.extend(res['rows']) + current_app.logger.error(res) + failed_objects.append('procedure') + else: + res_data.extend(res['rows']) # Fetch trigger functions if ntype in ['schema', 'trigger_function']: @@ -262,9 +264,10 @@ def properties(sid, did, node_id, node_type): status, res = conn.execute_dict(SQL) if not status: - return internal_server_error(errormsg=res) - - res_data.extend(res['rows']) + current_app.logger.error(res) + failed_objects.append('trigger function') + else: + res_data.extend(res['rows']) # Fetch Sequences against schema if ntype in ['schema', 'sequence']: @@ -274,8 +277,10 @@ def properties(sid, did, node_id, node_type): status, res = conn.execute_dict(SQL) if not status: - return internal_server_error(errormsg=res) - res_data.extend(res['rows']) + current_app.logger.error(res) + failed_objects.append('sequence') + else: + res_data.extend(res['rows']) # Fetch Tables against schema if ntype in ['schema', 'table']: @@ -285,9 +290,10 @@ def properties(sid, did, node_id, node_type): status, res = conn.execute_dict(SQL) if not status: - return internal_server_error(errormsg=res) - - res_data.extend(res['rows']) + current_app.logger.error(res) + failed_objects.append('table') + else: + res_data.extend(res['rows']) # Fetch Views against schema if ntype in ['schema', 'view']: @@ -297,9 +303,10 @@ def properties(sid, did, node_id, node_type): status, res = conn.execute_dict(SQL) if not status: - return internal_server_error(errormsg=res) - - res_data.extend(res['rows']) + current_app.logger.error(res) + failed_objects.append('view') + else: + res_data.extend(res['rows']) # Fetch Materialzed Views against schema if ntype in ['schema', 'mview']: @@ -309,12 +316,20 @@ def properties(sid, did, node_id, node_type): status, res = conn.execute_dict(SQL) if not status: - return internal_server_error(errormsg=res) + current_app.logger.error(res) + failed_objects.append('materialized view') + else: + res_data.extend(res['rows']) - res_data.extend(res['rows']) + msg = None + if len(failed_objects) > 0: + msg = gettext('Unable to fetch the {} objects'.format( + ", ".join(failed_objects)) + ) - return ajax_response( - response=res_data, + return make_json_response( + result=res_data, + info=msg, status=200 ) diff --git a/web/pgadmin/tools/grant_wizard/static/js/grant_wizard.js b/web/pgadmin/tools/grant_wizard/static/js/grant_wizard.js index 85d78da2d..4dd904b4d 100644 --- a/web/pgadmin/tools/grant_wizard/static/js/grant_wizard.js +++ b/web/pgadmin/tools/grant_wizard/static/js/grant_wizard.js @@ -608,9 +608,28 @@ define([ $('.wizard-progress-bar p').show(); coll.fetch({ - success: function() { + success: function(c, xhr) { $('.wizard-progress-bar p').html(''); $('.wizard-progress-bar').hide(); + c.set(xhr.result, {parse: true}); + // If some objects failed while fetching then we will notify the user + if (xhr && xhr.info && xhr.info !== '') { + $('.pg-prop-status-bar .alert-text').html(xhr.info); + $('.pg-prop-status-bar').css('visibility', 'visible'); + } + }, + error: function(m, xhr) { + // If the main request fails as whole then + let msg; + if (xhr && xhr.responseJSON && xhr.responseJSON.errormsg) { + msg = xhr.responseJSON.errormsg; + } + + if(!msg) { + msg = gettext('Unable to fetch the database objects due to an error'); + } + $('.wizard-progress-bar p').removeClass('alert-info').addClass('alert-danger'); + $('.wizard-progress-bar p').text(msg); }, reset: true, }, this);