GWT服务异常记录的最佳做法 [英] Best Practices for GWT services exceptions logging

查看:146
本文介绍了GWT服务异常记录的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定将日志系统添加到我的gwt服务层。首先我想记录从该层抛出的所有异常。我有一个类似于Spring的ServletDispatcher的对象,它调用其他服务。我以为我可以在那里添加日志记录,但是我意识到GWT服务会将ServletResponse中的检查异常包装到UnexpectedException中。



有谁可以分享他的经验吗?记录所有GWT服务的检查和未检查的异常的最佳方法是。






我找到了解决方案, em> RemoteServiceServlet 并覆盖默认异常流。但是我觉得这个解决方案太耗时了。是否有任何更容易的变体?

解决方案

在服务器端,我们有一个RemoteServiceServlet的子类,用于所有服务实现。你提到这似乎是耗时的,但是这是代码的样子。你做了一次,你完成了。

  @Override 
protected void doUnexpectedFailure(Throwable t){
t.printStackTrace(System.err);
super.doUnexpectedFailure(t);
}

注意:我们实际上并不发送到System.err,不应该,但是你可以得到这个想法。



在客户端,我们使用AsyncCallback的子类叫做AsyncSuccessCallback。它大部分的RPC调用统一处理onFailure案例。我们的大部分回调代码都可以处理onSuccess事件,知道onFailure被处理。它也提供了一个单一的地方来更改此实现。

  public abstract class AsyncSuccessCallback< T>实现AsyncCallback< T> {

public void onFailure(Throwable t){
handleException(t);
}

protected void handleException(Throwable t){
Window.alert(t.getMessage());
}

}

注意:使用Window.alert,但再次,你得到的想法。在这种情况下我们所做的是显示一个GWT DialogBox,该对话框显示一个窗体,该表单对接受错误报告的不同服务器执行POST。该表单允许用户在发生错误时键入对它们正在执行的操作的描述。



在客户端,如果要获取堆栈跟踪,则需要写一点额外的代码:

  // for lineEnding,使用< br>对于HTML,\\\
为文本
public static final String getStackTrace(Throwable t,String lineEnding){
Object [] stackTrace = t.getStackTrace();
if(stackTrace!= null){
StringBuilder output = new StringBuilder();
for(Object line:stackTrace){
output.append(line);
output.append(lineEnding);
}
return output.toString();
} else {
return[stack unavailable];
}
}


I decided to add logging system to my gwt service layer. First of all I wanted to log all exceptions that are thrown from that layer. I had an object similar to Spring's ServletDispatcher, which calls other services. I thought I could add logging there, but I realized that GWT services wraps checked exceptions in ServletResponse and unchecked into UnexpectedException.

Can anybody share his experience with that problem? What is the best way to log checked and unchecked exceptions for all GWT Services.


I found solution which suggests to extend RemoteServiceServlet and override default exceptions flow. But I find this solution too time-consuming. Does anybode know any easier variant?

解决方案

On the server side, we have a subclass of RemoteServiceServlet that we use for all service implementations. You mention that it seems time-consuming, but here's what the code looks like. You do it once and you're done.

@Override
protected void doUnexpectedFailure(Throwable t) {
    t.printStackTrace(System.err);
    super.doUnexpectedFailure(t);
}

Note: We don't actually send it to System.err and you probably shouldn't either, but you get the idea.

On the client side, we use a subclass of AsyncCallback called AsyncSuccessCallback. It handles the onFailure case uniformly for most of our RPC calls. Most of our callback code can deal with the onSuccess case knowing that onFailure is handled. It also provides a single place to change this implementation later.

public abstract class AsyncSuccessCallback<T> implements AsyncCallback<T> {

    public void onFailure(Throwable t) {
        handleException(t);
    }

    protected void handleException(Throwable t) {
         Window.alert(t.getMessage());
    }

}

Note: We don't actually use Window.alert, but again, you get the idea. What we do in this case is display a GWT DialogBox that displays a form that does a POST to a different server that accepts error reports. The form allows the user to type a description of what they were doing when the error occurred.

On the client side, if you want to get the stack trace, you need to write a little extra code:

// for lineEnding, use "<br>" for HTML, "\n" for text
public static final String getStackTrace(Throwable t, String lineEnding) {
    Object[] stackTrace = t.getStackTrace();
    if (stackTrace != null) {
        StringBuilder output = new StringBuilder();
        for (Object line : stackTrace) {
            output.append(line);
            output.append(lineEnding);
        }
        return output.toString();
    } else {
        return "[stack unavailable]";
    }
}

这篇关于GWT服务异常记录的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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