No response when ValidationException is thrown using Plupload and Symfony3
Sorry previous version was incorrect.
OneupUploaderBundle catches exceptions in a controller and pass them to error_handler
service.
class PluploadController extends AbstractChunkedController{ public function upload() { ... foreach ($files as $file) { try { $chunked ? $this->handleChunkedUpload($file, $response, $request) : $this->handleUpload($file, $response, $request) ; } catch (UploadException $e) { $this->errorHandler->addException($response, $e); } } return $this->createSupportedJsonResponse($response->assemble()); }
You should check which error handler is used for your application. Looks like by default for Plupload the bundle set up NoopErrorHandler
which does nothing with your exception.
Oneup\UploaderBundle\Uploader\ErrorHandler\NoopErrorHandler
public function addException(AbstractResponse $response, Exception $exception){ // noop}
<service id="oneup_uploader.error_handler.plupload" class="%oneup_uploader.error_handler.noop.class%" public="false" />
To get Response of desired shape you should set up BlueimpErrorHandler
or to define a custom handler.
Oneup\UploaderBundle\Uploader\ErrorHandler\BlueimpErrorHandler
public function addException(AbstractResponse $response, Exception $exception){ $message = $this->translator->trans($exception->getMessage(), array(), 'OneupUploaderBundle'); $response->addToOffset(array('error' => $message), array('files'));}
OT: Using exceptions in the validator that way will bring bad experience to a user. The user should see all errors with an uploaded file not just first one. You should collect all errors and then throw an exception. For example:
... if (in_array($full_file_name, $this->banned_files)) { throw new ValidationException('error.file_exists'); } $errors = []; // Is file mime type the same as extension mime type $mime_type_position = array_search($file_extension, $this->file_extension_array); if ($mime_type_position !== false) { $mime_type_by_extension = $this->file_type_array[$mime_type_position]; if ($mime_type_by_extension !== $file_mime_type) { $errors[] = 'error.mime_type_mismatch'; } } // Is file type not in activated file type array if (!in_array($file_mime_type, $this->file_type_array)) { $errors[] = 'error.forbidden_mime_type'; } if (empty($errors)) { throw new ValidationException(implode(', ', $errors)); } ...