1414from google .auth .transport .requests import Request
1515from google .cloud import storage
1616from django .conf import settings
17- from rest_framework .exceptions import UnsupportedMediaType
17+ from rest_framework import status as drf_status
18+ from rest_framework .exceptions import (
19+ APIException ,
20+ UnsupportedMediaType ,
21+ )
22+
23+
24+ class FileTooLarge (APIException ):
25+ status_code = drf_status .HTTP_413_REQUEST_ENTITY_TOO_LARGE
26+ default_detail = "File exceeds maximum allowed size."
27+ default_code = "file_too_large"
28+
1829
1930logger = logging .getLogger (__name__ )
2031
@@ -41,10 +52,8 @@ def upload_original(
4152
4253 Raises:
4354 UnsupportedMediaType: content-type not in allowlist.
44- rest_framework.exceptions.ValidationError : file too large .
55+ FileTooLarge : file exceeds IMAGE_MAX_BYTES .
4556 """
46- from rest_framework .exceptions import ValidationError
47-
4857 content_type = getattr (
4958 django_uploaded_file , "content_type" , ""
5059 ) or ""
@@ -64,14 +73,9 @@ def upload_original(
6473 django_uploaded_file .seek (0 )
6574
6675 if size_bytes > max_bytes :
67- raise ValidationError (
68- {
69- "file" : (
70- f"File too large ({ size_bytes } bytes). "
71- f"Maximum allowed: { max_bytes } bytes."
72- )
73- },
74- code = "file_too_large" ,
76+ raise FileTooLarge (
77+ f"File too large ({ size_bytes } bytes). "
78+ f"Maximum allowed: { max_bytes } bytes."
7579 )
7680
7781 ext = _ALLOWED_EXTENSIONS [content_type ]
0 commit comments