从 Jersey RESTful Web 服务中抛出信息性错误消息
我有一个 RESTful Web 服务,它执行一些 IO 和数据库活动以返回结果。
我想看到一些异常
,即由于未设置预期变量而引发的异常,或者我的数据库不再存在等...
但是,当我引发这些异常时,它们会被捕获并包装在 HTTP 500(内部服务器错误)
异常中,这就是客户端得到的内容(与根异常相反)。
我想在这里看到我的异常,而不是必须查看应用程序服务器日志。
I've got a RESTful Web-Service that does some IO and database activity in order to return a result.
There are some Exceptions
that I would like to see, ie exception thrown because an expected variable is not set, or my database is no longer exists, etc...
However when I throw these exceptions they get caught and wrapped in a HTTP 500 (Internal Server Error)
exception and that is what the client gets (As opposed to the root exception).
I would like to see my exception here, instead of going to have to look through the Application server logs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现一个返回任何您想要的内容的 ExceptionMapper。例如,它可以将整个异常和堆栈跟踪放入响应中。
这是您想要的客户端吗?我不确定。
对于客户端来说,重建异常并重新抛出它可能是一项相当困难的工作。
此外,它非常不安全,因为堆栈跟踪会将服务器的内部代码暴露给客户端。
我建议您声明一些对客户端有意义并且您想要公开的服务器错误代码。示例:
所以对于特定的异常,在 ExceptionMapper 中,您可以将服务器异常映射到错误代码,然后在客户端重新映射它们。
You can implement an ExceptionMapper that returns anything you want. So, for example, it can put the whole exception with the stacktrace to the response.
Is it what you want at client side? I'm not sure.
It can be quite a difficult job for a client to rebuild the exceptions and rethrow it.
In addition, it's quite unsecure, since the stacktrace will expose the server's internal code to the client.
I suggest you declare some server error codes that make sense to client and that you want to expose. Example:
So for specific exceptions, in the ExceptionMapper you can map the server exceptions to error codes and then remap them at the client side.