diff --git a/api_specification.md b/api_specification.md index 07f2b9d5f5f57df4461e963e83ec38406ef42d35..1d982828c1d410880afef143ddd8108a45f386ba 100644 --- a/api_specification.md +++ b/api_specification.md @@ -1,4 +1,4 @@ -# Specification of the Web API for the Video-AG Website (v0.51). +# Specification of the Web API for the Video-AG Website (v0.52). ## Introduction @@ -1518,9 +1518,14 @@ Possible `error_code`: | too_many_suggestions | 403 | | | site_is_readonly | 503 | | | site_is_disabled | 503 | | +| site_is_overloaded | 503 | | ## Changelog +### v0.52 + +* Added error code `site_is_overloaded` + ### v0.51 * Added `server_name` to `GET /status` diff --git a/src/api/miscellaneous/__init__.py b/src/api/miscellaneous/__init__.py index 7374751c6b460918fc0c067e949f84eae4c9d1d8..ed3dc292672dbce03d8a9cf39db71c9c8d1b50dd 100644 --- a/src/api/miscellaneous/__init__.py +++ b/src/api/miscellaneous/__init__.py @@ -25,7 +25,8 @@ from api.miscellaneous.errors import (ApiError, ApiClientException, api_on_error ERROR_MODIFICATION_UNEXPECTED_CURRENT_VALUE, ERROR_TOO_MANY_SUGGESTIONS, ERROR_SITE_IS_READONLY, - ERROR_SITE_IS_DISABLED) + ERROR_SITE_IS_DISABLED, + ERROR_SITE_IS_OVERLOADED) from api.miscellaneous.rate_limiter import IntervalRateLimiter, HostBasedCounterRateLimiter, create_configured_host_rate_limiters from api.miscellaneous.diagnostics import DIAGNOSTICS_TRACKER, DiagnosticsCounter, DiagnosticsDataProvider from api.miscellaneous.scheduler import scheduled_function diff --git a/src/api/miscellaneous/errors.py b/src/api/miscellaneous/errors.py index f88be8cd508020d7af52e9e7c7d35392676c13c7..d9e9556e9086ea6f22601f66724289abb491d3c1 100644 --- a/src/api/miscellaneous/errors.py +++ b/src/api/miscellaneous/errors.py @@ -83,6 +83,8 @@ ERROR_SITE_IS_READONLY = ApiError("site_is_readonly", HTTP_503_SERVICE_UNAVAILAB "The site is currently in read-only mode") ERROR_SITE_IS_DISABLED = ApiError("site_is_disabled", HTTP_503_SERVICE_UNAVAILABLE, "The site is currently disabled") +ERROR_SITE_IS_OVERLOADED = ApiError("site_is_overloaded", HTTP_503_SERVICE_UNAVAILABLE, + "Your request failed, as the site is currently experiencing a lot of traffic") ALL_ERRORS_RANDOM = [ ERROR_BAD_REQUEST(), @@ -103,5 +105,6 @@ ALL_ERRORS_RANDOM = [ ERROR_OBJECT_ERROR("?"), ERROR_TOO_MANY_SUGGESTIONS, ERROR_SITE_IS_READONLY, - ERROR_SITE_IS_DISABLED + ERROR_SITE_IS_DISABLED, + ERROR_SITE_IS_OVERLOADED ] diff --git a/src/api/routes/route.py b/src/api/routes/route.py index 122ebac1fde006c2b4664785f91af15b33eedd41..b9df1835a6d2c27fc7d07017d0534579ebc4c578 100644 --- a/src/api/routes/route.py +++ b/src/api/routes/route.py @@ -5,6 +5,7 @@ from re import Pattern import api from api.authentication import is_moderator +from api.database.database import TransactionConflictError, NoAvailableConnectionError from api.miscellaneous import * from api.version import get_api_path, API_LATEST_VERSION, API_OLDEST_ACTIVE_VERSION @@ -189,8 +190,12 @@ def api_function(track_in_diagnostics: bool = True, raise Exception(f"Api route {truncate_string(request.path)} returned result of unknown type: {str(result)}") except ApiClientException as e: return api_on_error(e.error) + except (TransactionConflictError, NoAvailableConnectionError) as e: + print(f"An transaction conflict occurred while handling api request '{truncate_string(request.path, 200)}':") + traceback.print_exception(e) + return api_on_error(ERROR_SITE_IS_OVERLOADED) except Exception as e: - print("An exception occurred while handling api request:") + print(f"An exception occurred while handling api request '{truncate_string(request.path, 200)}':") traceback.print_exception(e) return api_on_error(ERROR_INTERNAL_SERVER_ERROR)