Skip to content

Commit b196cf3

Browse files
committed
Fix rethrowing errors to V8
1 parent 6d04702 commit b196cf3

File tree

4 files changed

+18
-29
lines changed

4 files changed

+18
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dist/
66
/test-app/libs
77
/test-app/metadata
88
/test-app/.idea
9+
.idea
910

1011
local.properties
1112

runtime/src/main/jni/Module.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "NativeScriptException.h"
1515
#include "Util.h"
1616
#include "SimpleProfiler.h"
17+
#include "include/v8.h"
1718

1819
#include <sstream>
1920
#include <assert.h>
@@ -200,12 +201,7 @@ void Module::Load(const string& path)
200201
auto globalObject = context->Global();
201202
auto require = globalObject->Get(context, ConvertToV8String("require")).ToLocalChecked().As<Function>();
202203
Local<Value> args[] = { ConvertToV8String(path) };
203-
TryCatch tc;
204204
require->Call(context, globalObject, 1, args);
205-
if (tc.HasCaught())
206-
{
207-
throw NativeScriptException(tc, "Fail to load module: " + path);
208-
}
209205
}
210206

211207
Local<Object> Module::LoadImpl(Isolate *isolate, const string& moduleName, const string& baseDir, bool& isData)

runtime/src/main/jni/NativeScriptException.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "V8GlobalHelpers.h"
44
#include "NativeScriptAssert.h"
55
#include "V8StringConstants.h"
6+
#include "include/v8.h"
67
#include <sstream>
78

89
using namespace std;
@@ -43,6 +44,9 @@ void NativeScriptException::ReThrowToV8()
4344
if (m_javascriptException != nullptr)
4445
{
4546
errObj = Local<Value>::New(isolate, *m_javascriptException);
47+
if(errObj->IsObject() && !m_message.empty()) {
48+
errObj.As<Object>()->Set(ConvertToV8String("fullMessage"), ConvertToV8String(m_message));
49+
}
4650
}
4751
else if (!m_message.empty())
4852
{
@@ -151,30 +155,19 @@ void NativeScriptException::Init(ObjectManager *objectManager)
151155
// ON V8 UNCAUGHT EXCEPTION
152156
void NativeScriptException::OnUncaughtError(Local<Message> message, Local<Value> error)
153157
{
154-
auto errorMessage = PrintErrorMessage(message, error);
155-
156-
Isolate *isolate = Isolate::GetCurrent();
157-
HandleScope scope(isolate);
158+
string errorMessage;
159+
auto v8FullMessage = ConvertToV8String("fullMessage");
158160

159-
Local<Object> errorObject;
160-
if (error->IsObject())
161-
{
162-
errorObject = error.As<Object>();
163-
errorObject->Set(ConvertToV8String("message"), ConvertToV8String(errorMessage));
164-
}
165-
else
166-
{
167-
errorObject = Exception::Error(ConvertToV8String(errorMessage)).As<Object>();
161+
if(error->IsObject() && error.As<Object>()->Has(v8FullMessage)) {
162+
errorMessage = ConvertToString(error.As<Object>()->Get(v8FullMessage).As<String>());
168163
}
169164

170-
CallJsFuncWithErr(errorObject);
165+
if(errorMessage.size() == 0) {
166+
errorMessage = GetErrorMessage(message, error);
167+
}
171168

172-
// check whether the developer marked the error as "Caught"
173-
// As per discussion, it is safer to ALWAYS kill the application due to uncaught error(s)
174-
// TODO: We may think for some per-thread specific behavior and allow execution to continue for background thread exceptions
175-
// if(!returnValue.IsEmpty() && (returnValue->IsBoolean() || returnValue->IsBooleanObject())){
176-
// handled = returnValue->BooleanValue();
177-
// }
169+
NativeScriptException e(errorMessage);
170+
e.ReThrowToJava();
178171
}
179172

180173
void NativeScriptException::CallJsFuncWithErr(Local<Value> errObj)

runtime/src/main/jni/Runtime.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "V8NativeScriptExtension.h"
2424
#include "Runtime.h"
2525
#include "ArrayHelper.h"
26+
#include "include/v8.h"
2627
#include <sstream>
2728
#include <android/log.h>
2829
#include <string>
@@ -248,9 +249,6 @@ void Runtime::CreateJSInstanceNative(JNIEnv *_env, jobject obj, jobject javaObje
248249

249250
JEnv env(_env);
250251

251-
// TODO: Do we need a TryCatch here? It is currently not used anywhere
252-
TryCatch tc;
253-
254252
string existingClassName = ArgConverter::jstringToString(className);
255253
string jniName = Util::ConvertFromCanonicalToJniName(existingClassName);
256254
Local<Object> jsInstance;
@@ -259,9 +257,10 @@ void Runtime::CreateJSInstanceNative(JNIEnv *_env, jobject obj, jobject javaObje
259257
auto proxyClassName = m_objectManager->GetClassName(javaObject);
260258
DEBUG_WRITE("createJSInstanceNative class %s", proxyClassName.c_str());
261259
jsInstance = MetadataNode::CreateExtendedJSWrapper(isolate, m_objectManager, proxyClassName);
260+
262261
if (jsInstance.IsEmpty())
263262
{
264-
throw NativeScriptException(string("NativeScript application not initialized correctly. Cannot create extended JS wrapper."));
263+
throw NativeScriptException(string("Failed to create JavaScript extend wrapper for class '" + proxyClassName + "'"));
265264
}
266265

267266
implementationObject = MetadataNode::GetImplementationObject(jsInstance);

0 commit comments

Comments
 (0)