在prod环境中FOSRestBundle配置异常消息

Gui*_*TIT 5 php symfony fosrestbundle

我正在努力解决与FOSRestBundle相关的问题(版本0.13.*)

我有一些REST API会抛出一些异常,我猜不出异常.但是,尽管我做了特定的配置,允许在响应中格式化异常消息,即使在生产中也是如此(遵循我在此处找到的文档:https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/4 -exception-controller-support.md),JSON响应绝对是空的......

示例如下:

http://host/app_dev.php/api/postcode/search?postcode=  
Run Code Online (Sandbox Code Playgroud)

结果是:

HTTP 400: {"status":"error","status_code":400,"status_text":"Bad Request","current_content":"","message":"You must provide a postcode"}
Run Code Online (Sandbox Code Playgroud)

http://host/api/postcode/search?postcode=
Run Code Online (Sandbox Code Playgroud)

结果是:

HTTP 400: []
Run Code Online (Sandbox Code Playgroud)

我的API控制器如下所示:

/**
 * Search post codes
 *
 * @param Request   $request   Request
 * @param Promotion $promotion Promotion
 *
 * @Rest\View()
 *
 * @throws BadRequestHttpException
 * @return array
 */
public function searchAction(Request $request, Promotion $promotion)
{
    // Get post code
    $postCode = $request->query->get('postcode');
    if (!$postCode) {
        throw new BadRequestHttpException('You must provide a postcode');
    }

    // SOME LOGIC HERE TO GET DATA

    return $data;
}
Run Code Online (Sandbox Code Playgroud)

并且fos_rest配置如下所示:

fos_rest:
    routing_loader:
        default_format: json
    view:
        mime_types:
            json: ['application/json; charset=UTF-8']
        formats:
            json: true
        view_response_listener: force
    format_listener: false
    access_denied_listener:
        json: true
    body_listener: true
    exception:
        messages:
            Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
Run Code Online (Sandbox Code Playgroud)

据我所知,fos_rest.exception.messages配置数组应该列出我想要生成错误消息序列化的异常.正如您在控制器的代码中看到的,此响应包含将显示给客户端的已翻译错误消息.为什么忽略此配置?我可以肯定地说,即使在prod环境中也正确加载了配置,因为如果我在conf中错误地输入了类名,它就会因"无法加载类"异常而失败.

我错过了什么?提前感谢您提供的任何暗示......

tim*_*c22 6

我有类似的问题,你的问题帮我解决了!我知道它已经晚了但我发现清除prod缓存有助于我解决这个问题.

这些是我的设置:

fos_rest:
    param_fetcher_listener: true
    body_listener:
        array_normalizer: fos_rest.normalizer.camel_keys
    format_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true
        templating_formats:
            html: true
        force_redirects:
            html: true
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
    routing_loader:
        default_format: json
    serializer:
        serialize_null: true
    access_denied_listener:
        json: true
    exception:
        enabled: true
        messages:
            Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
Run Code Online (Sandbox Code Playgroud)

我也想知道是否总是有例外明确地添加到codesmessages该配置的部分,除非你用HttpException:

throw new HttpException(400, "New comment is not valid.");
Run Code Online (Sandbox Code Playgroud)


Ash*_*thi 0

我没有尝试过,但看起来你忘记了这一行:

 exception:
    codes:
      'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
Run Code Online (Sandbox Code Playgroud)

试一试。

你有没有提到这一点:

twig:
   ...
   ...
   exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'
Run Code Online (Sandbox Code Playgroud)