Custom exception with fosrest bundle in symfony2

本秂侑毒 提交于 2019-12-11 06:36:32

问题


Hi I am trying to add custom exception but it is not working

now my response is like:(also the breakdown debuger is not trigger that class)

{
  "error": {
    "code": 500,
    "message": "Required Parameter Missing"
  }
}

this is the default exception of fos rest I added new class that wrapp the exception

  class ExceptionWrapperHandler implements ExceptionWrapperHandlerInterface {
public function wrap($data)
{
    $exception = $data['exception'];

 $newException = array(
        'success' => false,
        'exception' => array(
            'exceptionClass' => $exception->getClass(),
            'message' => $data['status_text']
        )
    );
    return $newException;

}

}

my config file:

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener: true
    routing_loader:
           default_format: json
    view:
        view_response_listener: force
        formats:
            json: true
            xml: true
        templating_formats:
            html: false
#        exception_wrapper_handler: CLASS path
    exception:
        enabled: true
    service:
        exception_handler: appname.exception_handler 
        serializer: jms_serializer.serializer
    serializer:
        serialize_null: true

回答1:


You need to add your Exception class to the exception.messages array in your fos_rest config in your config.yml for it to show your custom message

fos_rest:
    exception:
        enabled: true
        messages:
            'AppBundle\ExpeptionHandler\ ExceptionWrapperHandler': true


来源:https://stackoverflow.com/questions/38040832/custom-exception-with-fosrest-bundle-in-symfony2

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