FOSRestBundle: MethodNotAllowedException is not caught

Hi all,

I’ve the following configuration:

product_option_create:
    pattern: /create.{_format}
    defaults:
        _format: ~
        _controller: colibri_product.controller.option:createAction
    requirements:
        _method: POST
twig:
    exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'

Accessing option/create.json, results in: Untitled

There is some way to handle this exception properly?

I was expecting something like:

{
    "status":"error",
    "status_code":405,
    "status_text":"Method Not Allowed",
    "current_content":"",
    "message":"Method Not Allowed (Allow: POST)"
}

Thanks in advance.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 32 (14 by maintainers)

Most upvoted comments

If anyone has the same issue in the future, this is what I did:

I created a custom Exception controller that extends this bundle’s controller and then override the showAction method.

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;

class ExceptionController extends \FOS\RestBundle\Controller\ExceptionController 
{

    public function showAction(Request $request, $exception, DebugLoggerInterface $logger = null)
    {

        if ($exception->getClass() == 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException') {
            return new Response("Not found", 404);
        }
        return parent::showAction($request, $exception, $logger);

    }
}

Don’t forget to change the configuration setttings (config.yml)

exception:
    enabled: true
    exception_controller: 'AppBundle\Controller\ExceptionController::showAction'