Skip to content

postgis connection error handling improvements #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/opengeo/gui/dialogs/pgconnectiondialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ def setupUi(self):
horizontalLayout.addWidget(passwordLabel)
horizontalLayout.addWidget(self.passwordBox)
self.verticalLayout.addLayout(horizontalLayout)

horizontalLayout = QHBoxLayout()
self.statusLabel = QLabel('')
horizontalLayout.addWidget(self.statusLabel)
self.verticalLayout.addLayout(horizontalLayout)

self.groupBox.setLayout(self.verticalLayout)
self.layout.addWidget(self.groupBox)
self.spacer = QSpacerItem(20,20, QSizePolicy.Minimum,QSizePolicy.Expanding)
self.spacer = QSpacerItem(20,10, QSizePolicy.Minimum,QSizePolicy.Expanding)
self.layout.addItem(self.spacer)

self.buttonBox = QDialogButtonBox(self)
Expand Down Expand Up @@ -146,6 +151,8 @@ def accept(self):
settings.setValue("database", self.databaseBox.text() );
settings.setValue("username", self.usernameBox.text());
settings.setValue("password", self.passwordBox.text());
self.statusLabel.setText('Connecting ...')
QApplication.processEvents()
self.conn = PgConnection(self.nameBox.text(), settings.value('host'), int(settings.value('port')),
settings.value('database'), settings.value('username'),
settings.value('password'))
Expand Down
8 changes: 6 additions & 2 deletions src/opengeo/gui/pgexploreritems.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ def contextMenuActions(self, tree, explorer):
return actions

def _getDescriptionHtml(self, tree, explorer):
if not self.element.isValid:
html = ('<p>Cannot connect to this database. This might be caused by missing user/passwd credentials.'
if not self.element.isValid:
if self.element.connectionFailureMessage:
msg = self.element.connectionFailureMessage.replace('\n','<br>')
html = '<p>There was an error connecting to the database:</p><p>%s</p>' % msg
else:
html = ('<p>Cannot connect to this database. This might be caused by missing user/passwd credentials.'
'Try <a href="refresh">refreshing</a> the connection, to enter new credentials and retry to connect</p>')
return html
else:
Expand Down
25 changes: 17 additions & 8 deletions src/opengeo/postgis/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from postgis_utils import GeoDB
from postgis_utils import GeoDB, DbError
from schema import Schema
from qgis.core import *
from PyQt4 import QtCore
Expand All @@ -9,17 +9,26 @@
class PgConnection(object):

def __init__(self, name, host, port, database, username, password):
self.name = name
self.name = name
self.host = host
self.port = port
self.database = database
self.database = database
self.connectionFailureMessage = None
self.isValid = False
try:
self.geodb = GeoDB(host, port, database, username, password)
self.isValid = True
self.username = username
self.password = password
except:
self.isValid = False
except DbError, ex:
self.connectionFailureMessage = str(ex)
else:
try:
self.geodb._exec_sql_and_commit('select * from postgis_version()')
self.isValid = True
self.username = username
self.password = password
except DbError, ex:
self.connectionFailureMessage = 'It appears that the PostGIS '\
'extensions are not loaded in the database.<hr>\n'\
'The error message was:\n%s' % ex


def schemas(self):
Expand Down