Skip to content

Commit

Permalink
Correção do ViewModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
0x3175633435 committed Jul 20, 2018
1 parent 6ef6664 commit be13ded
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Event/ErrorEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Zend\HttpErrors\Exceptions\Base\HttpErrorException;
use Zend\HttpErrors\Exceptions\HttpInternalServerErrorException;
use Zend\HttpErrors\Factory\JsonResponseFactory;
use Zend\HttpErrors\Factory\ResponseFactory;
use Zend\Mvc\MvcEvent;

Expand Down Expand Up @@ -60,6 +61,9 @@ public function handle(MvcEvent $event)
$exception = new HttpInternalServerErrorException();
}

$viewModel = JsonResponseFactory::createFrom($exception);
$event->setViewModel($viewModel);

$this->responseFactory->createFor($exception, $response);
$event->stopPropagation(true);

Expand Down
7 changes: 6 additions & 1 deletion src/Factory/JsonResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
namespace Zend\HttpErrors\Factory;

use Zend\HttpErrors\Exceptions\Base\HttpErrorException;
use Zend\View\Model\JsonModel;

class JsonResponseFactory
{
public static function createFrom(HttpErrorException $exception)
{
return json_encode([
$jsonViewModel = new JsonModel([
'errorCode' => $exception->getCode(),
'message' => $exception->getMessage(),
]);

$jsonViewModel->setTerminal(true);

return $jsonViewModel;
}
}
1 change: 0 additions & 1 deletion src/Factory/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function createFor(HttpErrorException $exception, $response = null)

$response->setStatusCode($exception::HTTP_ERROR_CODE);
$response->setReasonPhrase($exception::HTTP_REASON_PHRASE );
$response->setContent(JsonResponseFactory::createFrom($exception));

$httpErrorCode = $exception::HTTP_ERROR_CODE;

Expand Down

0 comments on commit be13ded

Please sign in to comment.