将异常消息添加到 json 响应
我有一段代码抛出特定类型的异常,如下所示:
throw new BadDataException("error message");
这些异常在响应类型为 json 的方法内抛出。我对此异常类型的配置如下:
<global-exception-mappings>
<exception-mapping result="badDataError" exception="mypackage.BadDataException" />
</global-exception-mappings>
<result name="badDataError" type="json">
<param name="statusCode">500</param>
</result>
我想将异常消息添加到 json 响应中以将其显示给用户。当返回 500 状态码时,有什么方法可以将异常消息映射到响应。 ajax 调用将如下所示:
$.ajax(
{
...
success: function(data, textStatus) {
alert('Success');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error");//I'd like to add here the reason (exception message)
}
...
}
);
如何自动将此异常的消息添加到 HTTP 500 响应中? (如果可以的话)
谢谢
I have a code which throws a specific type of exception like this:
throw new BadDataException("error message");
these kind of exceptions are thrown inside a method whose response type is json. I have a configuration for this exception type like this:
<global-exception-mappings>
<exception-mapping result="badDataError" exception="mypackage.BadDataException" />
</global-exception-mappings>
<result name="badDataError" type="json">
<param name="statusCode">500</param>
</result>
I'd like to add the exception message to the json response to show it to the user. Is there any way to map the exception message to the response when a 500 status code is returned. The ajax call would be something like this:
$.ajax(
{
...
success: function(data, textStatus) {
alert('Success');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error");//I'd like to add here the reason (exception message)
}
...
}
);
How can I add automatically the message of this exception to the HTTP 500 response? (if it is possible)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终就是这样做的。我以这种方式向 HTTP 500 响应添加了一个 errorMessage 字段。
在ajax请求中我恢复了这样的消息。
也许有一种更优雅的方法可以做到这一点,但至少它有效。
This is finally how I've done it. I've added a errorMessage field to the HTTP 500 response in this way.
and in the ajax request I recover the message like this.
Maybe there's a more elegant way to do this, but at least it works.
另一种选择是创建自定义结果类型,既设置 500 ISE 状态,又返回包含错误数据的 JSON 响应。然后只需将异常映射到 struts.xml 中的结果类型(就像将其映射到上面示例中的 httpheader 类型一样)。
另一种选择是为您打算通过 AJAX 调用的 Struts 操作方法创建注释。然后,对标准 ExceptionMappingInterceptor 进行子类化,如果操作方法被注释为 @AjaxRequest(或任何您所称的名称),那么您将返回一个包含异常信息的标准 JSON 响应。否则,您将陷入将结果映射到某种页面或结果的默认行为。
就我个人而言,我更喜欢后一种方法。
Another option is to create a custom result type that both sets a 500 ISE status and returns a JSON response containing the error data. Then just map your exception to that result type in your struts.xml (just as you mapped it to the httpheader type in your example above).
Yet another option is to create an annotation for your Struts action methods that you intend to invoke via AJAX. Then, subclass the standard ExceptionMappingInterceptor and if the action method is annotated as
@AjaxRequest
(or whatever you call it), then you return a standard JSON response that contains the exception information. Otherwise, you fall through to the default behavior of mapping the result to some sort of page or result.Personally, I prefer the latter approach.