Add extra error handling for avatar file size & large payload (#3042)

* Add extra error handling for avatar file size & large payload

* Change error message to return 'upload failure' on most errors instead of 'no file' message
This commit is contained in:
David Sevilla Martín 2021-09-05 20:43:59 -04:00 committed by GitHub
parent 1aa61f1f01
commit eb0dd1f0d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View File

@ -382,6 +382,10 @@ export default class Application {
content = app.translator.trans('core.lib.error.not_found_message');
break;
case 413:
content = app.translator.trans('core.lib.error.payload_too_large_message');
break;
case 429:
content = app.translator.trans('core.lib.error.rate_limit_exceeded_message');
break;

View File

@ -521,6 +521,7 @@ core:
generic_message: "Oops! Something went wrong. Please reload the page and try again."
missing_dependencies_message: "Cannot enable {extension} until the following dependencies are enabled: {extensions}"
not_found_message: The requested resource was not found.
payload_too_large_message: The request payload was too large.
permission_denied_message: You do not have permission to do that.
rate_limit_exceeded_message: You're going a little too quickly. Please try again in a few seconds.

View File

@ -28,6 +28,8 @@ validation:
ends_with: "The :attribute must end with one of the following: :values."
exists: "The selected :attribute is invalid."
file: "The :attribute must be a file."
file_too_large: "The :attribute is too large."
file_upload_failed: "The :attribute failed to upload."
filled: "The :attribute field must have a value."
gt:
numeric: "The :attribute must be greater than :value."

View File

@ -30,8 +30,18 @@ class AvatarValidator extends AbstractValidator
protected function assertFileRequired(UploadedFileInterface $file)
{
if ($file->getError() !== UPLOAD_ERR_OK) {
$this->raise('required');
$error = $file->getError();
if ($error !== UPLOAD_ERR_OK) {
if ($error === UPLOAD_ERR_INI_SIZE || $error === UPLOAD_ERR_FORM_SIZE) {
$this->raise('file_too_large');
}
if ($error === UPLOAD_ERR_NO_FILE) {
$this->raise('required');
}
$this->raise('file_upload_failed');
}
}