diff --git a/web/pgadmin/utils/driver/psycopg3/connection.py b/web/pgadmin/utils/driver/psycopg3/connection.py index ac7e30df2dd..c3ca4fa424e 100644 --- a/web/pgadmin/utils/driver/psycopg3/connection.py +++ b/web/pgadmin/utils/driver/psycopg3/connection.py @@ -1675,23 +1675,27 @@ def _formatted_exception_msg(self, exception_obj, formatted_msg): message """ + if hasattr(exception_obj, 'pgerror'): + errmsg = exception_obj.pgerror + elif hasattr(exception_obj, 'diag') and \ + hasattr(exception_obj.diag, 'message_detail') and\ + exception_obj.diag.message_detail is not None: + errmsg = exception_obj.diag.message_primary + '\n' + \ + exception_obj.diag.message_detail + else: + errmsg = str(exception_obj) + # if formatted_msg is false then return from the function if not formatted_msg: - if hasattr(exception_obj, 'pgerror'): - errmsg = exception_obj.pgerror - elif hasattr(exception_obj, 'diag') and \ - hasattr(exception_obj.diag, 'message_detail') and\ - exception_obj.diag.message_detail is not None: - errmsg = (exception_obj.diag.message_primary + '\n' + - exception_obj.diag.message_detail) - else: - errmsg = str(exception_obj) - notices = self.get_notices() errmsg = errmsg.replace('\n', '
') return errmsg if notices == '' else notices + '\n' + errmsg - errmsg = '' + # Do not append if error starts with `ERROR:` as most pg related + # error starts with `ERROR:` + if not errmsg.startswith('ERROR:'): + errmsg = gettext('ERROR: ') + errmsg + ' \n\n' + if exception_obj.diag.severity is not None \ and exception_obj.diag.message_primary is not None: ex_diag_message = "{0}: {1}".format( @@ -1714,25 +1718,29 @@ def _formatted_exception_msg(self, exception_obj, formatted_msg): errmsg += gettext('SQL state: ') errmsg += exception_obj.diag.sqlstate - if exception_obj.diag.message_detail is not None: + if exception_obj.diag.message_detail is not None and \ + 'Detail:'.lower() not in errmsg.lower(): if not errmsg.endswith('\n'): errmsg += '\n' errmsg += gettext('Detail: ') errmsg += exception_obj.diag.message_detail - if exception_obj.diag.message_hint is not None: + if exception_obj.diag.message_hint is not None and \ + 'Hint:'.lower() not in errmsg.lower(): if not errmsg.endswith('\n'): errmsg += '\n' errmsg += gettext('Hint: ') errmsg += exception_obj.diag.message_hint - if exception_obj.diag.statement_position is not None: + if exception_obj.diag.statement_position is not None and \ + 'Character:'.lower() not in errmsg.lower(): if not errmsg.endswith('\n'): errmsg += '\n' errmsg += gettext('Character: ') errmsg += exception_obj.diag.statement_position - if exception_obj.diag.context is not None: + if exception_obj.diag.context is not None and \ + 'Context:'.lower() not in errmsg.lower(): if not errmsg.endswith('\n'): errmsg += '\n' errmsg += gettext('Context: ')