[WIKI-730] chore: handle body too large error (#7963)
* chore: added middleware to handle body too large * chore: added middleware to handle body too large * chore: indentend the code * chore: changed the response structure * chore: changed the response structure * chore: created a new file for middleware * chore: added a standardized error key
This commit is contained in:
parent
a3019ebd46
commit
606e34ec81
3 changed files with 28 additions and 1 deletions
|
|
@ -12,7 +12,6 @@ from rest_framework.request import Request
|
|||
from plane.utils.ip_address import get_client_ip
|
||||
from plane.db.models import APIActivityLog
|
||||
|
||||
|
||||
api_logger = logging.getLogger("plane.api.request")
|
||||
|
||||
|
||||
|
|
|
|||
27
apps/api/plane/middleware/request_body_size.py
Normal file
27
apps/api/plane/middleware/request_body_size.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from django.core.exceptions import RequestDataTooBig
|
||||
from django.http import JsonResponse
|
||||
|
||||
|
||||
class RequestBodySizeLimitMiddleware:
|
||||
"""
|
||||
Middleware to catch RequestDataTooBig exceptions and return
|
||||
413 Request Entity Too Large instead of 400 Bad Request.
|
||||
"""
|
||||
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
try:
|
||||
_ = request.body
|
||||
except RequestDataTooBig:
|
||||
return JsonResponse(
|
||||
{
|
||||
"error": "REQUEST_BODY_TOO_LARGE",
|
||||
"detail": "The size of the request body exceeds the maximum allowed size.",
|
||||
},
|
||||
status=413,
|
||||
)
|
||||
|
||||
# If body size is OK, continue with the request
|
||||
return self.get_response(request)
|
||||
|
|
@ -62,6 +62,7 @@ MIDDLEWARE = [
|
|||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
"crum.CurrentRequestUserMiddleware",
|
||||
"django.middleware.gzip.GZipMiddleware",
|
||||
"plane.middleware.request_body_size.RequestBodySizeLimitMiddleware",
|
||||
"plane.middleware.logger.APITokenLogMiddleware",
|
||||
"plane.middleware.logger.RequestLoggerMiddleware",
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue