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 91d40c9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 33 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;
}

return javaProxy;
}

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,25 +46,37 @@ public class TiExceptionHandler implements Handler.Callback, KrollExceptionHandl
private static boolean dialogShowing = false;
private static Handler mainHandler;

private static final String fill(int count)
{
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("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());
return dict;
}

public static String getError(KrollDict error) {
String message = new String();
message += error.getString("sourceName") + ":" + error.getString("line") + "\n";
if (!error.getString("sourceName").isEmpty()) {
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";
}
message += error.getString("message") + "\n";
if (error.containsKeyAndNotNull("stack")) {
message += error.getString("stack");
message += error.getString("stack") + "\n";
}
message += error.getString("message") + "\n";
return message;
}

Expand Down Expand Up @@ -95,16 +106,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 @@ -115,8 +118,7 @@ protected static void handleOpenErrorDialog(final ExceptionMessage error)
dialogShowing = true;
tiApp.waitForCurrentActivity(new CurrentActivityListener() {
@Override
public void onCurrentActivityReady(Activity activity)
{
public void onCurrentActivityReady(Activity activity) {
createDialog(dict);
}
});
Expand All @@ -127,7 +129,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 All @@ -138,8 +140,7 @@ protected static void createDialog(final KrollDict error)
errorView.setBackgroundColor(0xFFF5F5F5);
errorView.setTextColor(0xFFE53935);
errorView.setPadding(5, 5, 5, 5);
errorView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
errorView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
errorView.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
errorView.setSingleLine(false);
errorView.setScroller(new Scroller(context));
Expand All @@ -152,14 +153,12 @@ protected static void createDialog(final KrollDict error)

final RelativeLayout layout = new RelativeLayout(context);
layout.setPadding(0, 50, 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
layout.addView(errorView);

final OnClickListener clickListener = new OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
Process.killProcess(Process.myPid());
}
Expand All @@ -172,19 +171,19 @@ public void onClick(DialogInterface dialog, int which)
};

final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle(error.getString("title"))
.setView(layout)
.setPositiveButton("Kill", clickListener)
.setNeutralButton("Continue", clickListener)
.setCancelable(false);
.setTitle(error.getString("title"))
.setView(layout)
.setPositiveButton("Kill", clickListener)
.setNeutralButton("Continue", clickListener)
.setCancelable(false);

final AlertDialog dialog = builder.create();
dialog.show();

Window window = ((Activity) context).getWindow();
Rect displayRect = new Rect();
window.getDecorView().getWindowVisibleDisplayFrame(displayRect);
dialog.getWindow().setLayout(displayRect.width(), (int) (displayRect.height() * 0.95));
dialog.getWindow().setLayout(displayRect.width(), (int)(displayRect.height() * 0.95));
}

public boolean handleMessage(Message msg)
Expand Down

0 comments on commit 91d40c9

Please sign in to comment.