Bring catch_etiquette_exception into common.

The decorators file only had a single function in it.
This commit is contained in:
voussoir 2022-03-23 14:58:41 -07:00
parent f7c51f394d
commit e6f5d16a03
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
3 changed files with 31 additions and 41 deletions

View file

@ -1,5 +1,4 @@
from . import common from . import common
from . import decorators
from . import endpoints from . import endpoints
from . import sessions from . import sessions
@ -7,7 +6,6 @@ site = common.site
__all__ = [ __all__ = [
'common', 'common',
'decorators',
'endpoints', 'endpoints',
'sessions', 'sessions',
'site', 'site',

View file

@ -14,7 +14,6 @@ from voussoirkit import pathclass
import etiquette import etiquette
from . import client_caching from . import client_caching
from . import decorators
from . import jinja_filters from . import jinja_filters
from . import sessions from . import sessions
@ -53,19 +52,23 @@ file_etag_manager = client_caching.FileEtagManager(
# Response wrappers ################################################################################ # Response wrappers ################################################################################
site.route = flasktools.decorate_and_route( def catch_etiquette_exception(endpoint):
flask_app=site, '''
decorators=[ If an EtiquetteException is raised, automatically catch it and convert it
flasktools.ensure_response_type, into a json response so that the user doesn't receive error 500.
functools.partial( '''
flasktools.give_theme_cookie, @functools.wraps(endpoint)
cookie_name='etiquette_theme', def wrapped(*args, **kwargs):
default_theme='slate', try:
), return endpoint(*args, **kwargs)
decorators.catch_etiquette_exception, except etiquette.exceptions.EtiquetteException as exc:
session_manager.give_token if isinstance(exc, etiquette.exceptions.NoSuch):
], status = 404
) else:
status = 400
response = flasktools.json_response(exc.jsonify(), status=status)
flask.abort(response)
return wrapped
@site.before_request @site.before_request
def before_request(): def before_request():
@ -82,6 +85,20 @@ def after_request(response):
response = flasktools.gzip_response(request, response) response = flasktools.gzip_response(request, response)
return response return response
site.route = flasktools.decorate_and_route(
flask_app=site,
decorators=[
flasktools.ensure_response_type,
functools.partial(
flasktools.give_theme_cookie,
cookie_name='etiquette_theme',
default_theme='slate',
),
catch_etiquette_exception,
session_manager.give_token
],
)
# P functions ###################################################################################### # P functions ######################################################################################
def P_wrapper(function): def P_wrapper(function):

View file

@ -1,25 +0,0 @@
import flask; from flask import request
import functools
import werkzeug.datastructures
from voussoirkit import flasktools
import etiquette
def catch_etiquette_exception(function):
'''
If an EtiquetteException is raised, automatically catch it and convert it
into a json response so that the user isn't receiving error 500.
'''
@functools.wraps(function)
def wrapped(*args, **kwargs):
try:
return function(*args, **kwargs)
except etiquette.exceptions.EtiquetteException as exc:
if isinstance(exc, etiquette.exceptions.NoSuch):
status = 404
else:
status = 400
response = flasktools.json_response(exc.jsonify(), status=status)
flask.abort(response)
return wrapped