Skip to content
Snippets Groups Projects
Commit 2806cebb authored by Simon Künzel's avatar Simon Künzel
Browse files

Add errors for invalid route decorators

parent fefe33eb
No related branches found
No related tags found
1 merge request!6Feedback page & Misc
...@@ -135,6 +135,10 @@ class EffectiveViewPermissions(ViewPermissions): ...@@ -135,6 +135,10 @@ class EffectiveViewPermissions(ViewPermissions):
def api_moderator_route(require_csrf_token: bool = False): def api_moderator_route(require_csrf_token: bool = False):
def decorator(func): def decorator(func):
if hasattr(func, "is_api_route") and func.is_api_route:
raise Exception("@api_moderator_route() seems to be applied after @api_route(). @api_moderator_route() "
"should be the first (lowest) decorator!")
@wraps(func) @wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
if not is_moderator(): if not is_moderator():
......
...@@ -124,6 +124,9 @@ def api_add_route(path: str, methods: list[str], ...@@ -124,6 +124,9 @@ def api_add_route(path: str, methods: list[str],
min_api_version: int = API_OLDEST_ACTIVE_VERSION, min_api_version: int = API_OLDEST_ACTIVE_VERSION,
max_api_version: int = API_LATEST_VERSION): max_api_version: int = API_LATEST_VERSION):
def decorator(func): def decorator(func):
if not hasattr(func, "is_api_route") or not func.is_api_route:
raise Exception("@api_add_route() seems to be applied before @api_function()")
for version in range(min_api_version, max_api_version + 1): for version in range(min_api_version, max_api_version + 1):
full_path = get_api_path(version, path) full_path = get_api_path(version, path)
if DEBUG_ENABLED: if DEBUG_ENABLED:
...@@ -139,6 +142,10 @@ def api_function(track_in_diagnostics: bool = True, ...@@ -139,6 +142,10 @@ def api_function(track_in_diagnostics: bool = True,
allow_while_disabled: bool = False, allow_while_disabled: bool = False,
rate_limiters: tuple[HostBasedCounterRateLimiter, ...] = _API_GLOBAL_RATE_LIMITERS): rate_limiters: tuple[HostBasedCounterRateLimiter, ...] = _API_GLOBAL_RATE_LIMITERS):
def decorator(func): def decorator(func):
if hasattr(func, "is_api_route") and func.is_api_route:
raise Exception("An @api_function() decorator has already been applied. Are you using multiple @api_route()? "
"Use @api_add_route(...)@api_add_route(..)@api_function() instead")
call_counter = None call_counter = None
if track_in_diagnostics: if track_in_diagnostics:
func_name: str = func.__name__ func_name: str = func.__name__
...@@ -199,6 +206,7 @@ def api_function(track_in_diagnostics: bool = True, ...@@ -199,6 +206,7 @@ def api_function(track_in_diagnostics: bool = True,
traceback.print_exception(e) traceback.print_exception(e)
return api_on_error(ERROR_INTERNAL_SERVER_ERROR) return api_on_error(ERROR_INTERNAL_SERVER_ERROR)
wrapper.is_api_route = True
return wrapper return wrapper
return decorator return decorator
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment