1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-11-25 23:58:18 +01:00

Merge pull request #5 from ilri/database-error-handling

Database error handling
This commit is contained in:
Alan Orth 2018-11-11 00:07:25 +02:00 committed by GitHub
commit 40aac8bf89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Changed
- Properly handle database connection errors
### [0.7.0] - 2018-11-07 ### [0.7.0] - 2018-11-07
## Added ## Added
- Ability to configure PostgreSQL database port with DATABASE_PORT environment variable (defaults to 5432) - Ability to configure PostgreSQL database port with DATABASE_PORT environment variable (defaults to 5432)

View File

@ -72,7 +72,6 @@ The item id is the *internal* id for an item. You can get these from the standar
- Better logging - Better logging
- Tests - Tests
- Check if database exists (try/except)
- Version API - Version API
- Use JSON in PostgreSQL - Use JSON in PostgreSQL
- Switch to [Python 3.6+ f-string syntax](https://realpython.com/python-f-strings/) - Switch to [Python 3.6+ f-string syntax](https://realpython.com/python-f-strings/)

View File

@ -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):