72 lines
1.7 KiB
Java
72 lines
1.7 KiB
Java
package org.n0m.pawsqlite;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.StringWriter;
|
|
import java.io.PrintWriter;
|
|
|
|
import org.apache.cordova.CallbackContext;
|
|
|
|
|
|
public class CallbackWrapper {
|
|
private CallbackContext callbackContext;
|
|
|
|
CallbackWrapper(CallbackContext callbackContext) {
|
|
this.callbackContext = callbackContext;
|
|
}
|
|
|
|
public void error(String name) {
|
|
error(name, null, null);
|
|
}
|
|
public void error(String name, String message) {
|
|
error(name, message, null);
|
|
}
|
|
|
|
public void error(String name, Exception e) {
|
|
String message = null;
|
|
if (e != null) {
|
|
message = e.getMessage();
|
|
}
|
|
error(name, message, e);
|
|
}
|
|
|
|
public void error(String name, String message, Exception e) {
|
|
try {
|
|
JSONObject response = new JSONObject();
|
|
response.put("name", name);
|
|
|
|
if (message != null) {
|
|
response.put("message", message);
|
|
}
|
|
|
|
if (e != null) {
|
|
StringWriter stringWriter = new StringWriter();
|
|
e.printStackTrace(new PrintWriter(stringWriter));
|
|
response.put("trace", stringWriter.toString());
|
|
}
|
|
|
|
callbackContext.error(response);
|
|
} catch (JSONException jsonException) {
|
|
callbackContext.error("Encountered error and unable to generate " +
|
|
"response");
|
|
}
|
|
}
|
|
|
|
public void success() {
|
|
JSONObject response = new JSONObject();
|
|
success(response);
|
|
}
|
|
|
|
public void success(JSONObject response) {
|
|
if (!response.has("success")) {
|
|
try {
|
|
response.put("success", true);
|
|
} catch (JSONException e) {
|
|
// Will never happen
|
|
}
|
|
}
|
|
callbackContext.success(response);
|
|
}
|
|
} |