Commit a138ed3d authored by Primsi's avatar Primsi
Browse files

Issue #2554727 by Primsi: Provide better feedback about upload errors

parent 17131ad7
......@@ -167,8 +167,6 @@ class UploadController extends ControllerBase {
*
* @throws \Drupal\dropzonejs\UploadException
* @throws Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*
* @todo Handle all the possible upload errors. See file_save_upload().
*/
protected function handleUpload() {
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
......@@ -176,8 +174,28 @@ class UploadController extends ControllerBase {
if (!$file instanceof UploadedFile) {
throw new AccessDeniedHttpException();
}
elseif ($file->getError() != UPLOAD_ERR_OK) {
throw new UploadException(UploadException::FILE_UPLOAD_ERROR);
elseif ($error = $file->getError() && $error != UPLOAD_ERR_OK) {
// Check for file upload errors and return FALSE for this file if a lower
// level system error occurred. For a complete list of errors:
// See http://php.net/manual/features.file-upload.errors.php.
switch ($error) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$message = t('The file could not be saved because it exceeds the maximum allowed size for uploads.');
continue;
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
$message = t('The file could not be saved because the upload did not complete.');
continue;
// Unknown error.
default:
$message = t('The file could not be saved. An unknown error has occurred.');
continue;
}
throw new UploadException(UploadException::FILE_UPLOAD_ERROR, $message);
}
// Open temp file.
......
......@@ -50,7 +50,7 @@ class UploadException extends \Exception {
self::INPUT_ERROR => 'Failed to open input stream.',
self::OUTPUT_ERROR => 'Failed to open output stream.',
self::MOVE_ERROR => 'Failed to move uploaded file.',
self::DESTINATION_FOLDER_ERROR => 'Failed to open temporary directory.',
self::DESTINATION_FOLDER_ERROR => 'Failed to open temporary directory for write.',
self::FILENAME_ERROR => 'Invalid temporary file name.',
self::FILE_UPLOAD_ERROR => 'The file upload resulted in an error on php level. See http://php.net/manual/en/features.file-upload.errors.php',
);
......@@ -60,10 +60,12 @@ class UploadException extends \Exception {
*
* @param int $code
* Error code.
* @param string|null $message
* The error message. Defaults to null.
*/
public function __construct($code) {
public function __construct($code, $message = NULL) {
$this->code = $code;
$this->message = $this->errorMessages[$this->code];
$this->message = isset($message) ? $message : $this->errorMessages[$this->code];
}
/**
......@@ -76,10 +78,7 @@ class UploadException extends \Exception {
return new JsonResponse(
array(
'jsonrpc' => '2.0',
'error' => array(
'code' => $this->code,
'message' => $this->errorMessages[$this->code],
),
'error' => $this->errorMessages[$this->code],
'id' => 'id',
),
500
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment