简体   繁体   English

Google App Engine Java上的UncaughtExceptionHandler

[英]UncaughtExceptionHandler on Google App Engine Java

I would like to do some custom logic anytime there is an uncaught exception in my application. 每当我的应用程序中存在未捕获的异常时,我都想执行一些自定义逻辑。 Normally I would do something like: 通常我会做类似的事情:

Thread.setDefaultUncaughtExceptionHandler(myCustomHandler);

However, Thread is locked down on Google App Engine. 但是, Thread已在Google App Engine上锁定。 The above code raises a security exception. 上面的代码引发了安全异常。 Is there anyway to achieve similar functionality on Google App Engine? 无论如何,在Google App Engine上是否可以实现类似功能?

EDIT: I have implemented my approach as a Servlet Filter. 编辑:我已经实现了作为Servlet筛选器的方法。 My experience with Java web programming is fairly limited. 我在Java Web编程方面的经验非常有限。 Can someone comment on such an approach? 有人可以评论这种方法吗? Since all requests in AppEngine originate as HTTP requests, I think this is a comprehensive solution. 由于AppEngine中的所有请求均源自HTTP请求,因此我认为这是一个全面的解决方案。

@Override
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {

    try {
        chain.doFilter(request, response);
    } catch (RuntimeException ex) {
        // Work my magic here...
        throw ex;
    }
}

Servlets handle threads on their own so you should not mess with it in this way. Servlet自行处理线程,因此您不应以这种方式弄乱线程。

Instead, install a servlet Error Handler : 相反,请安装servlet 错误处理程序

<error-page>
    <error-type>com.package.YourException</error-type>
    <location>/errors/handler</location>
</error-page>

then have /errors/handler map to servlet that handles the error and returns the appropriate response. 然后使/errors/handler映射到处理错误并返回适当响应的servlet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM