1
votes

My app tries to do an async network request at app launch, using RequestBuilder request = Rest.get, as described in this article: https://www.codenameone.com/blog/rest-api-error-handling.html. If I power off my testing server, at the app launch I have this exception:

[Network Thread] 0:0:0,586 - Exception: java.net.ConnectException - Connection refused

but no info, message or dialog is shown to the user. The following boilerplate code in the init() is not called:

    addNetworkErrorListener(err -> {
        // prevent the event from propagating
        err.consume();
        if (err.getError() != null) {
            Log.e(err.getError());
        }
        Log.sendLogAsync();
        Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
    });

If I provide a custom network error handler, with request.onError(networkError);, the issue is the same: my network error handler is not called. I tried to implement it so (it's very similar to the above boilerplate code):

private static final ActionListener<NetworkEvent> networkError = new ActionListener<NetworkEvent>() {
        @Override
        public void actionPerformed(NetworkEvent err) {
            Log.p("NETWORK ERROR connecting to the server");
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            DialogUtilities.genericNetworkError();
        }
    };

I tried using the Simulator with the server offline. Then I tried with Android and iOS app, disconnecting the device from the Internet, but also in this case there is no message to the user. What's wrong?

Note that the error code handlers used with onErrorCodeBytes, onErrorCodeJSON and onErrorCodeString seem to work correctly (they are invoked, for example, if I have a 404 http code).

1

1 Answers

1
votes

Did you define onError? It should be invoked for exceptions and might override the global error handling logic once defined.

I tried this case with wifi turned off and it seems to have worked correctly:

Button test = new Button("Test");
test.addActionListener(e -> {
    Rest.get("https://www.codenameone.com/").
        onError(ee -> {  
            if(ee.getError() != null) {
                Log.e(ee.getError());
            }
        }).
        fetchAsBytes(call -> {});
});