FOSRestBundle:显示我的自定义异常消息 [英] FOSRestBundle: show my custom exception message

查看:89
本文介绍了FOSRestBundle:显示我的自定义异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在FOSRestBundle中添加自定义的异常控件,但它似乎忽略了我的自定义消息(响应的状态码正常).

I'm trying to add a custom control of exceptions in FOSRestBundle but it seems to ignore my custom messages (the status code of the response is ok).

我有:

throw new HttpException(404, "User {$id} not found");

但是得到这个json响应:

But get this json response:

{
  "error": {
    "code": 404,
    "message": "Not Found"
  }
}

所以我找不到显示自定义消息的方法

So I don't find the way to show my custom message

推荐答案

您还可以使用以下配置抛出自己的异常:

You could also throw you own exception using the following configuration:

fos_rest:
    exception:
        codes:
            'My\Custom\NotFound\Exception404': 404
        messages:
            'My\Custom\NotFound\Exception404': true

在您自己的异常类中提供自定义消息,它应该可以按预期工作.

Provide a custom message in your own exception class and it should work as expected.

这篇关于FOSRestBundle:显示我的自定义异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆