Skip to content

Commit

Permalink
Exception tweaks.
Browse files Browse the repository at this point in the history
ValidationException is now extending Symfonys UploadException and
will be passed through. There is now a new field for adding custom error messages.
  • Loading branch information
sheeep committed Jul 14, 2013
1 parent d7802d5 commit 75b9de7
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 18 deletions.
9 changes: 1 addition & 8 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -15,7 +14,6 @@
use Oneup\UploaderBundle\Event\ValidationEvent;
use Oneup\UploaderBundle\Uploader\Storage\StorageInterface;
use Oneup\UploaderBundle\Uploader\Response\ResponseInterface;
use Oneup\UploaderBundle\Uploader\Exception\ValidationException;
use Oneup\UploaderBundle\Uploader\ErrorHandler\ErrorHandlerInterface;

abstract class AbstractController
Expand Down Expand Up @@ -144,11 +142,6 @@ protected function validate(UploadedFile $file)
$dispatcher = $this->container->get('event_dispatcher');
$event = new ValidationEvent($file, $this->config, $this->type);

try {
$dispatcher->dispatch(UploadEvents::VALIDATION, $event);
} catch (ValidationException $exception) {
// pass the exception one level up
throw new UploadException($exception->getMessage());
}
$dispatcher->dispatch(UploadEvents::VALIDATION, $event);
}
}
2 changes: 1 addition & 1 deletion Controller/BlueimpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function upload()

$chunked = !is_null($request->headers->get('content-range'));

foreach ((array)$files as $file) {
foreach ((array) $files as $file) {
try {
$chunked ?
$this->handleChunkedUpload($file, $response, $request) :
Expand Down
2 changes: 1 addition & 1 deletion Controller/FineUploaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function upload()
} catch (UploadException $e) {
$response->setSuccess(false);
$response->setError($translator->trans($e->getMessage(), array(), 'OneupUploaderBundle'));

$this->errorHandler->addException($response, $e);

// an error happended, return this error message.
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/OneupUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function load(array $configs, ContainerBuilder $container)
if(empty($controllerName) || empty($controllerType))
throw new ServiceNotFoundException('Empty controller class or name. If you really want to use a custom frontend implementation, be sure to provide a class and a name.');
}

$errorHandler = new Reference($mapping['error_handler']);

// create controllers based on mapping
Expand All @@ -133,8 +133,8 @@ public function load(array $configs, ContainerBuilder $container)
->setScope('request')
;

if($mapping['enable_progress'] || $mapping['enable_cancelation']) {
if(strnatcmp(phpversion(), '5.4.0') < 0) {
if ($mapping['enable_progress'] || $mapping['enable_cancelation']) {
if (strnatcmp(phpversion(), '5.4.0') < 0) {
throw new InvalidArgumentException('You need to run PHP version 5.4.0 or above to use the progress/cancelation feature.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Controller/BlueimpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testEvents()
$this->assertEquals($uploadCount, count($this->getUploadedFiles()));
$this->assertEquals(1, $preValidation);
}

protected function getConfigKey()
{
return 'blueimp';
Expand Down
2 changes: 1 addition & 1 deletion Tests/Controller/BlueimpValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function testAgainstIncorrectMimeType()
$this->assertEquals($response->headers->get('Content-Type'), 'application/json');
$this->assertCount(0, $this->getUploadedFiles());
}

protected function getConfigKey()
{
return 'blueimp_validation';
Expand Down
8 changes: 7 additions & 1 deletion Uploader/ErrorHandler/BlueimpErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class BlueimpErrorHandler implements ErrorHandlerInterface
{
public function addException(ResponseInterface $response, UploadException $exception)
{
$response->addToOffset($exception->getMessage(), 'files');
if ($exception instanceof ValidationException) {
$message = $exception->getErrorMessage();
} else {
$message = $exception->getMessage();
}

$response->addToOffset(array('error' => $message), 'files');
}
}
2 changes: 1 addition & 1 deletion Uploader/ErrorHandler/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace Oneup\UploaderBundle\Uploader\ErrorHandler;

use Symfony\Component\HttpFoundation\File\Exception\UploadException;
Expand Down
21 changes: 20 additions & 1 deletion Uploader/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@

namespace Oneup\UploaderBundle\Uploader\Exception;

class ValidationException extends \DomainException
use Symfony\Component\HttpFoundation\File\Exception\UploadException;

class ValidationException extends UploadException
{
protected $errorMessage;

public function setErrorMessage($message)
{
$this->errorMessage = $message;

return $this;
}

public function getErrorMessage()
{
// if no error message is set, return the exception message
if (!$this->errorMessage) {
return $this->getMessage();
}

return $this->errorMessage;
}
}
9 changes: 9 additions & 0 deletions Uploader/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public function offsetGet($offset)
{
return isset($this->data[$offset]) ? $this->data[$offset] : null;
}

public function addToOffset($offset, array $value)
{
if (!array_key_exists($offset, $this->data)) {
$this->data[$offset] = array();
}

$this->data[$offset] += $value;
}
}

0 comments on commit 75b9de7

Please sign in to comment.