Skip to content

Commit

Permalink
show script eval errors in redbox
Browse files Browse the repository at this point in the history
Differential Revision: D2624801

fb-gh-sync-id: 48741a8caf029415753a4c616a07f18d3660e6fb
  • Loading branch information
foghina authored and facebook-github-bot-4 committed Nov 6, 2015
1 parent e637271 commit 9647c5a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ public void run() {

incrementPendingJSCalls();

mJSBundleLoader.loadScript(mBridge);
try {
mJSBundleLoader.loadScript(mBridge);
} catch (JSExecutionException e) {
mNativeModuleCallExceptionHandler.handleException(e);
}

initLatch.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2004-present Facebook. All Rights Reserved.

package com.facebook.react.bridge;

import com.facebook.proguard.annotations.DoNotStrip;

/**
* Exception thrown when there is an error evaluating JS, e.g. a syntax error.
*/
@DoNotStrip
public class JSExecutionException extends RuntimeException {

@DoNotStrip
public JSExecutionException(String detailMessage) {
super(detailMessage);
}
}
14 changes: 14 additions & 0 deletions ReactAndroid/src/main/jni/react/JSCExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <fb/log.h>
#include <folly/json.h>
#include <folly/String.h>
#include <jni/fbjni/Exceptions.h>
#include "Value.h"

#ifdef WITH_JSC_EXTRA_TRACING
Expand All @@ -24,6 +25,8 @@ using fbsystrace::FbSystraceSection;
// Add native performance markers support
#include <react/JSCPerfLogging.h>

using namespace facebook::jni;

namespace facebook {
namespace react {

Expand Down Expand Up @@ -53,6 +56,17 @@ static JSValueRef evaluateScriptWithJSC(
JSValueProtect(ctx, exn);
std::string exceptionText = Value(ctx, exn).toString().str();
FBLOGE("Got JS Exception: %s", exceptionText.c_str());
auto line = Value(ctx, JSObjectGetProperty(ctx,
JSValueToObject(ctx, exn, nullptr),
JSStringCreateWithUTF8CString("line"), nullptr
));
std::ostringstream lineInfo;
if (line != nullptr && line.isNumber()) {
lineInfo << " (line " << line.asInteger() << " in the generated bundle)";
} else {
lineInfo << " (no line info)";
}
throwNewJavaException("com/facebook/react/bridge/JSExecutionException", (exceptionText + lineInfo.str()).c_str());
}
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/OnLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ static void loadScriptFromAssets(JNIEnv* env, jobject obj, jobject assetManager,

env->CallStaticVoidMethod(markerClass, gLogMarkerMethod, env->NewStringUTF("loadScriptFromAssets_read"));
executeApplicationScript(bridge, script, assetNameStr);
if (env->ExceptionCheck()) {
return;
}
env->CallStaticVoidMethod(markerClass, gLogMarkerMethod, env->NewStringUTF("loadScriptFromAssets_done"));
}

Expand All @@ -666,6 +669,9 @@ static void loadScriptFromFile(JNIEnv* env, jobject obj, jstring fileName, jstri
#endif
env->CallStaticVoidMethod(markerClass, gLogMarkerMethod, env->NewStringUTF("loadScriptFromFile_read"));
executeApplicationScript(bridge, script, jni::fromJString(env, sourceURL));
if (env->ExceptionCheck()) {
return;
}
env->CallStaticVoidMethod(markerClass, gLogMarkerMethod, env->NewStringUTF("loadScriptFromFile_exec"));
}

Expand Down

0 comments on commit 9647c5a

Please sign in to comment.