Java – best practices for GWT service exception logging

I decided to add the logging system to my GWT service layer First, I want to record all exceptions thrown from this layer I have an object similar to spring's servletdispatcher, which calls other services I thought I could add logging there, but I realized that the GWT service would include the checked exception in the servletresponse and uncheck it as unexpected exception

Can anyone share his experience? What is the best way to record checked and unchecked exceptions for all GWT services?

I found that the solution suggested extending the remoteserviceservlet and overriding the default exception flow But I think this solution is too time-consuming Do residents know any easier variants?

Solution

On the server side, we have a subclass of remoteserviceservlet for all service implementations You mention that it seems time-consuming, but it'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 didn't actually send it to system Err, you probably shouldn't, but you'll get the idea

On the client side, we use the subclass asyncsuccesscallback Most of its RPC calls handle onfailure cases uniformly Most of our callback code can handle onsuccess events until onfailure is processed It also provides a single place to change this implementation

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 get the idea again In this case, what we do is display a GWT Dialog@R_934_2419 @, the dialog box displays a form that performs post on different servers that accept error reports This form allows users to type a description of what they are doing when an error occurs

On the client side, if you want to get the stack trace, you need to write some additional 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]";
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>