Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Cordova exec handled all wrong ... should not be returning booleans or false #1

Closed
tripflex opened this issue Jan 8, 2018 · 0 comments
Labels
bug Something isn't working

Comments

@tripflex
Copy link
Owner

tripflex commented Jan 8, 2018

The ONLY time we should be returning FALSE is when it's an invalid action, otherwise

Any other action returns false and results in an INVALID_ACTION error, which translates to an error callback invoked on the JavaScript side

So that's why I was getting all these Invalid Action errors and error not handled in promises ... due to incorrect Android codebase returning FALSE when it should always return TRUE ... even at that ... almost all functions should be void not boolean bc there's no reason to return true/false as true should always be returned to exec because the callback handles the error! GAWRR!

package org.apache.cordova.plugin;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
* This class echoes a string called from JavaScript.
*/
public class Echo extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("echo")) {
        String message = args.getString(0);
        this.echo(message, callbackContext);
        return true;
    }
    return false;
}

private void echo(String message, CallbackContext callbackContext) {
    if (message != null && message.length() > 0) {
        callbackContext.success(message);
    } else {
        callbackContext.error("Expected one non-empty string argument.");
    }
}
}

The necessary imports at the top of the file extends the class from CordovaPlugin, whose execute() method it overrides to receive messages from exec(). The execute() method first tests the value of action, for which in this case there is only one valid echo value. Any other action returns false and results in an INVALID_ACTION error, which translates to an error callback invoked on the JavaScript side.

Next, the method retrieves the echo string using the args object's getString method, specifying the first parameter passed to the method. After the value is passed to a private echo method, it is parameter-checked to make sure it is not null or an empty string, in which case callbackContext.error() invokes JavaScript's error callback. If the various checks pass, the callbackContext.success() passes the original message string back to JavaScript's success callback as a parameter.

https://cordova.apache.org/docs/en/latest/guide/platforms/android/plugin.html

@tripflex tripflex added the bug Something isn't working label Jan 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant