简体   繁体   English

Symfony2 FOSRestBundle ParamFetcher JSON错误

[英]Symfony2 FOSRestBundle ParamFetcher JSON error

I have a Symfony2 setup with FOSRestBundle and I'm using the ParamFetcher. 我有一个带有FOSRestBundle的Symfony2设置,我正在使用ParamFetcher。

I now have a route which has some requirements for it's parameters and I'd like to show a the error messages to the API-user, but in my current setup the error messages are not shown in production and in development they are way to extensive. 我现在有一条路线对它的参数有一些要求,我想向API用户显示错误信息,但在我目前的设置中,错误信息没有显示在生产和开发中,它们是广泛的。

Parameter requirements 参数要求

offset
Requirement \d+
Description Result offset
Default 0

limit
Requirement \d+
Description Result limit count
Default 100

Annotations: 注释:

/**
 * Get a list of users which can be limited to a certain result count and offset.
 *
 * Max 30 users per request allowed.
 *
 * @ApiDoc(
 *  resource=true,
 *  description="Load list of users",
 *  statusCodes={
 *      200="Returned when successful",
 *      400="Bad user input",
 *      500="In case of server error,"
 *  }
 * )
 *
 * @View()
 *
 * @param ParamFetcher $paramFetcher
 * @param int $offset integer offset (requires param_fetcher_listener: force)
 * @param int $limit integer result limit (requires param_fetcher_listener: force)
 * @QueryParam(name="offset", description="Result offset", default="0",
 *     requirements="\d+", strict=true, nullable=true)
 * @QueryParam(name="limit", description="Result limit count", default="30",
 *     requirements="\d+", strict=true, nullable=true)
 *
 * @return array response array
 */

Making the request in production mode: 在生产模式下发出请求:

Request URL
GET /events.json?offset=strangeText&limit=huh!?
Response Headers [Expand] 
400 Bad Request

Date: Tue, 02 Jun 2015 20:45:15 GMT
Server: Apache/2
X-Powered-By: PHP/5.5.25
Vary: User-Agent
Content-Type: application/json
Cache-Control: no-cache
Connection: close
Content-Length: 47
Response Body [Raw]
▿{
  "error": ▿{
    "code": 400,
    "message": "Bad Request"
  }
}

I already tried adding error_message to the Param rule, but that does not lead to a nice error message. 我已经尝试将error_message添加到Param规则中,但这不会导致错误消息。

What should I do get a nice error message for the API end user? 我应该怎么做为API最终用户提供一个很好的错误消息?

We are doing it by implementing the ExceptionWrapperHandlerInterface, see eg https://symfony.com/doc/current/bundles/FOSRestBundle/2-the-view-layer.html#forms-and-views : 我们通过实现ExceptionWrapperHandlerInterface来实现它,参见例如https://symfony.com/doc/current/bundles/FOSRestBundle/2-the-view-layer.html#forms-and-views

namespace My\Bundle\Handler;

class MyExceptionWrapperHandler implements ExceptionWrapperHandlerInterface
{
/**
 * {@inheritdoc}
 */
public function wrap($data)
{
    return new MyExceptionWrapper($data);
}
}

You'll have access to lot's of information in $data. 您可以访问$ data中的大量信息。 Check what you get for your param validation exception. 检查您的param验证异常所得到的内容。

We're not using the annotation param validation but by extending the ExceptionWrapper we can eg easily achieve something like that in our code: 我们没有使用注释参数验证,但通过扩展ExceptionWrapper,我们可以在代码中轻松实现类似的功能:

throw new HttpException( 400, 'My custom message' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM