Skip to content

Commit

Permalink
Catch connection error from the CLI entry point
Browse files Browse the repository at this point in the history
This avoids showing a traceback to the user. To do so, we simply let the
OperationalError propagate from data.pg_connect(), eventually catch it in
cli.main() and exit with a nice error message.
  • Loading branch information
dlax committed Jul 6, 2023
1 parent 7072914 commit 5dfd4f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* The *rollback ration* is now displayed in the "global" header (#385).

### Fixed

* At startup, do not show a traceback upon failure to connect to PostgreSQL.

### Misc

* Document how to *hack* on pg\_activity in the `README`.
Expand Down
15 changes: 9 additions & 6 deletions pgactivity/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,15 @@ def main() -> None:
except ConfigurationError as e:
parser.error(str(e))

dataobj = data.pg_connect(
args,
args.connection_string,
min_duration=args.minduration,
filters=filters,
)
try:
dataobj = data.pg_connect(
args,
args.connection_string,
min_duration=args.minduration,
filters=filters,
)
except OperationalError as e:
parser.exit(status=1, message=f"could not connect to PostgreSQL: {e}")
hostname = socket.gethostname()
conninfo = dataobj.pg_conn.info
host = types.Host(
Expand Down
2 changes: 1 addition & 1 deletion pgactivity/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def pg_connect(
msg = str(err).replace("FATAL:", "")
raise SystemExit("pg_activity: FATAL: %s" % clean_str(msg))
else:
raise Exception("Could not connect to PostgreSQL")
raise
except pg.ProgrammingError as err:
errmsg = str(err).strip()
if errmsg.startswith("invalid dsn"):
Expand Down

0 comments on commit 5dfd4f0

Please sign in to comment.