feat:doric android and iOS's monitor add source parameter when exception

This commit is contained in:
pengfei.zhou 2020-02-22 15:37:35 +08:00 committed by osborn
parent f65b116607
commit abb1873b49
15 changed files with 139 additions and 50 deletions

View File

@ -73,13 +73,7 @@ public class DoricNativeDriver implements IDoricDriver {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<JSDecoder>() {
@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<Boolean> createContext(final String contextId, final String script, final String source) {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
@ -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;
}

View File

@ -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);
}
}

View File

@ -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.

View File

@ -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();
}

View File

@ -89,6 +89,24 @@ public class DoricBridgeExtension {
}
};
AsyncResult<JavaValue> asyncResult = context.getDriver().asyncCall(callable, doricMethod.thread());
asyncResult.setCallback(new AsyncResult.Callback<JavaValue>() {
@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()));

View File

@ -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<JSDecoder>() {
@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<JSDecoder>() {
@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() {
}
});
}
}

View File

@ -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);
}

View File

@ -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

View File

@ -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<JSValue *> *)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;

View File

@ -11,7 +11,17 @@ typedef NS_ENUM(NSInteger, DoricLogType) {
};
@protocol DoricMonitorProtocol <NSObject>
- (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

View File

@ -80,8 +80,8 @@ @interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol>
@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 <DoricMonitorProtocol>)monitor {
[self.monitors addObject:monitor];
}
- (void)onException:(NSException *)exception {
- (void)onException:(NSException *)exception source:(NSString *)source {
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onException:exception];
[monitor onException:exception source:source];
}
}

View File

@ -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]];
}

View File

@ -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];
}
};

View File

@ -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

View File

@ -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(), ^{