mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-26 08:08:19 +01:00
dspace_statistics_api/database.py: Raise HTTP 500 on error
Properly except on database connection error and raise an HTTP 500 instead of spamming the console/log with twenty lines of text.
This commit is contained in:
parent
4c51d12eb4
commit
d5d2d2149b
@ -3,6 +3,7 @@ from .config import DATABASE_USER
|
|||||||
from .config import DATABASE_PASS
|
from .config import DATABASE_PASS
|
||||||
from .config import DATABASE_HOST
|
from .config import DATABASE_HOST
|
||||||
from .config import DATABASE_PORT
|
from .config import DATABASE_PORT
|
||||||
|
import falcon
|
||||||
import psycopg2
|
import psycopg2
|
||||||
import psycopg2.extras
|
import psycopg2.extras
|
||||||
|
|
||||||
@ -14,7 +15,13 @@ class DatabaseManager():
|
|||||||
self._connection_uri = 'dbname={} user={} password={} host={} port={}'.format(DATABASE_NAME, DATABASE_USER, DATABASE_PASS, DATABASE_HOST, DATABASE_PORT)
|
self._connection_uri = 'dbname={} user={} password={} host={} port={}'.format(DATABASE_NAME, DATABASE_USER, DATABASE_PASS, DATABASE_HOST, DATABASE_PORT)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self._connection = psycopg2.connect(self._connection_uri, cursor_factory=psycopg2.extras.DictCursor)
|
try:
|
||||||
|
self._connection = psycopg2.connect(self._connection_uri, cursor_factory=psycopg2.extras.DictCursor)
|
||||||
|
except psycopg2.OperationalError:
|
||||||
|
title = '500 Internal Server Error'
|
||||||
|
description = 'Could not connect to database'
|
||||||
|
raise falcon.HTTPInternalServerError(title, description)
|
||||||
|
|
||||||
return self._connection
|
return self._connection
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, exc_traceback):
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
||||||
|
Loading…
Reference in New Issue
Block a user