diff --git a/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java b/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java index c7b85aff..d69f8638 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricNativeDriver.java @@ -73,13 +73,7 @@ public class DoricNativeDriver implements IDoricDriver { return AsyncCall.ensureRunInHandler(mJSHandler, new Callable() { @Override public JSDecoder call() { - try { - return doricJSEngine.invokeDoricMethod(method, args); - } catch (Exception e) { - doricJSEngine.getRegistry().onException(e); - doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("invokeDoricMethod(%s,...),error is %s", method, e.getLocalizedMessage())); - return new JSDecoder(null); - } + return doricJSEngine.invokeDoricMethod(method, args); } }); } @@ -97,6 +91,14 @@ public class DoricNativeDriver implements IDoricDriver { } } + private String sourceWithContextId(String contextId) { + DoricContext context = DoricContextManager.getContext(contextId); + if (context == null) { + return "Unknown:" + contextId; + } + return context.getSource(); + } + @Override public AsyncResult createContext(final String contextId, final String script, final String source) { return AsyncCall.ensureRunInHandler(mJSHandler, new Callable() { @@ -106,7 +108,7 @@ public class DoricNativeDriver implements IDoricDriver { doricJSEngine.prepareContext(contextId, script, source); return true; } catch (Exception e) { - doricJSEngine.getRegistry().onException(e); + doricJSEngine.getRegistry().onException(sourceWithContextId(contextId), e); doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("createContext %s error is %s", source, e.getLocalizedMessage())); return false; } @@ -123,7 +125,7 @@ public class DoricNativeDriver implements IDoricDriver { doricJSEngine.destroyContext(contextId); return true; } catch (Exception e) { - doricJSEngine.getRegistry().onException(e); + doricJSEngine.getRegistry().onException(sourceWithContextId(contextId), e); doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("destroyContext %s error is %s", contextId, e.getLocalizedMessage())); return false; } diff --git a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java index 6a0a5f36..9a827804 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -145,7 +145,7 @@ public class DoricRegistry { } - public void setEnvironmentVariabel(String key, Object val) { + public void setEnvironmentVariable(String key, Object val) { extendedEnvValues.put(key, val); } @@ -157,9 +157,9 @@ public class DoricRegistry { this.monitors.add(monitor); } - public void onException(Exception e) { + public void onException(String source, Exception e) { for (IDoricMonitor monitor : this.monitors) { - monitor.onException(e); + monitor.onException(source, e); } } diff --git a/doric-android/doric/src/main/java/pub/doric/IDoricMonitor.java b/doric-android/doric/src/main/java/pub/doric/IDoricMonitor.java index ba670489..8bc1002f 100644 --- a/doric-android/doric/src/main/java/pub/doric/IDoricMonitor.java +++ b/doric-android/doric/src/main/java/pub/doric/IDoricMonitor.java @@ -24,10 +24,11 @@ public interface IDoricMonitor { /** * Called when native or js exception occurred in doric * - * @param e exception which is thrown within doric sdk + * @param source Which source file + * @param e exception which is thrown within doric sdk * @see com.github.pengfeizhou.jscore.JSRuntimeException */ - void onException(Exception e); + void onException(String source, Exception e); /** * @param type The priority/type of this log message. diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 4addd212..c6cfe3cf 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -34,6 +34,8 @@ import java.util.ArrayList; import java.util.Map; import pub.doric.Doric; +import pub.doric.DoricContext; +import pub.doric.DoricContextManager; import pub.doric.DoricRegistry; import pub.doric.IDoricMonitor; import pub.doric.extension.bridge.DoricBridgeExtension; @@ -130,7 +132,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time break; } } catch (Exception e) { - mDoricRegistry.onException(e); + mDoricRegistry.onException("Log", e); } return null; } @@ -154,7 +156,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name); return new JavaValue(true); } catch (Exception e) { - mDoricRegistry.onException(e); + mDoricRegistry.onException("Require", e); return new JavaValue(false); } } @@ -168,7 +170,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time args[1].number().longValue(), args[2].bool()); } catch (Exception e) { - mDoricRegistry.onException(e); + mDoricRegistry.onException("Timer", e); } return null; } @@ -179,7 +181,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time try { mTimerExtension.clearTimer(args[0].number().longValue()); } catch (Exception e) { - mDoricRegistry.onException(e); + mDoricRegistry.onException("Timer", e); } return null; } @@ -187,6 +189,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_BRIDGE, new JavaFunction() { @Override public JavaValue exec(JSDecoder[] args) { + String source = "Unknown"; try { String contextId = args[0].string(); String module = args[1].string(); @@ -195,7 +198,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time JSDecoder jsDecoder = args[4]; return mDoricBridgeExtension.callNative(contextId, module, method, callbackId, jsDecoder); } catch (Exception e) { - mDoricRegistry.onException(e); + mDoricRegistry.onException("Bridge", e); } return null; } @@ -209,7 +212,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time String libJS = DoricUtils.readAssetFile(DoricConstant.DORIC_BUNDLE_LIB); mDoricJSE.loadJS(packageModuleScript(libName, libJS), "Module://" + libName); } catch (Exception e) { - mDoricRegistry.onException(e); + mDoricRegistry.onException("Init Environment", e); } } @@ -260,7 +263,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time try { invokeDoricMethod(DoricConstant.DORIC_TIMER_CALLBACK, timerId); } catch (Exception e) { - mDoricRegistry.onException(e); + mDoricRegistry.onException("Timer", e); mDoricRegistry.onLog( Log.ERROR, String.format("Timer Callback error:%s", e.getLocalizedMessage())); @@ -272,7 +275,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time } @Override - public void onException(Exception e) { + public void onException(String source, Exception e) { + Log.e(DoricJSEngine.class.getSimpleName(), "In source file: " + source); e.printStackTrace(); } diff --git a/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricBridgeExtension.java b/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricBridgeExtension.java index 75b687ea..ba77bb2c 100644 --- a/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricBridgeExtension.java +++ b/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricBridgeExtension.java @@ -89,6 +89,24 @@ public class DoricBridgeExtension { } }; AsyncResult asyncResult = context.getDriver().asyncCall(callable, doricMethod.thread()); + asyncResult.setCallback(new AsyncResult.Callback() { + @Override + public void onResult(JavaValue result) { + + } + + @Override + public void onError(Throwable t) { + context.getDriver().getRegistry().onException( + context.getSource(), + t instanceof Exception ? (Exception) t : new RuntimeException(t)); + } + + @Override + public void onFinish() { + + } + }); if (asyncResult.hasResult()) { return asyncResult.getResult(); } @@ -102,7 +120,7 @@ public class DoricBridgeExtension { try { return DoricUtils.toJavaObject(clz, jsDecoder); } catch (Exception e) { - context.getDriver().getRegistry().onException(e); + context.getDriver().getRegistry().onException(context.getSource(), e); context.getDriver().getRegistry().onLog( Log.ERROR, String.format("createParam error:%s", e.getLocalizedMessage())); diff --git a/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricPromise.java b/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricPromise.java index bdb136cf..d74fa87d 100644 --- a/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricPromise.java +++ b/doric-android/doric/src/main/java/pub/doric/extension/bridge/DoricPromise.java @@ -15,9 +15,13 @@ */ package pub.doric.extension.bridge; +import android.util.Log; + import pub.doric.DoricContext; +import pub.doric.async.AsyncResult; import pub.doric.utils.DoricConstant; +import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JavaValue; /** @@ -39,9 +43,26 @@ public class DoricPromise { params[0] = context.getContextId(); params[1] = callbackId; System.arraycopy(javaValue, 0, params, 2, javaValue.length); - context.getDriver().invokeDoricMethod( - DoricConstant.DORIC_BRIDGE_RESOLVE, - params); + context.getDriver() + .invokeDoricMethod( + DoricConstant.DORIC_BRIDGE_RESOLVE, + params) + .setCallback(new AsyncResult.Callback() { + @Override + public void onResult(JSDecoder result) { + + } + + @Override + public void onError(Throwable t) { + context.getDriver().getRegistry().onException(context.getSource(), t instanceof Exception ? (Exception) t : new RuntimeException(t)); + } + + @Override + public void onFinish() { + + } + }); } public void reject(JavaValue... javaValue) { @@ -49,8 +70,25 @@ public class DoricPromise { params[0] = context.getContextId(); params[1] = callbackId; System.arraycopy(javaValue, 0, params, 2, javaValue.length); - context.getDriver().invokeDoricMethod( - DoricConstant.DORIC_BRIDGE_REJECT, - params); + context.getDriver() + .invokeDoricMethod( + DoricConstant.DORIC_BRIDGE_REJECT, + params) + .setCallback(new AsyncResult.Callback() { + @Override + public void onResult(JSDecoder result) { + + } + + @Override + public void onError(Throwable t) { + context.getDriver().getRegistry().onException(context.getSource(), t instanceof Exception ? (Exception) t : new RuntimeException(t)); + } + + @Override + public void onFinish() { + + } + }); } } diff --git a/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java b/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java index 0606a296..3f993a9c 100644 --- a/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java +++ b/doric-android/doric/src/main/java/pub/doric/plugin/ShaderPlugin.java @@ -78,9 +78,9 @@ public class ShaderPlugin extends DoricJavaPlugin { @Override public void onError(Throwable t) { - if (t instanceof Exception) { - getDoricContext().getDriver().getRegistry().onException((Exception) t); - } + getDoricContext().getDriver().getRegistry().onException( + getDoricContext().getSource(), + t instanceof Exception ? (Exception) t : new RuntimeException(t)); getDoricContext().getDriver().getRegistry().onLog( Log.ERROR, String.format("Shader.render:error%s", t.getLocalizedMessage())); @@ -92,7 +92,7 @@ public class ShaderPlugin extends DoricJavaPlugin { } }); } catch (Exception e) { - getDoricContext().getDriver().getRegistry().onException(e); + getDoricContext().getDriver().getRegistry().onException(getDoricContext().getSource(), e); getDoricContext().getDriver().getRegistry().onLog( Log.ERROR, String.format("Shader.render:error%s", e.getLocalizedMessage()) @@ -173,7 +173,7 @@ public class ShaderPlugin extends DoricJavaPlugin { } } } catch (ArchiveException e) { - getDoricContext().getDriver().getRegistry().onException(e); + getDoricContext().getDriver().getRegistry().onException(getDoricContext().getSource(), e); } return new JavaValue(true); } diff --git a/doric-iOS/Pod/Classes/DoricDriver.h b/doric-iOS/Pod/Classes/DoricDriver.h index 3355001e..f04d72dc 100644 --- a/doric-iOS/Pod/Classes/DoricDriver.h +++ b/doric-iOS/Pod/Classes/DoricDriver.h @@ -54,6 +54,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)disconnectDevKit; - (void)ensureSyncInMainQueue:(dispatch_block_t)block; + +- (NSString *)aliasWithContextId:(NSString *)contextId; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/DoricDriver.m b/doric-iOS/Pod/Classes/DoricDriver.m index 3e979dd6..c8d65126 100644 --- a/doric-iOS/Pod/Classes/DoricDriver.m +++ b/doric-iOS/Pod/Classes/DoricDriver.m @@ -24,6 +24,7 @@ #import "DoricJSEngine.h" #import "DoricConstant.h" #import "DoricWSClient.h" +#import "DoricContextManager.h" @interface DoricDriver () @property(nonatomic, strong) DoricJSEngine *jsExecutor; @@ -62,6 +63,10 @@ + (instancetype)instance { return ret; } +- (NSString *)aliasWithContextId:(NSString *)contextId { + return [[DoricContextManager instance] getContext:contextId].source; +} + - (DoricAsyncResult *)invokeDoricMethod:(NSString *)method arguments:(va_list)args { DoricAsyncResult *ret = [[DoricAsyncResult alloc] init]; NSMutableArray *array = [[NSMutableArray alloc] init]; @@ -78,7 +83,6 @@ + (instancetype)instance { [ret setupResult:jsValue]; } @catch (NSException *exception) { [ret setupError:exception]; - [self.jsExecutor.registry onException:exception]; } }); return ret; @@ -111,7 +115,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString [ret setupResult:jsValue]; } @catch (NSException *exception) { [ret setupError:exception]; - [self.jsExecutor.registry onException:exception]; + [self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]]; } }); return ret; @@ -134,7 +138,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString [ret setupResult:jsValue]; } @catch (NSException *exception) { [ret setupError:exception]; - [self.jsExecutor.registry onException:exception]; + [self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]]; } }); return ret; @@ -151,7 +155,7 @@ - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scr [ret setupResult:@YES]; } @catch (NSException *exception) { [ret setupError:exception]; - [self.jsExecutor.registry onException:exception]; + [self.jsExecutor.registry onException:exception source:source]; } }); return ret; @@ -168,7 +172,7 @@ - (DoricAsyncResult *)destroyContext:(NSString *)contextId { [ret setupResult:@YES]; } @catch (NSException *exception) { [ret setupError:exception]; - [self.jsExecutor.registry onException:exception]; + [self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]]; } }); return ret; diff --git a/doric-iOS/Pod/Classes/DoricMonitorProtocol.h b/doric-iOS/Pod/Classes/DoricMonitorProtocol.h index 964e2c2b..08b5d439 100644 --- a/doric-iOS/Pod/Classes/DoricMonitorProtocol.h +++ b/doric-iOS/Pod/Classes/DoricMonitorProtocol.h @@ -11,7 +11,17 @@ typedef NS_ENUM(NSInteger, DoricLogType) { }; @protocol DoricMonitorProtocol -- (void)onException:(NSException *)exception; +/** + * Called when native or js exception occurred in doric + * + * @param source Which source file + * @param e exception which is thrown within doric sdk + */ +- (void)onException:(NSException *)exception source:(NSString *)source; +/** + * @param type The priority/type of this log message. + * @param message The message you would like logged. + */ - (void)onLog:(DoricLogType)type message:(NSString *)message; @end \ No newline at end of file diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index e99a277c..6caee381 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -80,8 +80,8 @@ @interface DoricDefaultMonitor : NSObject @end @implementation DoricDefaultMonitor -- (void)onException:(NSException *)exception { - DoricLog(@"DefaultMonitor - onException - %@", exception.reason); +- (void)onException:(NSException *)exception source:(NSString *)source { + DoricLog(@"DefaultMonitor - source: %@- onException - %@", source, exception.reason); } - (void)onLog:(DoricLogType)type message:(NSString *)message { @@ -187,9 +187,9 @@ - (void)registerMonitor:(id )monitor { [self.monitors addObject:monitor]; } -- (void)onException:(NSException *)exception { +- (void)onException:(NSException *)exception source:(NSString *)source { for (id monitor in self.monitors) { - [monitor onException:exception]; + [monitor onException:exception source:source]; } } diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m index 5c121f61..748bbc63 100644 --- a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m +++ b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m @@ -139,7 +139,7 @@ - (void)initDoricEnvironment { [self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent] source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]]; } @catch (NSException *exception) { - [self.registry onException:exception]; + [self.registry onException:exception source:@"InitEnvironment"]; } } @@ -206,7 +206,7 @@ - (void)callbackTimer:(NSTimer *)timer { @try { [self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil]; } @catch (NSException *exception) { - [self.registry onException:exception]; + [self.registry onException:exception source:@"Timer"]; [self.registry onLog:DoricLogTypeError message:[NSString stringWithFormat:@"Timer Callback error:%@", exception.reason]]; } diff --git a/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m b/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m index 51cd4278..31063e7a 100644 --- a/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m +++ b/doric-iOS/Pod/Classes/Extension/DoricBridgeExtension.m @@ -87,7 +87,7 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met [invocation invoke]; } @catch (NSException *exception) { DoricLog(@"CallNative Error:%@", exception.reason); - [context.driver.registry onException:exception]; + [context.driver.registry onException:exception source:context.source]; } }; diff --git a/doric-iOS/Pod/Classes/Plugin/DoricPromise.m b/doric-iOS/Pod/Classes/Plugin/DoricPromise.m index 5df3b2eb..5c3c580e 100644 --- a/doric-iOS/Pod/Classes/Plugin/DoricPromise.m +++ b/doric-iOS/Pod/Classes/Plugin/DoricPromise.m @@ -40,10 +40,20 @@ - (instancetype)initWithContext:(DoricContext *)context callbackId:(NSString *)c } - (void)resolve:(id)result { - [self.context.driver invokeDoricMethod:DORIC_BRIDGE_RESOLVE, self.context.contextId, self.callbackId, result, nil]; + [[self.context.driver invokeDoricMethod:DORIC_BRIDGE_RESOLVE, self.context.contextId, self.callbackId, result, nil] + setExceptionCallback:^(NSException *e) { + [self.context.driver.registry + onException:e + source:self.context.source]; + }]; } - (void)reject:(id)result { - [self.context.driver invokeDoricMethod:DORIC_BRIDGE_REJECT, self.context.contextId, self.callbackId, result, nil]; + [[self.context.driver invokeDoricMethod:DORIC_BRIDGE_REJECT, self.context.contextId, self.callbackId, result, nil] + setExceptionCallback:^(NSException *e) { + [self.context.driver.registry + onException:e + source:self.context.source]; + }]; } @end diff --git a/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m index e397a15d..b0d25f1a 100644 --- a/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m +++ b/doric-iOS/Pod/Classes/Plugin/DoricShaderPlugin.m @@ -108,7 +108,7 @@ - (id)findClass:(Class)clz target:(id)target method:(NSString *)name promise:(Do [invocation invoke]; } @catch (NSException *exception) { DoricLog(@"CallNative Error:%@", exception.reason); - [self.doricContext.driver.registry onException:exception]; + [self.doricContext.driver.registry onException:exception source:self.doricContext.source]; } }; dispatch_async(dispatch_get_main_queue(), ^{