From 2806cebb6df4929f40efe9ff5a770f1b51971454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de> Date: Tue, 18 Jun 2024 23:10:20 +0200 Subject: [PATCH] Add errors for invalid route decorators --- src/api/authentication.py | 4 ++++ src/api/routes/route.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/api/authentication.py b/src/api/authentication.py index e166511..3d6e497 100644 --- a/src/api/authentication.py +++ b/src/api/authentication.py @@ -135,6 +135,10 @@ class EffectiveViewPermissions(ViewPermissions): def api_moderator_route(require_csrf_token: bool = False): 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) def wrapper(*args, **kwargs): if not is_moderator(): diff --git a/src/api/routes/route.py b/src/api/routes/route.py index b9df183..d65ca04 100644 --- a/src/api/routes/route.py +++ b/src/api/routes/route.py @@ -124,6 +124,9 @@ def api_add_route(path: str, methods: list[str], min_api_version: int = API_OLDEST_ACTIVE_VERSION, max_api_version: int = API_LATEST_VERSION): 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): full_path = get_api_path(version, path) if DEBUG_ENABLED: @@ -139,6 +142,10 @@ def api_function(track_in_diagnostics: bool = True, allow_while_disabled: bool = False, rate_limiters: tuple[HostBasedCounterRateLimiter, ...] = _API_GLOBAL_RATE_LIMITERS): 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 if track_in_diagnostics: func_name: str = func.__name__ @@ -199,6 +206,7 @@ def api_function(track_in_diagnostics: bool = True, traceback.print_exception(e) return api_on_error(ERROR_INTERNAL_SERVER_ERROR) + wrapper.is_api_route = True return wrapper return decorator -- GitLab