From aa679e06b27cf036d6906fdfd201b26368b10e80 Mon Sep 17 00:00:00 2001 From: Aditya Toshniwal Date: Wed, 5 Aug 2020 12:41:28 +0530 Subject: [PATCH] Replace the generic exception class with a more specific one to fix SonarQube issues. --- .../databases/schemas/tables/columns/utils.py | 6 ++--- .../schemas/tables/compound_triggers/utils.py | 8 +++--- .../constraints/check_constraint/utils.py | 6 ++--- .../constraints/exclusion_constraint/utils.py | 6 ++--- .../tables/constraints/foreign_key/utils.py | 12 ++++----- .../constraints/index_constraint/utils.py | 6 ++--- .../databases/schemas/tables/indexes/utils.py | 6 ++--- .../tables/row_security_policies/utils.py | 6 ++--- .../schemas/tables/triggers/utils.py | 8 +++--- web/pgadmin/tools/sqleditor/command.py | 16 ++++++------ .../tools/sqleditor/utils/get_column_types.py | 3 ++- .../utils/is_query_resultset_updatable.py | 7 +++--- web/pgadmin/utils/exception.py | 25 ++++++++++++++++++- .../python_test_utils/test_utils.py | 6 ++--- 14 files changed, 73 insertions(+), 48 deletions(-) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/utils.py index b102f0205..8b55d26da 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ExecuteError from pgadmin.browser.server_groups.servers.databases.schemas.utils \ import DataTypeReader from pgadmin.browser.server_groups.servers.utils import parse_priv_from_db, \ @@ -52,7 +52,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -211,7 +211,7 @@ def get_formatted_columns(conn, tid, data, other_columns, status, res = conn.execute_dict(SQL) if not status: - raise Exception(res) + raise ExecuteError(res) all_columns = res['rows'] edit_types = {} diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/compound_triggers/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/compound_triggers/utils.py index 7274cafa5..7ecc6d5c4 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/compound_triggers/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/compound_triggers/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from pgadmin.browser.server_groups.servers.databases.schemas.utils \ import trigger_definition from functools import wraps @@ -50,7 +50,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -105,7 +105,7 @@ def get_sql(conn, data, tid, trid, datlastsysoid, template_path=None): status, res = conn.execute_dict(sql) if not status: - raise Exception(res) + raise ExecuteError(res) elif len(res['rows']) == 0: raise ObjectGone( _('Could not find the compound trigger in the table.')) @@ -162,7 +162,7 @@ def get_reverse_engineered_sql(conn, **kwargs): status, res = conn.execute_dict(SQL) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone( diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/utils.py index 081fa405a..d46c0ee2d 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from functools import wraps @@ -48,7 +48,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -163,7 +163,7 @@ def get_sql(conn, data, tid, cid=None, template_path=None): tid=tid, cid=cid) status, res = conn.execute_dict(sql) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone( diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/utils.py index 826325450..f216adfbb 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from functools import wraps @@ -48,7 +48,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -191,7 +191,7 @@ def get_sql(conn, data, did, tid, exid=None, template_path=None): did=did, tid=tid, cid=exid) status, res = conn.execute_dict(sql) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone( diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/utils.py index 2c0502027..a8c09bdcb 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/foreign_key/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from functools import wraps @@ -110,7 +110,7 @@ def search_coveringindex(conn, tid, cols, template_path=None): tid=tid) status, constraints = conn.execute_dict(SQL) if not status: - raise Exception(constraints) + raise ExecuteError(constraints) for constraint in constraints['rows']: sql = render_template( @@ -120,7 +120,7 @@ def search_coveringindex(conn, tid, cols, template_path=None): status, rest = conn.execute_dict(sql) if not status: - raise Exception(rest) + raise ExecuteError(rest) index_cols = set() for r in rest['rows']: @@ -146,7 +146,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -254,7 +254,7 @@ def get_sql(conn, data, tid, fkid=None, template_path=None): status, res = conn.execute_dict(col_sql) if not status: - raise Exception(res) + raise ExecuteError(res) columns = [] for row in res['rows']: @@ -290,7 +290,7 @@ def _get_properties_for_fk_const(tid, fkid, data, template_path, conn): tid=tid, cid=fkid) status, res = conn.execute_dict(sql) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone( diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py index 23de4de12..e6fa984d4 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from functools import wraps @@ -48,7 +48,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -195,7 +195,7 @@ def get_sql(conn, data, did, tid, ctype, cid=None, template_path=None): constraint_type=ctype) status, res = conn.execute_dict(sql) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone( diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/utils.py index ae05e22d5..d67f11d4d 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/indexes/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from functools import wraps @@ -48,7 +48,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -290,7 +290,7 @@ def get_reverse_engineered_sql(conn, **kwargs): status, res = conn.execute_dict(SQL) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone(_('Could not find the index in the table.')) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/row_security_policies/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/row_security_policies/utils.py index 90c15551d..8cfaa3906 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/row_security_policies/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/row_security_policies/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from functools import wraps @@ -49,7 +49,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -119,7 +119,7 @@ def get_reverse_engineered_sql(conn, **kwargs): status, res = conn.execute_dict(SQL) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone(_('Could not find the policy in the table.')) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/utils.py index 66ca7aeb8..0db7e433b 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/triggers/utils.py @@ -12,7 +12,7 @@ from flask import render_template from flask_babelex import gettext as _ from pgadmin.utils.ajax import internal_server_error -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from pgadmin.browser.server_groups.servers.databases.schemas.utils \ import trigger_definition from config import PG_DEFAULT_DRIVER @@ -52,7 +52,7 @@ def get_parent(conn, tid, template_path=None): 'get_parent.sql']), tid=tid) status, rset = conn.execute_2darray(SQL) if not status: - raise Exception(rset) + raise ExecuteError(rset) schema = '' table = '' @@ -164,7 +164,7 @@ def get_sql(conn, **kwargs): status, res = conn.execute_dict(sql) if not status: - raise Exception(res) + raise ExecuteError(res) elif len(res['rows']) == 0: raise ObjectGone(_('Could not find the trigger in the table.')) @@ -271,7 +271,7 @@ def get_reverse_engineered_sql(conn, **kwargs): status, res = conn.execute_dict(SQL) if not status: - raise Exception(res) + raise ExecuteError(res) if len(res['rows']) == 0: raise ObjectGone(_('Could not find the trigger in the table.')) diff --git a/web/pgadmin/tools/sqleditor/command.py b/web/pgadmin/tools/sqleditor/command.py index 288e145be..d856c62bb 100644 --- a/web/pgadmin/tools/sqleditor/command.py +++ b/web/pgadmin/tools/sqleditor/command.py @@ -21,7 +21,7 @@ from pgadmin.tools.sqleditor.utils.is_query_resultset_updatable \ from pgadmin.tools.sqleditor.utils.save_changed_data import save_changed_data from pgadmin.tools.sqleditor.utils.get_column_types import get_columns_types from pgadmin.utils.preferences import Preferences -from pgadmin.utils.exception import ObjectGone +from pgadmin.utils.exception import ObjectGone, ExecuteError from config import PG_DEFAULT_DRIVER @@ -187,7 +187,7 @@ class SQLFilter(object): status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) if len(result['rows']) == 0: raise ObjectGone( gettext("The specified object could not be found.")) @@ -401,7 +401,7 @@ class GridCommand(BaseCommand, SQLFilter, FetchedRowTracker): ) status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) for row in result['rows']: all_columns.append(row['attname']) @@ -542,7 +542,7 @@ class TableCommand(GridCommand): status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) for row in result['rows']: pk_names += driver.qtIdent(conn, row['attname']) + ',' @@ -590,7 +590,7 @@ class TableCommand(GridCommand): status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) for row in result['rows']: all_columns.append(row['attname']) @@ -602,7 +602,7 @@ class TableCommand(GridCommand): ) status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) for row in result['rows']: # Only append if not already present in the list @@ -646,7 +646,7 @@ class TableCommand(GridCommand): status, has_oids = conn.execute_scalar(query) if not status: - raise Exception(has_oids) + raise ExecuteError(has_oids) else: raise Exception( @@ -996,7 +996,7 @@ class QueryToolCommand(BaseCommand, FetchedRowTracker): status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) self.nsp_name = result['rows'][0]['nspname'] self.object_name = result['rows'][0]['relname'] diff --git a/web/pgadmin/tools/sqleditor/utils/get_column_types.py b/web/pgadmin/tools/sqleditor/utils/get_column_types.py index 5484985be..df901b16d 100644 --- a/web/pgadmin/tools/sqleditor/utils/get_column_types.py +++ b/web/pgadmin/tools/sqleditor/utils/get_column_types.py @@ -12,6 +12,7 @@ """ from flask import render_template +from pgadmin.utils.exception import ExecuteError def get_columns_types(is_query_tool, columns_info, table_oid, conn, has_oids): @@ -24,7 +25,7 @@ def get_columns_types(is_query_tool, columns_info, table_oid, conn, has_oids): colst, rset = conn.execute_2darray(query) if not colst: - raise Exception(rset) + raise ExecuteError(rset) column_types = dict() for key, col in enumerate(columns_info): diff --git a/web/pgadmin/tools/sqleditor/utils/is_query_resultset_updatable.py b/web/pgadmin/tools/sqleditor/utils/is_query_resultset_updatable.py index 453b3f641..220ca9e91 100644 --- a/web/pgadmin/tools/sqleditor/utils/is_query_resultset_updatable.py +++ b/web/pgadmin/tools/sqleditor/utils/is_query_resultset_updatable.py @@ -26,6 +26,7 @@ from flask_babelex import gettext from collections import OrderedDict from pgadmin.tools.sqleditor.utils.get_column_types import get_columns_types +from pgadmin.utils.exception import ExecuteError def is_query_resultset_updatable(conn, sql_path): @@ -135,7 +136,7 @@ def _check_oids(conn, sql_path, table_oid, columns_info): status, has_oids = conn.execute_scalar(query) if not status: - raise Exception(has_oids) + raise ExecuteError(has_oids) # Check that the oid column is selected in results columns oid_column_selected = False @@ -192,7 +193,7 @@ def _get_primary_keys(sql_path, table_oid, conn): ) status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) primary_keys_columns = [] primary_keys = OrderedDict() @@ -216,7 +217,7 @@ def _get_table_columns(sql_path, table_oid, conn): ) status, result = conn.execute_dict(query) if not status: - raise Exception(result) + raise ExecuteError(result) columns = {} for row in result['rows']: diff --git a/web/pgadmin/utils/exception.py b/web/pgadmin/utils/exception.py index 5417ccc66..43e9ad42d 100644 --- a/web/pgadmin/utils/exception.py +++ b/web/pgadmin/utils/exception.py @@ -12,7 +12,7 @@ from werkzeug.http import HTTP_STATUS_CODES from flask_babelex import gettext as _ from flask import request -from pgadmin.utils.ajax import service_unavailable, gone +from pgadmin.utils.ajax import service_unavailable, gone, internal_server_error class ConnectionLost(HTTPException): @@ -128,3 +128,26 @@ class ObjectGone(HTTPException): def __repr__(self): return self.error_msg + + +class ExecuteError(HTTPException): + """ + ExecuteError + """ + + def __init__(self, error_msg): + self.error_msg = error_msg + HTTPException.__init__(self) + + @property + def name(self): + return HTTP_STATUS_CODES.get(500, 'Internal server error') + + def get_response(self, environ=None): + return internal_server_error(self.error_msg) + + def __str__(self): + return self.error_msg + + def __repr__(self): + return self.error_msg diff --git a/web/regression/python_test_utils/test_utils.py b/web/regression/python_test_utils/test_utils.py index b93d8ad19..2f36b5e81 100644 --- a/web/regression/python_test_utils/test_utils.py +++ b/web/regression/python_test_utils/test_utils.py @@ -1129,9 +1129,9 @@ def check_binary_path_or_skip_test(cls, utility_name): cls.server['default_binary_paths'][cls.server['type']], utility_name ) - retVal = does_utility_exist(binary_path) - if retVal is not None: - cls.skipTest(retVal) + ret_val = does_utility_exist(binary_path) + if ret_val is not None: + cls.skipTest(ret_val) def get_watcher_dialogue_status(self):