【问题标题】:Custom exception with fosrest bundle in symfony2symfony2 中 fosrest 包的自定义异常
【发布时间】:2016-06-26 16:49:10
【问题描述】:

您好,我正在尝试添加自定义异常,但它不起作用

现在我的回复是这样的:(故障调试器也不会触发该类)

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

这是 fos rest 的默认例外 我添加了包装异常的新类

  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;

}

}

我的配置文件:

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

【问题讨论】:

    标签: symfony fosrestbundle


    【解决方案1】:

    您需要将 Exception 类添加到 config.yml 中 fos_rest 配置中的 exception.messages 数组中,以显示您的自定义消息

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

    【讨论】:

      猜你喜欢
      • 2014-03-14
      • 2011-12-15
      • 1970-01-01
      • 2016-02-02
      • 2022-01-03
      • 2012-09-11
      • 2011-12-20
      • 1970-01-01
      • 2011-04-19
      相关资源
      最近更新 更多