Skip to content

Commit 7aca8a2

Browse files
authored
Merge pull request #538 from DDMAL/add-img-fix
fix: handle non-JSON error responses gracefully
2 parents e3cf13f + e222e79 commit 7aca8a2

6 files changed

Lines changed: 20 additions & 7 deletions

File tree

web-app/django/VIM/apps/instruments/error_codes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ErrorCode:
4949
ErrorCode.INVALID_LANGUAGE_CODE: "One or more language codes are invalid.",
5050
ErrorCode.INVALID_HBS_CLASSIFICATION: "Valid Hornbostel-Sachs classification (at least 2 digits) is required.",
5151
ErrorCode.INVALID_IMAGE_TYPE: "Invalid image type. Allowed types: JPEG, PNG, GIF, WebP.",
52-
ErrorCode.INVALID_IMAGE_SIZE: "Image file size must be less than 5MB.",
52+
ErrorCode.INVALID_IMAGE_SIZE: "Image file size must be less than 2MB.",
5353
ErrorCode.FIELD_TOO_LONG: "One or more fields exceed the maximum allowed length.",
5454
ErrorCode.INVALID_JSON_FORMAT: "Invalid data format. Please check your request and try again.",
5555
ErrorCode.DUPLICATE_NAME_IN_REQUEST: "Duplicate entries detected in your submission.",

web-app/django/VIM/apps/instruments/utils/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def validate_image_file(image_file) -> Tuple[bool, str]:
195195

196196
# Check file size
197197
if image_file.size > settings.MAX_IMAGE_SIZE:
198-
return False, "Image file size must be less than 5MB"
198+
return False, "Image file size must be less than 2MB"
199199

200200
# Check content type
201201
content_type = image_file.content_type

web-app/django/VIM/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
# Media files (user uploads & wikidata images)
183183
MEDIA_ROOT = ROOT_DIR / "media"
184184
MEDIA_URL = "/media/"
185-
MAX_IMAGE_SIZE = 5 * 1024 * 1024 # 5MB
185+
MAX_IMAGE_SIZE = 2 * 1024 * 1024 # 2MB
186186

187187
# Default primary key field type
188188
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

web-app/django/VIM/templates/instruments/create.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ <h5>Image (Optional)</h5>
101101
<div class="form-text">
102102
Accepted formats: JPEG, PNG, GIF, WebP.
103103
<br />
104-
Max size: 5MB.
104+
Max size: 2MB.
105105
</div>
106106
<div class="valid-feedback"></div>
107107
<div class="invalid-feedback"></div>

web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,20 @@ export class CreateInstrumentManager {
383383
body: submitData,
384384
});
385385

386-
const data: CreateInstrumentResponse = await response.json();
386+
let data: CreateInstrumentResponse;
387+
try {
388+
data = await response.json();
389+
} catch {
390+
// Server returned non-JSON (e.g. 413 HTML page from an upstream proxy)
391+
const message =
392+
response.status === 413
393+
? 'The image is too large to upload. Please use an image under 2MB.'
394+
: `Server error (${response.status}). Please try again.`;
395+
this.showModalError(message);
396+
confirmBtn.disabled = false;
397+
confirmBtn.innerHTML = originalText;
398+
return;
399+
}
387400

388401
// Check HTTP status first
389402
if (!response.ok) {

web-app/frontend/src/instruments/helpers/CreateInstrumentValidator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const ALLOWED_IMAGE_TYPES = [
2121
'image/gif',
2222
'image/webp',
2323
];
24-
const MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 5MB
24+
const MAX_IMAGE_SIZE = 2 * 1024 * 1024; // 2MB
2525

2626
export class CreateInstrumentValidator {
2727
private languages: WikidataLanguage[];
@@ -194,7 +194,7 @@ export class CreateInstrumentValidator {
194194
if (file.size > MAX_IMAGE_SIZE) {
195195
return {
196196
isValid: false,
197-
message: 'Image file size must be less than 5MB',
197+
message: 'Image file size must be less than 2MB',
198198
type: 'error',
199199
};
200200
}

0 commit comments

Comments
 (0)