Bring catch_etiquette_exception into common.
The decorators file only had a single function in it.
This commit is contained in:
		
							parent
							
								
									f7c51f394d
								
							
						
					
					
						commit
						e6f5d16a03
					
				
					 3 changed files with 31 additions and 41 deletions
				
			
		|  | @ -1,5 +1,4 @@ | |||
| from . import common | ||||
| from . import decorators | ||||
| from . import endpoints | ||||
| from . import sessions | ||||
| 
 | ||||
|  | @ -7,7 +6,6 @@ site = common.site | |||
| 
 | ||||
| __all__ = [ | ||||
|     'common', | ||||
|     'decorators', | ||||
|     'endpoints', | ||||
|     'sessions', | ||||
|     'site', | ||||
|  |  | |||
|  | @ -14,7 +14,6 @@ from voussoirkit import pathclass | |||
| import etiquette | ||||
| 
 | ||||
| from . import client_caching | ||||
| from . import decorators | ||||
| from . import jinja_filters | ||||
| from . import sessions | ||||
| 
 | ||||
|  | @ -53,19 +52,23 @@ file_etag_manager = client_caching.FileEtagManager( | |||
| 
 | ||||
| # Response wrappers ################################################################################ | ||||
| 
 | ||||
| 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', | ||||
|         ), | ||||
|         decorators.catch_etiquette_exception, | ||||
|         session_manager.give_token | ||||
|     ], | ||||
| ) | ||||
| def catch_etiquette_exception(endpoint): | ||||
|     ''' | ||||
|     If an EtiquetteException is raised, automatically catch it and convert it | ||||
|     into a json response so that the user doesn't receive error 500. | ||||
|     ''' | ||||
|     @functools.wraps(endpoint) | ||||
|     def wrapped(*args, **kwargs): | ||||
|         try: | ||||
|             return endpoint(*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 | ||||
| 
 | ||||
| @site.before_request | ||||
| def before_request(): | ||||
|  | @ -82,6 +85,20 @@ def after_request(response): | |||
|     response = flasktools.gzip_response(request, 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 ###################################################################################### | ||||
| 
 | ||||
| def P_wrapper(function): | ||||
|  |  | |||
|  | @ -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 | ||||
		Loading…
	
		Reference in a new issue