rest - Symfony2 FOSRestBundle ParamFetcher JSON error -
i have symfony2 setup fosrestbundle , i'm using paramfetcher.
i have route has requirements it's parameters , i'd show error messages api-user, in current setup error messages not shown in production , in development way extensive.
parameter requirements
offset requirement \d+ description result offset default 0 limit requirement \d+ description result limit count default 100
annotations:
/** * list of users can limited result count , 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 request in production mode:
request url /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 tried adding error_message param rule, not lead nice error message.
what should nice error message api end user?
we doing implementing exceptionwrapperhandlerinterface, see e.g. 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 lot's of information in $data. check param validation exception.
we're not using annotation param validation extending exceptionwrapper can e.g. achieve in our code:
throw new httpexception( 400, 'my custom message' );
Comments
Post a Comment