[modules | media] Fix media upload error handling for non-JSON server responses#10700
Conversation
The media upload flow assumed every error response was JSON and attempted to parse the response unconditionally. When nginx rejected oversized uploads with an HTML 413 response, the browser threw an Unexpected token '<' exception and the upload error handling failed. This change wraps JSON parsing in defensive try/catch handling so HTML or other non-JSON responses fall back to the default error message instead of crashing the frontend.
skarya22
left a comment
There was a problem hiding this comment.
LGTM! I tried to upload a 3G file on Chrome on 29.0-release and it froze, but when I switched to this branch and re-compiled I got a File Too Large error. The problem was not present on Firefox.
| try { | ||
| msg = JSON.parse(xhr.response).message; | ||
| } catch (e) { | ||
| // Ignore non-JSON responses. |
There was a problem hiding this comment.
should we console.warn the error if it's not?
There was a problem hiding this comment.
Hello @driusan, the msg was already initialized at line 346, so ideally we do not need to do anything inside catch block. But if you suggest we can do something like this console.warn('Upload response was not valid JSON.'); for developers
There was a problem hiding this comment.
I meant something like:
console.warn('Failed upload response was not valid JSON', e);
so that the error isn't lost silently in case it was significant. The try / catch is catching all errors.
… responses (aces#10700) Uploading a file larger than the configured limit in the LORIS Media module caused the browser to freeze and show JavaScript stack traces. The initial assumption was that the backend was failing, but the actual failure mode was in the frontend response handling. The frontend should not freeze or crash when the server returns HTML instead of JSON. Oversized uploads now fall back to the default error message. Fixes aces#10659
closes #10659
Situation
Uploading a file larger than the configured limit in the LORIS Media module caused the browser to freeze and show JavaScript stack traces. The initial assumption was that the backend was failing, but the actual failure mode was in the frontend response handling.
Task
Identify the root cause of the upload failure, verify it against the nginx-based VM behavior, and update the upload flow so it can safely handle non-JSON error responses.
Action
I reproduced the issue on the test VM, confirmed that oversized uploads were rejected by nginx with an HTML 413 response as shown in the screenshot, and traced the crash to unconditional

JSON.parse()calls in the upload handler. I updated the logic to parse responses defensively with `try/catch.Result
The frontend should not freeze or crash when the server returns HTML instead of JSON. Oversized uploads now fall back to the default error message.
Testing Instructions for a VM with nginx
413 Request Entity Too Largeand that the response body is HTML.Unexpected token '<'in the console.