Skip to content

4. Error processing

Andrii Konovalenko edited this page Apr 12, 2017 · 3 revisions

1. Main information

During operation, errors may occur. EasyNet shares errors in 2 categories: Error and Failed.

Error - This is a server error. This is not a critical error, but, on the contrary, a scenario that should arise when validating fields, missing mandatory data, etc. Server errors start with 4** status code.

Failed - This is a critical error that could occur in the absence of an Internet connection, a connection failure, with the server turned off, error while parsing data, which could lead to the completion of the flow of query execution and other cases. If such errors occurred, the application must notify the user that it is impossible to work and check the connection. Errors enum contains main errors fields, with will come in case failed.

2. Examples

EasyNet.get("path").start(new NCallbackGson<PinModel>(PinModel.class) {
            @Override
            public void onSuccess(PinModel model, NResponseModel responseModel) {
            }

            @Override
            public void onError(NResponseModel responseModel) {
                switch (responseModel.getStatusCode()) {
                    case 401:
                        break;
                    case 403:
                        break;
                    case 404:
                        break;
                }
            }

            @Override
            public void onFailed(NRequestModel nRequestModel, NErrors error) {
                Toast.makeText(getContext(), "Check internet connection", Toast.LENGTH_LONG).show();
            }
        });

It is not always convenient to perform error handling on every request. For this, there are basic handlers that help reduce and structure the error handling code.

Examples of using:

public class App extends Application {
    @Override
    public void onCreate() {
               EasyNet.getInstance()
                .setDefaultNBuilderListener(new EasyNet.NBuilderDefaultListener() {
                    @Override
                    public Request defaultConfig(Request request) {
                        return request
                                .setHost(APIConfig.API_URL)
                                .addHeader(NConst.ACCEPT_TYPE, NConst.MIME_TYPE_JSON)
                                .setContentType(NConst.MIME_TYPE_JSON)
                                .addHeader(APIConfig.HEADER_TOKEN, PreferencesManager.readToken(getApplicationContext()));
                    }
                })
                .addOnErrorDefaultListener(new EasyNet.OnErrorDefaultListenerWithCode(401) {
                    @Override
                    public void onError(NResponseModel responseModel) {
                        EasyNet.getInstance().cancelAllTasks();
                        Toast.makeText(App.this, "unauthorized", Toast.LENGTH_SHORT).show();
                        PreferencesManager.clear(App.this);
                        Intent intent = new Intent(App.this, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
                                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }
                })
    }
}

In this case, we processed error 401, which occurs when you need to authorize. This error is always handled and other client handlers can process it no more.

Also we can handle and failed:

               ...
               .setDefaultOnFailedListener(new EasyNet.OnFailedDefaultListener() {
                    @Override
                    public boolean onFailed(NRequestModel nRequestModel, NErrors error) {
                        Toast.makeText(App.this, "No internet connection", Toast.LENGTH_SHORT)
                                .show();
                        return true;
                    }
                });

3. Processing sequence

The default handler methods are called first. After that, execution is passed by the method of the request call. If on some request you want to bypass the handlers by default, call enableDefaultListeners(false). By default enableDefaultListeners sets true.

 EasyNet.get()
            .enableDefaultListeners(true)
            .start(callback);

In DefaultOnFailedListener method onFailed returns boolean value. If you want to continue processing sequence you need return true, else if you want to stop processing on default listener, return false;

Clone this wiki locally