Skip to content

Commit

Permalink
additional check for fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan committed May 21, 2019
1 parent d1f5f23 commit 3eb65ef
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/holdup/cli.py
Expand Up @@ -56,12 +56,17 @@ def is_passing(self, options):
try:
self.run(options)
except Exception as exc:
if self.is_fatal_error(exc):
raise exc
self.error = exc
else:
if options.verbose:
print('holdup: Passed check: %r' % self)
return True

def is_fatal_error(self, error):
return False

def __repr__(self):
if self.error:
return '{0} ({0.error})'.format(self)
Expand Down Expand Up @@ -92,15 +97,21 @@ def __init__(self, host, port, database, user, password):
self.password = password
self.port = port

def run(self, timeout, _):
def run(self, options):
with closing(psycopg2.connect(host=self.host, database=self.database, user=self.user, password=self.password,
port=self.port, connect_timeout=timeout)) as conn:
port=self.port, connect_timeout=options.check_timeout)) as conn:
# create a cursor
with closing(conn.cursor()) as cur:
start = time()
cur.execute('SELECT version()')
db_version = cur.fetchone()

def is_fatal_error(self, error):
representation = repr(error)
if 'password authentication failed' in representation or\
'database "{0}" does not exist'.format(self.database) in representation:
return True

def __str__(self):
return 'postgresql://{0.user}:{0.password}:{0.host}:{0.database}'.format(self)

Expand Down

0 comments on commit 3eb65ef

Please sign in to comment.