Commit 22badde9 authored by sim's avatar sim

Database errors reporting at REST level

parent 22e2502b
from .celery_app import app as celery_app
from . import exceptions # Need to import this here to activate error handling
"""
This module handles automatic conversion between SQLAlchemy's exceptions and
Django REST framework's APIException.
It mimics behavior of PostgREST, hereunder is the conversion table between
PostgreSQL errors and HTTP statuses.
See https://postgrest.com/en/v4.3/api.html#http-status-codes
HTTP 400 → Bad Request
HTTP 401 → Unauthorized
HTTP 403 → Forbidden
HTTP 404 → Not Found
HTTP 409 → Conflict
HTTP 413 → Payload Too Large
HTTP 500 → Server Internal Error
HTTP 503 → Service Unavailable
Error code(s) | HTTP status | Error description
==============|===============================|================================
08* | 503 | pg connection err
09* | 500 | triggered action exception
0L* | 403 | invalid grantor
0P* | 403 | invalid role specification
23503 | 409 | foreign key violation
23505 | 409 | uniqueness violation
25* | 500 | invalid transaction state
28* | 403 | invalid auth specification
2D* | 500 | invalid transaction termination
38* | 500 | external routine exception
39* | 500 | external routine invocation
3B* | 500 | savepoint exception
40* | 500 | transaction rollback
53* | 503 | insufficient resources
54* | 413 | too complex
55* | 500 | obj not in prerequisite state
57* | 500 | operator intervention
58* | 500 | system error
F0* | 500 | conf file error
HV* | 500 | foreign data wrapper error
P0001 | 400 | default code for “raise”
P0* | 500 | PL/pgSQL error
XX* | 500 | internal error
42883 | 404 | undefined function
42P01 | 404 | undefined table
42501 | if authenticated 403 else 401 | insufficient privileges
other | 500 |
"""
import logging
from sqlalchemy import event
from rest_framework.exceptions import APIException
from gargantext.core.db import protected_engine
logger = logging.getLogger('gargantext.backend')
PGERROR_TO_HTTPSTATUS = {
'08' : 503,
'09' : 500,
'0L' : 403,
'0P' : 403,
'23503': 409,
'23505': 409,
'25' : 500,
'28' : 403,
'2D' : 500,
'38' : 500,
'39' : 500,
'3B' : 500,
'40' : 500,
'53' : 503,
'54' : 413,
'55' : 500,
'57' : 500,
'58' : 500,
'F0' : 500,
'HV' : 500,
'P0001': 400,
'P0' : 500,
'XX' : 500,
'42883': 404,
'42P01': 404,
'42501': lambda c: 403 if c.info.get('authenticated') else 401
}
class DatabaseError(APIException):
"""Represent any gargantext database exception.
Intended to be used in with SQLAlchemy's `handler_error` signal, to convert
any database error to a local one, and ease database error handling.
Arguments:
conn -- SQLAlchemy connection
orig_exc -- SQLALchemy original exception
"""
def __init__(self, conn, orig_exc):
# Error code
code = orig_exc.pgcode or '??'
# Whole error message
error = str(orig_exc.pgerror or \
(orig_exc.args and orig_exc.args[0]) or '')
# XXX PostgREST takes care of each error part (MESSAGE, DETAIL, HINT,
# ERRCODE), we don't replicate this behavior.
# See https://www.postgresql.org/docs/9.3/static/plpgsql-errors-and-messages.html
first_line = error.split('\n', 2)[0]
parts = first_line.split(':', 2)
# Extract a meaningful message
message = parts[1 if len(parts) > 1 else 0].strip()
# Search error code in PGERROR_TO_HTTPSTATUS:
# * Try first with two first chars of the error code
# * Then try with the entire code
status = PGERROR_TO_HTTPSTATUS.get(
code[:1],
PGERROR_TO_HTTPSTATUS.get(code, 500))
if callable(status):
status = status(conn)
self.status_code = status
super().__init__(code=code, detail=message)
self.args = (orig_exc,)
# See http://docs.sqlalchemy.org/en/latest/core/events.html#sqlalchemy.events.ConnectionEvents.handle_error
@event.listens_for(protected_engine, 'handle_error', named=True)
def receive_handle_error(exception_context):
exc = DatabaseError(
exception_context.connection, exception_context.original_exception)
logger.exception(exc)
raise exc
def base_exception_handler(exc, context):
from rest_framework.views import exception_handler as _exception_handler
response = _exception_handler(exc, context)
session = context['request'].db
session.rollback()
return response
from .exceptions import base_exception_handler
def exception_handler(exc, context):
response = base_exception_handler(exc, context)
if response is not None:
code = getattr(response.data.get('detail'), 'code', None)
if code:
response.data['code'] = code
response.data['status_code'] = response.status_code
return response
......@@ -228,6 +228,7 @@ REST_FRAMEWORK = {
'gargantext.backend.authentication.JWTAuthentication',
'gargantext.backend.authentication.AnonymousAuthentication',
),
'EXCEPTION_HANDLER': 'gargantext.backend.views.exception_handler',
}
# See http://getblimp.github.io/django-rest-framework-jwt/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment