diff --git a/app/jxcore_cordova.js b/app/jxcore_cordova.js index 5b23c9c..4ee4b15 100644 --- a/app/jxcore_cordova.js +++ b/app/jxcore_cordova.js @@ -180,9 +180,9 @@ internal_methods['loadMainFile'] = function (filePath, callback_) { require(path.join(process.cwd(), filePath)); } catch (e) { result = false; + Error.captureStackTrace(e); err = e; - Error.captureStackTrace(err); - console.error("loadMainFile", e); + JXMobile('OnError').callNative(e.message, JSON.stringify(e.stack)); } callback_(result, !err ? null : err.message + "\n" + err.stack); }; @@ -192,39 +192,43 @@ JXMobile.executeJSON = function (json, callbackId) { var internal = internal_methods[json.methodName]; var fnc = jx_methods[json.methodName]; - - if (internal) { - var cb = new MakeCallback(callbackId).callback - json.params.push(cb); - internal.apply(null, json.params); - return; - } else if (fnc) { - if (!fnc.is_synced) { - if (!json.params || (json.params.length == 1 && json.params[0] === null)) { - json.params = []; + try { + if (internal) { + var cb = new MakeCallback(callbackId).callback + json.params.push(cb); + internal.apply(null, json.params); + return; + } else if (fnc) { + if (!fnc.is_synced) { + if (!json.params || (json.params.length == 1 && json.params[0] === null)) { + json.params = []; + } + json.params[json.params.length] = new MakeCallback(callbackId).callback; } - json.params[json.params.length] = new MakeCallback(callbackId).callback; - } - var ret_val = fnc.method.apply(null, json.params); - if (fnc.is_synced && callbackId) { - new MakeCallback(callbackId).callback(ret_val); - } else { - return ret_val; - } - return; - } else if (json.methodName && json.methodName.length>3 && json.methodName.substr(0,3) === "RC-") { - var cb = new MakeCallback(callbackId).callback - json.params.push(cb); - fnc = ui_methods[json.methodName.substr(3)]; - if (fnc && fnc.returnCallback) { - fnc.returnCallback.apply(null, json.params); - delete ui_methods[json.methodName.substr(3)]; + var ret_val = fnc.method.apply(null, json.params); + if (fnc.is_synced && callbackId) { + new MakeCallback(callbackId).callback(ret_val); + } else { + return ret_val; + } return; + } else if (json.methodName && json.methodName.length>3 && json.methodName.substr(0,3) === "RC-") { + var cb = new MakeCallback(callbackId).callback + json.params.push(cb); + fnc = ui_methods[json.methodName.substr(3)]; + if (fnc && fnc.returnCallback) { + fnc.returnCallback.apply(null, json.params); + delete ui_methods[json.methodName.substr(3)]; + return; + } } - } - console.error("JXcore: Method Doesn't Exist [", json.methodName, "] Did you register it?"); + throw new Error("JXcore: Method Doesn't Exist [", json.methodName, "] Did you register it?"); + } catch(e) { + Error.captureStackTrace(e); + JXMobile('OnError').callNative(e.message, JSON.stringify(e.stack)); + } }; console.warn("Platform", process.platform); @@ -421,4 +425,9 @@ if (isAndroid) { jxcore.tasks.register(process.setPaths); } +process.on('uncaughtException', function (e) { + Error.captureStackTrace(e); + JXMobile('OnError').callNative(e.message, JSON.stringify(e.stack)); +}); + console.log("JXcore Cordova bridge is ready!"); \ No newline at end of file diff --git a/plugin.xml b/plugin.xml index 7843f41..47f551d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -26,6 +26,8 @@ + + @@ -58,6 +60,7 @@ + diff --git a/src/android/java/io/jxcore/node/JXMobile.java b/src/android/java/io/jxcore/node/JXMobile.java new file mode 100644 index 0000000..6902a78 --- /dev/null +++ b/src/android/java/io/jxcore/node/JXMobile.java @@ -0,0 +1,25 @@ +// License information is available from LICENSE file + +package io.jxcore.node; + +import io.jxcore.node.jxcore.JXcoreCallback; + +import java.util.ArrayList; + +import android.annotation.SuppressLint; +import android.util.Log; + +public class JXMobile { + public static void Initialize() { + jxcore.RegisterMethod("OnError", new JXcoreCallback() { + @SuppressLint("NewApi") + @Override + public void Receiver(ArrayList params, String callbackId) { + String message = (String) params.get(0); + String stack = (String) params.get(1); + + Log.e("jxcore", "Error!: " + message + "\nStack: " + stack); + } + }); + } +} \ No newline at end of file diff --git a/src/android/java/io/jxcore/node/JXcoreExtension.java b/src/android/java/io/jxcore/node/JXcoreExtension.java index d288cab..437d506 100644 --- a/src/android/java/io/jxcore/node/JXcoreExtension.java +++ b/src/android/java/io/jxcore/node/JXcoreExtension.java @@ -1,5 +1,11 @@ // License information is available from LICENSE file +/* + * This file is only a sample + * You are free to update it with the methods you need + * from the Native Mobile API + */ + package io.jxcore.node; import io.jxcore.node.jxcore.JXcoreCallback; diff --git a/src/android/java/io/jxcore/node/jxcore.java b/src/android/java/io/jxcore/node/jxcore.java index c007171..c24f291 100644 --- a/src/android/java/io/jxcore/node/jxcore.java +++ b/src/android/java/io/jxcore/node/jxcore.java @@ -221,6 +221,7 @@ public void Receiver(ArrayList params, String callbackId) { }); JXcoreExtension.LoadExtensions(); + JXMobile.Initialize(); if (!new_instance) return; diff --git a/src/ios/CDVJXcore.m b/src/ios/CDVJXcore.m index 34b28df..34d5d4d 100644 --- a/src/ios/CDVJXcore.m +++ b/src/ios/CDVJXcore.m @@ -17,6 +17,7 @@ #import #import "CDVJXcore.h" #import "JXcoreExtension.h" +#import "JXMobile.h" static CDVJXcore *activeDevice = nil; @@ -108,6 +109,8 @@ - (void)pluginInitialize { id extension = [[extensionClass alloc] init]; [extension defineMethods]; } + + [JXMobile defineMethods]; } - (void)Evaluate:(CDVInvokedUrlCommand *)command { diff --git a/src/ios/JXMobile.h b/src/ios/JXMobile.h new file mode 100644 index 0000000..90a4e13 --- /dev/null +++ b/src/ios/JXMobile.h @@ -0,0 +1,11 @@ +// See LICENSE file + +#ifndef JXcordova_JXMobile_h +#define JXcordova_JXMobile_h + +@interface JXMobile : NSObject +{} ++ (void) defineMethods; +@end + +#endif diff --git a/src/ios/JXMobile.m b/src/ios/JXMobile.m new file mode 100644 index 0000000..68da92c --- /dev/null +++ b/src/ios/JXMobile.m @@ -0,0 +1,20 @@ +// See LICENSE file + +#import +#import "JXcore.h" +#import "JXMobile.h" +#import "CDVJXcore.h" + +@implementation JXMobile +{} + ++ (void) defineMethods { + // Listen to Errors on the JS land + [JXcore addNativeBlock:^(NSArray *params, NSString *callbackId) { + NSString *errorMessage = (NSString*)[params objectAtIndex:0]; + NSString *errorStack = (NSString*)[params objectAtIndex:1]; + + NSLog(@"Error!: %@\nStack:%@\n", errorMessage, errorStack); + } withName:@"OnError"]; +} +@end \ No newline at end of file