Skip to content
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
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@ baselayer_doc_reqs:

baselayer_html: | baselayer_doc_reqs
export SPHINXOPTS=-W; make -C doc html

db: ## Connect to the database with psql
db:
@PYTHONPATH=. python ./baselayer/tools/db_connect.py
15 changes: 10 additions & 5 deletions app/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,16 @@ def write_error(self, status_code, exc_info=None):
self.render("loginerror.html", app=cfg["app"], error_message=str(err))

def log_exception(self, typ=None, value=None, tb=None):
if "Authentication Error:" in str(value):
log(value)
expected_exceptions = [
"Authentication Error:",
"User account expired",
"Credentials malformed",
"Method Not Allowed",
"Unauthorized"
]
v_str = str(value)
if any(exception in v_str for exception in expected_exceptions):
log(f"Error response returned by [{self.request.path}]: [{v_str}]")
else:
app_log.error(
"Uncaught exception %s\n%r",
Expand Down Expand Up @@ -305,9 +313,6 @@ def error(self, message, data={}, status=400, extra={}):
extra : dict
Extra fields to be included in the response.
"""
if not (status == 404 and self.request.method == "HEAD"):
log(f"Error response returned by [{self.request.path}]: [{message}]")

self.set_header("Content-Type", "application/json")
self.set_status(status)
self.write({"status": "error", "message": message, "data": data, **extra})
Expand Down
32 changes: 32 additions & 0 deletions tools/db_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
import os
import sys

from baselayer.app.env import load_env

env, cfg = load_env()
db = cfg["database.database"]

user = cfg["database.user"] or db
host = cfg["database.host"]
port = cfg["database.port"]
password = cfg["database.password"]

flags = f"-U {user}"

if password:
psql_cmd = f'PGPASSWORD="{password}" {psql_cmd}'
flags += " --no-password"

if host:
flags += f" -h {host}"

if port:
flags += f" -p {port}"

cmd = f'psql {flags}'.split()
return_code = os.spawnvpe(os.P_WAIT, cmd[0], cmd, os.environ)
if return_code == 127:
sys.stderr.write(f'{cmd[0]}: command not found\n')

sys.exit(return_code)