Skip to content

Commit

Permalink
[TIMOB-25963] Handle java call exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Mathews committed Apr 18, 2018
1 parent e9e599c commit 0877998
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 28 deletions.
35 changes: 34 additions & 1 deletion android/runtime/v8/src/native/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ static void onPropertyChangedForProxy(Isolate* isolate, Local<String> property,
env->DeleteLocalRef(javaValue);
}

if (env->ExceptionCheck()) {
JSException::fromJavaException(isolate);
env->ExceptionClear();
return;
}

// Store new property value on JS internal map.
setPropertyOnProxy(isolate, property, value, proxyObject);
}
Expand Down Expand Up @@ -206,6 +212,12 @@ void Proxy::getIndexedProperty(uint32_t index, const PropertyCallbackInfo<Value>

proxy->unreferenceJavaObject(javaProxy);

if (env->ExceptionCheck()) {
JSException::fromJavaException(isolate);
env->ExceptionClear();
return;
}

Local<Value> result = TypeConverter::javaObjectToJsValue(isolate, env, value);
env->DeleteLocalRef(value);

Expand Down Expand Up @@ -237,6 +249,12 @@ void Proxy::setIndexedProperty(uint32_t index, Local<Value> value, const Propert
env->DeleteLocalRef(javaValue);
}

if (env->ExceptionCheck()) {
JSException::fromJavaException(isolate);
env->ExceptionClear();
return;
}

info.GetReturnValue().Set(value);
}

Expand Down Expand Up @@ -272,6 +290,12 @@ void Proxy::hasListenersForEventType(const v8::FunctionCallbackInfo<v8::Value>&

env->DeleteLocalRef(krollObject);
env->DeleteLocalRef(javaEventType);

if (env->ExceptionCheck()) {
JSException::fromJavaException(isolate);
env->ExceptionClear();
return;
}
}

void Proxy::onEventFired(const v8::FunctionCallbackInfo<v8::Value>& args)
Expand Down Expand Up @@ -312,6 +336,12 @@ void Proxy::onEventFired(const v8::FunctionCallbackInfo<v8::Value>& args)
if (isNew) {
env->DeleteLocalRef(javaEventData);
}

if (env->ExceptionCheck()) {
JSException::fromJavaException(isolate);
env->ExceptionClear();
return;
}
}

Local<FunctionTemplate> Proxy::inheritProxyTemplate(Isolate* isolate,
Expand Down Expand Up @@ -498,7 +528,10 @@ void Proxy::proxyOnPropertiesChanged(const v8::FunctionCallbackInfo<v8::Value>&

proxy->unreferenceJavaObject(javaProxy);

return;
if (env->ExceptionCheck()) {
JSException::fromJavaException(isolate);
env->ExceptionClear();
}
}

void Proxy::dispose(Isolate* isolate)
Expand Down
7 changes: 7 additions & 0 deletions android/runtime/v8/src/native/ProxyFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "AndroidUtil.h"
#include "JavaObject.h"
#include "JSException.h"
#include "JNIUtil.h"
#include "KrollBindings.h"
#include "Proxy.h"
Expand Down Expand Up @@ -166,6 +167,12 @@ jobject ProxyFactory::createJavaProxy(jclass javaClass, Local<Object> v8Proxy, c
env->DeleteLocalRef(javaArgs);
// We don't delete the global jclass reference...

if (env->ExceptionCheck()) {
JSException::fromJavaException(isolate);
env->ExceptionClear();
return NULL;
}

return javaProxy;
}

Expand Down
16 changes: 10 additions & 6 deletions android/runtime/v8/src/native/V8Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,19 @@ JNIEXPORT void JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeDi

JNIEXPORT jstring JNICALL Java_org_appcelerator_kroll_runtime_v8_V8Runtime_nativeStackTrace(JNIEnv *env, jobject self)
{
v8::Local<v8::StackTrace> frames = v8::StackTrace::CurrentStackTrace(V8Runtime::v8_isolate, 10);
if (!frames->GetFrameCount()) {
HandleScope scope(V8Runtime::v8_isolate);

Local<StackTrace> frames = StackTrace::CurrentStackTrace(V8Runtime::v8_isolate, 10);
if (frames.IsEmpty() || !frames->GetFrameCount()) {
frames = V8Runtime::exceptionStackTrace.Get(V8Runtime::v8_isolate);
}

std::string stack = V8Util::stackTraceString(frames);
if (!stack.empty()) {
return env->NewStringUTF(stack.c_str());
if (!frames.IsEmpty()) {
std::string stack = V8Util::stackTraceString(frames);
if (!stack.empty()) {
return env->NewStringUTF(stack.c_str());
}
}

return NULL;
}

Expand Down
4 changes: 4 additions & 0 deletions android/runtime/v8/src/native/V8Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ void V8Util::dispose()
}

std::string V8Util::stackTraceString(Local<StackTrace> frames) {
if (frames.IsEmpty()) {
return NULL;
}

std::stringstream stack;

for (int i = 0, count = frames->GetFrameCount(); i < count; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Arrays;
import java.util.LinkedList;

import org.appcelerator.kroll.KrollApplication;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollExceptionHandler;
import org.appcelerator.kroll.KrollRuntime;
Expand Down Expand Up @@ -47,26 +46,58 @@ public class TiExceptionHandler implements Handler.Callback, KrollExceptionHandl
private static boolean dialogShowing = false;
private static Handler mainHandler;

private static final String ERROR_TITLE = "title";
private static final String ERROR_MESSAGE = "message";
private static final String ERROR_SOURCENAME = "sourceName";
private static final String ERROR_LINE = "line";
private static final String ERROR_LINESOURCE = "lineSource";
private static final String ERROR_LINEOFFSET = "lineOffset";
private static final String ERROR_STACK = "stack";

private static final String fill(int count)
{
char[] string = new char[count];
Arrays.fill(string, ' ');
return new String(string);
}

private static String getError(KrollDict error)
public static final KrollDict getErrorDict(ExceptionMessage error)
{
final KrollDict dict = new KrollDict();
dict.put(ERROR_TITLE, error.title);
dict.put(ERROR_MESSAGE, error.message);
dict.put(ERROR_SOURCENAME, error.sourceName);
dict.put(ERROR_LINE, error.line);
dict.put(ERROR_LINESOURCE, error.lineSource);
dict.put(ERROR_LINEOFFSET, error.lineOffset);
dict.put(ERROR_STACK, KrollRuntime.getInstance().getStackTrace());
return dict;
}

public static String getError(KrollDict error)
{
String message = new String();
message += error.getString("sourceName") + ":" + error.getString("line") + "\n";
if (error.containsKeyAndNotNull("lineSource")) {
message += error.getString("lineSource") + "\n";
message += fill(Integer.parseInt(error.getString("lineOffset")) - 1) + "^\n";
String output = new String();

final String sourceName = error.getString(ERROR_SOURCENAME);
final int line = error.getInt(ERROR_LINE);
final String lineSource = error.getString(ERROR_LINESOURCE);
final int lineOffset = error.getInt(ERROR_LINEOFFSET);
final String stack = error.getString(ERROR_STACK);
final String message = error.getString(ERROR_MESSAGE);

if (sourceName != null) {
output += sourceName + ":" + line + "\n";
}
if (lineSource != null) {
output += lineSource + "\n";
output += fill(lineOffset - 1) + "^\n";
}
message += error.getString("message") + "\n";
if (error.containsKeyAndNotNull("stack")) {
message += error.getString("stack");
if (stack != null) {
output += stack + "\n";
}
return message;
output += message + "\n";

return output;
}

public TiExceptionHandler()
Expand Down Expand Up @@ -95,16 +126,8 @@ protected static void handleOpenErrorDialog(final ExceptionMessage error)
return;
}

final KrollDict dict = new KrollDict();
dict.put("title", error.title);
dict.put("message", error.message);
dict.put("sourceName", error.sourceName);
dict.put("line", error.line);
dict.put("lineSource", error.lineSource);
dict.put("lineOffset", error.lineOffset);
dict.put("stack", KrollRuntime.getInstance().getStackTrace());
final KrollDict dict = getErrorDict(error);
tiApp.fireAppEvent("uncaughtException", dict);

Log.e(TAG, getError(dict));

if (tiApp.getDeployType().equals(TiApplication.DEPLOY_TYPE_PRODUCTION)) {
Expand All @@ -127,7 +150,7 @@ public void onCurrentActivityReady(Activity activity)

protected static void createDialog(final KrollDict error)
{
final KrollApplication tiApp = KrollRuntime.getInstance().getKrollApplication();
final TiApplication tiApp = TiApplication.getInstance();
if (tiApp == null) {
return;
}
Expand Down

0 comments on commit 0877998

Please sign in to comment.