Capture exceptions with a different handler for applications REST

北战南征 提交于 2019-12-04 11:07:51
Thomas Trötscher

I have been using the following in my Silex RESTful app to return errors in json format:

$app->error(function (\Exception $e, $code) use($app) {
    return $app->json(array("error" => $e->getMessage()),$code);    
});

Not sure if this is the right way, but it works for me.

This is documented on the Silex site: http://silex.sensiolabs.org/doc/usage.html#error-handlers

On Symfony2 you can use the ExceptionHandler. On the Exception you have the stack trace, so you can identify where it was thrown.

Also, in Symfony2 you can customize depending on the expected format. It's well explained in it's documentation.

For instance, if you replace the ExceptionController with one of yours, the third parameter shows the expected format:

Reference on where to change the ExceptionController

ExceptionController API

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!