Improve PostgreSQL error handling

Re-raise a DbConnectionError if the connection raises an error.
This commit is contained in:
Nick Hall 2017-09-23 21:24:54 +01:00
parent 0f9a6d57bf
commit 457722811d
3 changed files with 27 additions and 2 deletions

View File

@ -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):

View File

@ -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(

View File

@ -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)
try:
self.dbapi = Connection(**dbkwargs)
except psycopg2.OperationalError as msg:
raise DbConnectionError(str(msg), config_file)
#-------------------------------------------------------------------------