From 457722811da82e584769cf80969e1c4f4448d1ba Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Sat, 23 Sep 2017 21:24:54 +0100 Subject: [PATCH] Improve PostgreSQL error handling Re-raise a DbConnectionError if the connection raises an error. --- gramps/gen/db/exceptions.py | 17 +++++++++++++++++ gramps/gui/dbloader.py | 6 +++++- gramps/plugins/db/dbapi/postgresql.py | 6 +++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/gramps/gen/db/exceptions.py b/gramps/gen/db/exceptions.py index f01a9948f..9fb2ee346 100644 --- a/gramps/gen/db/exceptions.py +++ b/gramps/gen/db/exceptions.py @@ -343,6 +343,23 @@ class PythonUpgradeRequiredError(Exception): 'db_python_version': self.db_python_version, 'current_python_version': self.current_python_version } +class DbConnectionError(Exception): + """ + Error used to report that a database connection failed. + """ + def __init__(self, msg, settings_file): + Exception.__init__(self) + self.msg = msg + self.settings_file = settings_file + + def __str__(self): + return _('Database connection failed.\n\n' + '%(message)s\n' + 'Please check your connection settings file:\n' + '%(settings_file)s') % { + 'message': self.msg, + 'settings_file': self.settings_file} + if __name__ == "__main__": """ Call this from the CLI (in order to find the imported modules): diff --git a/gramps/gui/dbloader.py b/gramps/gui/dbloader.py index 2b15382e9..243acdbe4 100644 --- a/gramps/gui/dbloader.py +++ b/gramps/gui/dbloader.py @@ -65,7 +65,8 @@ from gramps.gen.db.exceptions import (DbUpgradeRequiredError, BsddbUpgradeRequiredError, BsddbDowngradeRequiredError, PythonUpgradeRequiredError, - PythonDowngradeError) + PythonDowngradeError, + DbConnectionError) from .pluginmanager import GuiPluginManager from .dialog import (DBErrorDialog, ErrorDialog, QuestionDialog2, WarningDialog) @@ -278,6 +279,9 @@ class DbLoader(CLIDbLoader): except PythonDowngradeError as msg: self.dbstate.no_database() self._warn( _("Cannot open database"), str(msg)) + except DbConnectionError as msg: + self.dbstate.no_database() + self._warn(_("Cannot open database"), str(msg)) except OSError as msg: self.dbstate.no_database() self._errordialog( diff --git a/gramps/plugins/db/dbapi/postgresql.py b/gramps/plugins/db/dbapi/postgresql.py index 0de3f3c5e..fea696feb 100644 --- a/gramps/plugins/db/dbapi/postgresql.py +++ b/gramps/plugins/db/dbapi/postgresql.py @@ -41,6 +41,7 @@ from gramps.plugins.db.dbapi.dbapi import DBAPI from gramps.gen.utils.configmanager import ConfigManager from gramps.gen.config import config from gramps.gen.db.dbconst import ARRAYSIZE +from gramps.gen.db.exceptions import DbConnectionError from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -91,7 +92,10 @@ class PostgreSQL(DBAPI): for key in config_mgr.get_section_settings('database'): dbkwargs[key] = config_mgr.get('database.' + key) - self.dbapi = Connection(**dbkwargs) + try: + self.dbapi = Connection(**dbkwargs) + except psycopg2.OperationalError as msg: + raise DbConnectionError(str(msg), config_file) #-------------------------------------------------------------------------