From 4a23a24f6291c218c48d1050cd90f311fbc21cf1 Mon Sep 17 00:00:00 2001 From: Yinan Zhou Date: Thu, 19 Mar 2026 16:01:19 -0400 Subject: [PATCH 1/2] fix: handle non-JSON error responses gracefully refs: #534 --- .../helpers/CreateInstrumentManager.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts b/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts index 5b1e51e7..3632ef6b 100644 --- a/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts +++ b/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts @@ -383,7 +383,20 @@ export class CreateInstrumentManager { body: submitData, }); - const data: CreateInstrumentResponse = await response.json(); + let data: CreateInstrumentResponse; + try { + data = await response.json(); + } catch { + // Server returned non-JSON (e.g. 413 HTML page from an upstream proxy) + const message = + response.status === 413 + ? 'The image is too large to upload. Please use an image under 5MB.' + : `Server error (${response.status}). Please try again.`; + this.showModalError(message); + confirmBtn.disabled = false; + confirmBtn.innerHTML = originalText; + return; + } // Check HTTP status first if (!response.ok) { From e222e79c4ad493bc08786a5911eda2726c28ad05 Mon Sep 17 00:00:00 2001 From: Yinan Zhou Date: Fri, 20 Mar 2026 15:38:10 -0400 Subject: [PATCH 2/2] fix: change image size limit from 5MB to 2MB refs: #534 --- web-app/django/VIM/apps/instruments/error_codes.py | 2 +- web-app/django/VIM/apps/instruments/utils/validators.py | 2 +- web-app/django/VIM/settings.py | 2 +- web-app/django/VIM/templates/instruments/create.html | 2 +- .../src/instruments/helpers/CreateInstrumentManager.ts | 2 +- .../src/instruments/helpers/CreateInstrumentValidator.ts | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/web-app/django/VIM/apps/instruments/error_codes.py b/web-app/django/VIM/apps/instruments/error_codes.py index 95738200..a237f299 100644 --- a/web-app/django/VIM/apps/instruments/error_codes.py +++ b/web-app/django/VIM/apps/instruments/error_codes.py @@ -49,7 +49,7 @@ class ErrorCode: ErrorCode.INVALID_LANGUAGE_CODE: "One or more language codes are invalid.", ErrorCode.INVALID_HBS_CLASSIFICATION: "Valid Hornbostel-Sachs classification (at least 2 digits) is required.", ErrorCode.INVALID_IMAGE_TYPE: "Invalid image type. Allowed types: JPEG, PNG, GIF, WebP.", - ErrorCode.INVALID_IMAGE_SIZE: "Image file size must be less than 5MB.", + ErrorCode.INVALID_IMAGE_SIZE: "Image file size must be less than 2MB.", ErrorCode.FIELD_TOO_LONG: "One or more fields exceed the maximum allowed length.", ErrorCode.INVALID_JSON_FORMAT: "Invalid data format. Please check your request and try again.", ErrorCode.DUPLICATE_NAME_IN_REQUEST: "Duplicate entries detected in your submission.", diff --git a/web-app/django/VIM/apps/instruments/utils/validators.py b/web-app/django/VIM/apps/instruments/utils/validators.py index 130e4685..3641a243 100644 --- a/web-app/django/VIM/apps/instruments/utils/validators.py +++ b/web-app/django/VIM/apps/instruments/utils/validators.py @@ -195,7 +195,7 @@ def validate_image_file(image_file) -> Tuple[bool, str]: # Check file size if image_file.size > settings.MAX_IMAGE_SIZE: - return False, "Image file size must be less than 5MB" + return False, "Image file size must be less than 2MB" # Check content type content_type = image_file.content_type diff --git a/web-app/django/VIM/settings.py b/web-app/django/VIM/settings.py index fdddcf27..d9868793 100644 --- a/web-app/django/VIM/settings.py +++ b/web-app/django/VIM/settings.py @@ -182,7 +182,7 @@ # Media files (user uploads & wikidata images) MEDIA_ROOT = ROOT_DIR / "media" MEDIA_URL = "/media/" -MAX_IMAGE_SIZE = 5 * 1024 * 1024 # 5MB +MAX_IMAGE_SIZE = 2 * 1024 * 1024 # 2MB # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/web-app/django/VIM/templates/instruments/create.html b/web-app/django/VIM/templates/instruments/create.html index e070ae3b..ee9673a5 100644 --- a/web-app/django/VIM/templates/instruments/create.html +++ b/web-app/django/VIM/templates/instruments/create.html @@ -101,7 +101,7 @@
Image (Optional)
Accepted formats: JPEG, PNG, GIF, WebP.
- Max size: 5MB. + Max size: 2MB.
diff --git a/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts b/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts index 3632ef6b..c5411491 100644 --- a/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts +++ b/web-app/frontend/src/instruments/helpers/CreateInstrumentManager.ts @@ -390,7 +390,7 @@ export class CreateInstrumentManager { // Server returned non-JSON (e.g. 413 HTML page from an upstream proxy) const message = response.status === 413 - ? 'The image is too large to upload. Please use an image under 5MB.' + ? 'The image is too large to upload. Please use an image under 2MB.' : `Server error (${response.status}). Please try again.`; this.showModalError(message); confirmBtn.disabled = false; diff --git a/web-app/frontend/src/instruments/helpers/CreateInstrumentValidator.ts b/web-app/frontend/src/instruments/helpers/CreateInstrumentValidator.ts index 6534562b..f22860e1 100644 --- a/web-app/frontend/src/instruments/helpers/CreateInstrumentValidator.ts +++ b/web-app/frontend/src/instruments/helpers/CreateInstrumentValidator.ts @@ -21,7 +21,7 @@ const ALLOWED_IMAGE_TYPES = [ 'image/gif', 'image/webp', ]; -const MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 5MB +const MAX_IMAGE_SIZE = 2 * 1024 * 1024; // 2MB export class CreateInstrumentValidator { private languages: WikidataLanguage[]; @@ -194,7 +194,7 @@ export class CreateInstrumentValidator { if (file.size > MAX_IMAGE_SIZE) { return { isValid: false, - message: 'Image file size must be less than 5MB', + message: 'Image file size must be less than 2MB', type: 'error', }; }