finish up db rollback checks

- move catch_db_error to utils
- tidy catch/propagate errors in prepare, get_current_user
This commit is contained in:
Min RK
2021-08-10 15:03:41 +02:00
parent 044fb23a70
commit 3bcc542e27
3 changed files with 32 additions and 31 deletions

View File

@@ -4,9 +4,9 @@
import asyncio
import concurrent.futures
import errno
import functools
import hashlib
import inspect
import os
import random
import secrets
import socket
@@ -22,6 +22,7 @@ from hmac import compare_digest
from operator import itemgetter
from async_generator import aclosing
from sqlalchemy.exc import SQLAlchemyError
from tornado import ioloop
from tornado import web
from tornado.httpclient import AsyncHTTPClient
@@ -635,3 +636,21 @@ def get_accepted_mimetype(accept_header, choices=None):
else:
return mime
return None
def catch_db_error(f):
"""Catch and rollback database errors"""
@functools.wraps(f)
async def catching(self, *args, **kwargs):
try:
r = f(self, *args, **kwargs)
if inspect.isawaitable(r):
r = await r
except SQLAlchemyError:
self.log.exception("Rolling back session due to database error")
self.db.rollback()
else:
return r
return catching