Commit 225daed6 authored by slashrsm's avatar slashrsm
Browse files

Merge pull request #11 from primsi/2554727_error_feedback

Issue #2554727 by Primsi: Provide better feedback about upload errors
parents 17131ad7 a138ed3d
...@@ -167,8 +167,6 @@ class UploadController extends ControllerBase { ...@@ -167,8 +167,6 @@ class UploadController extends ControllerBase {
* *
* @throws \Drupal\dropzonejs\UploadException * @throws \Drupal\dropzonejs\UploadException
* @throws Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * @throws Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*
* @todo Handle all the possible upload errors. See file_save_upload().
*/ */
protected function handleUpload() { protected function handleUpload() {
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
...@@ -176,8 +174,28 @@ class UploadController extends ControllerBase { ...@@ -176,8 +174,28 @@ class UploadController extends ControllerBase {
if (!$file instanceof UploadedFile) { if (!$file instanceof UploadedFile) {
throw new AccessDeniedHttpException(); throw new AccessDeniedHttpException();
} }
elseif ($file->getError() != UPLOAD_ERR_OK) { elseif ($error = $file->getError() && $error != UPLOAD_ERR_OK) {
throw new UploadException(UploadException::FILE_UPLOAD_ERROR); // 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. // Open temp file.
......
...@@ -50,7 +50,7 @@ class UploadException extends \Exception { ...@@ -50,7 +50,7 @@ class UploadException extends \Exception {
self::INPUT_ERROR => 'Failed to open input stream.', self::INPUT_ERROR => 'Failed to open input stream.',
self::OUTPUT_ERROR => 'Failed to open output stream.', self::OUTPUT_ERROR => 'Failed to open output stream.',
self::MOVE_ERROR => 'Failed to move uploaded file.', 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::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', 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 { ...@@ -60,10 +60,12 @@ class UploadException extends \Exception {
* *
* @param int $code * @param int $code
* Error 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->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 { ...@@ -76,10 +78,7 @@ class UploadException extends \Exception {
return new JsonResponse( return new JsonResponse(
array( array(
'jsonrpc' => '2.0', 'jsonrpc' => '2.0',
'error' => array( 'error' => $this->errorMessages[$this->code],
'code' => $this->code,
'message' => $this->errorMessages[$this->code],
),
'id' => 'id', 'id' => 'id',
), ),
500 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