dougongnan2167 2015-12-23 11:18
浏览 32

在JSON上仅显示404错误消息(FOSRestBundle)

I used FOSRestBundle in order to convert the error messages format to JSON.

This is the fos_rest and twig config part in config.yml :

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'

fos_rest:
    routing_loader:
        default_format: json
        include_format: true                           # We do not include format in request, so that all responses
    view:
        view_response_listener: force
        force_redirects:
            html: true
    format_listener:
        rules:
            - { path: '^/', fallback_format: json}
    param_fetcher_listener: force
    body_listener: true
    exception:
        codes:
            'Symfony\Component\HttpKernel\Exception\NotFoundHttpException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true

The problem is that all pages (even with status code = 200) are converted and not only when error occurs (404, 500, ...)

EDITED : I used the default showAction of the ExceptionController of FOSRestBundle :

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

    {

        if (!$exception instanceof DebugFlattenException && !$exception instanceof HttpFlattenException) {
            throw new \InvalidArgumentException(sprintf(
                'ExceptionController::showAction can only accept some exceptions (%s, %s), "%s" given',
                'Symfony\Component\HttpKernel\Exception\FlattenException',
                'Symfony\Component\Debug\Exception\FlattenException',
                get_class($exception)
            ));
        }

        try {
            $format = $this->getFormat($request, $request->getRequestFormat());
        } catch (\Exception $e) {
            $format = null;
        }
        if (null === $format) {
            $message = 'No matching accepted Response format could be determined, while handling: ';
            $message .= $this->getExceptionMessage($exception);

            return $this->createPlainResponse($message, Codes::HTTP_NOT_ACCEPTABLE, $exception->getHeaders());
        }

        $currentContent = $this->getAndCleanOutputBuffering($request);
        $code = $this->getStatusCode($exception);
        $viewHandler = $this->container->get('fos_rest.view_handler');
        $parameters = $this->getParameters($viewHandler, $currentContent, $code, $exception, $logger, $format);
        $showException = $request->attributes->get('showException', $this->container->get('kernel')->isDebug());

        try {
            if (!$viewHandler->isFormatTemplating($format)) {
                $parameters = $this->createExceptionWrapper($parameters);
            }

            $view = View::create($parameters, $code, $exception->getHeaders());
            $view->setFormat($format);

            if ($viewHandler->isFormatTemplating($format)) {
                $view->setTemplate($this->findTemplate($request, $format, $code, $showException));
            }

            $response = $viewHandler->handle($view);
        } catch (\Exception $e) {
            $message = 'An Exception was thrown while handling: ';
            $message .= $this->getExceptionMessage($exception);
            $response = $this->createPlainResponse($message, Codes::HTTP_INTERNAL_SERVER_ERROR, $exception->getHeaders());
        }

        return $response;
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来