Skip to content

Commit

Permalink
Merge pull request #219 from FriendsOfCake/validation-exception
Browse files Browse the repository at this point in the history
Make ValidationException extend HttpException.
  • Loading branch information
lorenzo committed Sep 24, 2014
2 parents 71d5802 + cdd9fa6 commit 5c0ac1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/Error/Exception/ValidationException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Crud\Error\Exception;

use Cake\Core\Exception\Exception;
use Cake\Network\Exception\BadRequestException;
use Cake\ORM\Entity;
use Cake\Utility\Hash;

Expand All @@ -10,7 +10,7 @@
* responses where you need an error code in response
*
*/
class ValidationException extends Exception {
class ValidationException extends BadRequestException {

/**
* List of validation errors that occurred in the model
Expand Down Expand Up @@ -38,7 +38,13 @@ public function __construct(Entity $entity, $code = 412) {
$flat = Hash::flatten($this->_validationErrors);

$errorCount = $this->_validationErrorCount = count($flat);
$this->message = __dn('crud', 'A validation error occurred', '{0} validation errors occurred', $errorCount, [$errorCount]);
$this->message = __dn(
'crud',
'A validation error occurred',
'{0} validation errors occurred',
$errorCount,
[$errorCount]
);

parent::__construct($this->message, $code);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Error/ExceptionRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ public function testGenericExceptionDuringRendering() {
}

public function testValidationErrorSingleKnownError() {
Configure::write('debug', false);

$entity = new Entity();
$entity->errors('title', ['error message']);

Expand Down Expand Up @@ -374,9 +376,7 @@ public function testValidationErrorSingleKnownError() {
),
'message' => 'A validation error occurred'
);
$data = $Controller->viewVars['data'];
unset($data['exception']['trace']);
$this->assertEquals($expected, $data);
$this->assertEquals($expected, $Controller->viewVars['data']);
}

public function testValidationErrorMultipleMessages() {
Expand Down

0 comments on commit 5c0ac1a

Please sign in to comment.