Hello,
I update fosRest to 1.8 to 2.1.
I have the same error of #1588.
The error is raising when ExceptionController is construct.
I have a custom controller exception define in fos rest configuration :
fos_rest:
exception:
enabled: true
exception_controller: '\XXX\CommonBundle\Handler\FosRestExceptionController::showAction'
With FosRestExceptionController extends FOS\RestBundle\Controller\ExceptionController.
I have not see a clear change for this in the doc.
I use JMSSerializer.
I have missing a part in my upgrade or a regression is make in custom exception controller?
Thanks in advance.
Can you show us your controller ?
yes the code is the follow :
<?php
namespace XXX\CommonBundle\Handler;
use FOS\RestBundle\Controller\ExceptionController;
use Symfony\Component\Translation\TranslatorInterface;
class FosRestExceptionController extends ExceptionController
{
/**
* @var TranslatorInterface
*/
private $translator;
/*
* @var string
*/
private $translationDomain = 'exception-ENV';
protected function getExceptionMessage($exception)
{
$this->translator = $this->container->get('translator');
$env = $this->container->get('kernel')->getEnvironment();
$this->translationDomain = str_replace('ENV', $env, $this->translationDomain);
switch ($exception->getClass()) {
case 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException':
return $this->getNotFoundHttpExceptionMessage($exception->getMessage());
case 'Doctrine\ORM\EntityNotFoundException':
$message = 'entity_not_found';
break;
default:
$message = $exception->getMessage();
break;
}
return $this->translator->trans($message, [], $this->translationDomain);
}
private function getNotFoundHttpExceptionMessage($message)
{
if (preg_match('`(.+) object not found.$`', $message, $matches)) {
return $this->translator->trans(
'given_object_not_found',
[
'%class%' => (string) $matches[1],
],
$this->translationDomain
);
} elseif (preg_match('`^No route found for (.+)`', $message, $matches)) {
return $this->translator->trans(
'given_route_not_found',
[
'%route%' => (string) $matches[1],
],
$this->translationDomain
);
}
return $this->translator->trans($message, [], $this->translationDomain);
}
}
@ektarum the container is no longer passed to the ExceptionController as it has been transformed in a service (its constructor is changed).
To have something more stable, you should either use normalizers (as explained in the upgrading file) or extend/decorate fos_rest.exception.messages_map.
For me, it's a huge BC break for users of JMS serializer who extends ExceptionController.
Pass to JMS to normalizers it's possible but it's time consumming.
I think the user should be aware before the upgrade.
Perhaps the changelog need more clearly informations for this point ?
can you submit a PR improving https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/UPGRADING-2.0.md accordingly?
Ok i attempt to make that in few days.
@GuilhemN, @lsmith77, @ektarum guys, can you give some examples, how it should be implemented now, to handle exceptions? I'd appreciate it (and I think not only me). Thank you!
@RuslanZavacky does this description here help you?
@RuslanZavacky for my part i replace totally JMS serializer by the native symfony serializer.
Closing as it's too late to improve upgrade for 2.0. FYI, upgrading to 2.6 and getting rid of symfony deprecations will affect exception controllers again, see changelog file of rest-bundle.
Most helpful comment
@GuilhemN, @lsmith77, @ektarum guys, can you give some examples, how it should be implemented now, to handle exceptions? I'd appreciate it (and I think not only me). Thank you!