Skip to content

Commit

Permalink
Add overriden NotFoundHttpException to have access to the original re…
Browse files Browse the repository at this point in the history
…sponse when converting 404
  • Loading branch information
emodric committed May 4, 2017
1 parent 3515e9b commit df75819
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
37 changes: 37 additions & 0 deletions bundle/Exception/NotFoundHttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace eZ\Bundle\EzPublishLegacyBundle\Exception;

use eZ\Bundle\EzPublishLegacyBundle\LegacyResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException as BaseNotFoundHttpException;

class NotFoundHttpException extends BaseNotFoundHttpException
{
/**
* @var \eZ\Bundle\EzPublishLegacyBundle\LegacyResponse
*/
protected $originalResponse;

/**
* Constructor.
*
* @param string $message
* @param \eZ\Bundle\EzPublishLegacyBundle\LegacyResponse $originalResponse
*/
public function __construct($message, LegacyResponse $originalResponse = null)
{
parent::__construct($message);

$this->originalResponse = $originalResponse;
}

/**
* Returns the response.
*
* @return \eZ\Bundle\EzPublishLegacyBundle\LegacyResponse
*/
public function getOriginalResponse()
{
return $this->originalResponse;
}
}
8 changes: 5 additions & 3 deletions bundle/LegacyResponse/LegacyResponseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use eZ\Bundle\EzPublishLegacyBundle\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Templating\EngineInterface;
use DateTime;
Expand Down Expand Up @@ -104,13 +104,15 @@ public function generateResponseFromModuleResult(ezpKernelResult $result)
$errorMessage = isset($moduleResult['errorMessage']) ? $moduleResult['errorMessage'] : 'Access denied';
throw new AccessDeniedException($errorMessage);
}
// If having an "Not found" error code in non-legacy mode and conversation is true,

// If having an "Not found" error code in non-legacy mode and conversion is true,
// we send an NotFoundHttpException to be able to trigger error page in Symfony stack.
if ($moduleResult['errorCode'] == 404) {
if ($this->notFoundHttpConversion) {
$errorMessage = isset($moduleResult['errorMessage']) ? $moduleResult['errorMessage'] : 'Not found';
throw new NotFoundHttpException($errorMessage);
throw new NotFoundHttpException($errorMessage, $response);
}

@trigger_error(
"Legacy 404 error handling is deprecated, and will be removed in legacy-bridge 2.0.\n" .
'Use the not_found_http_conversion setting to use the new behavior and disable this notice.',
Expand Down

0 comments on commit df75819

Please sign in to comment.