Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

codenameone - Distinguish between server-side errors and connection problems

As described in the Codename One blog post "Terse Table, Radar Chart and Networking Enhancements":

Better Error Code Handling

Up until recently if we got an error response code it wasn’t sent through the global error handler and was handled via the local error handling chain first. This is no longer the case and these errors are now handled correctly.

However, if you relied on that misbehavior of older versions we have setHandleErrorCodesInGlobalErrorHandler(boolean). This defaults to true, you can set it to false to change the default behavior.

So, the global error handler is invoked both by server-side errors and connection problems. It's a very good thing to be able to manage networks errors in the most generic way, however I need to distinguish between server-side errors and connection problems.

After some testing, I wrote the following code, tested on Simulator, Android and iOS:

addNetworkErrorListener(err -> {
            // prevents the event from propagating
            err.consume();

            if (err.getError() != null) {
                // this is the case of a network error,
                // like: java.io.IOException: Unreachable
                Log.p("Error connectiong to: " + err.getConnectionRequest().getUrl());
                // maybe there are connectivity issues, let's try again
                ToastBar.showInfoMessage("Trying to reconnect...");
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        err.getConnectionRequest().retry();
                    }
                }, 2000);
            } else {
                // this is the case of a server error
                // logs the error
                Log.p("REST ERROR
URL:" + err.getConnectionRequest().getUrl()
                        + "
Method: " + err.getConnectionRequest().getHttpMethod()
                        + "
Request body: " + err.getConnectionRequest().getRequestBody()
                        + "
Response code: " + err.getConnectionRequest().getResponseCode()
                        + "
Response message: " + StringUtilities.toString(err.getConnectionRequest().getResponseData()),
                        Log.ERROR);

                Log.sendLogAsync();
                ToastBar.showErrorMessage("Server Error", 10000);
            }
        });

In the tested platforms, server-side errors are reported, but no further action follows; connectivity errors are both reported and managed by retrying the connection every two seconds.

So, that code works.

Anyway, what I don't like is the assumption that err.getError() != null is true for connectivity errors, false for server-side errors. I came to this assumption after several tests, but I didn't find it documented anywhere.

So my question is whether:

  1. my assumption is always correct;
  2. there is a better way to distinguish server-side errors from connectivity errors.

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...