MCPcopy Create free account
hub / github.com/NativeScript/android / ReThrowToJava

Method ReThrowToJava

test-app/runtime/src/main/cpp/NativeScriptException.cpp:80–141  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78}
79
80void NativeScriptException::ReThrowToJava() {
81 jthrowable ex = nullptr;
82 JEnv env;
83
84 auto isolate = Isolate::GetCurrent();
85
86 if (!m_javaException.IsNull()) {
87 auto excClassName = ObjectManager::GetClassName((jobject)m_javaException);
88 if (excClassName == "com/tns/NativeScriptException") {
89 ex = m_javaException;
90 } else {
91 JniLocalRef msg(env.NewStringUTF("Java Error!"));
92 ex = static_cast<jthrowable>(env.NewObject(
93 NATIVESCRIPTEXCEPTION_CLASS, NATIVESCRIPTEXCEPTION_THROWABLE_CTOR_ID,
94 (jstring)msg, (jobject)m_javaException));
95 }
96 } else if (m_javascriptException != nullptr) {
97 /*
98 * If exception is an object, then check if it has 'nativeException'
99 * property and it if does then use it. If the found 'nativeException' is of
100 * type different than com.tns.NativeScript then we should wrap it and keep
101 * the JavaScript callstack as a message.
102 * Otherwise create we have to create new exception object.
103 */
104 auto isolate = Isolate::GetCurrent();
105 auto errObj = Local<Value>::New(isolate, *m_javascriptException);
106 if (errObj->IsObject()) {
107 auto exObj = TryGetJavaThrowableObject(env, errObj.As<Object>());
108 ex = (jthrowable)exObj.Move();
109 }
110
111 JniLocalRef msg(env.NewStringUTF(m_message.c_str()));
112 JniLocalRef stackTrace(env.NewStringUTF(m_stackTrace.c_str()));
113
114 if (ex == nullptr) {
115 ex = static_cast<jthrowable>(env.NewObject(
116 NATIVESCRIPTEXCEPTION_CLASS, NATIVESCRIPTEXCEPTION_JSVALUE_CTOR_ID,
117 (jstring)msg, (jstring)stackTrace,
118 reinterpret_cast<jlong>(m_javascriptException)));
119 } else {
120 auto excClassName = ObjectManager::GetClassName(ex);
121 if (excClassName != "com/tns/NativeScriptException") {
122 ex = static_cast<jthrowable>(
123 env.NewObject(NATIVESCRIPTEXCEPTION_CLASS,
124 NATIVESCRIPTEXCEPTION_THROWABLE_CTOR_ID, (jstring)msg,
125 (jstring)stackTrace, ex));
126 }
127 }
128 } else if (!m_message.empty()) {
129 JniLocalRef msg(env.NewStringUTF(m_message.c_str()));
130 JniLocalRef stackTrace(env.NewStringUTF(m_stackTrace.c_str()));
131 ex = static_cast<jthrowable>(env.NewObject(
132 NATIVESCRIPTEXCEPTION_CLASS, NATIVESCRIPTEXCEPTION_JSVALUE_CTOR_ID,
133 (jstring)msg, (jstring)stackTrace, (jlong)0));
134 } else {
135 JniLocalRef msg(env.NewStringUTF("No java exception or message provided."));
136 ex = static_cast<jthrowable>(env.NewObject(
137 NATIVESCRIPTEXCEPTION_CLASS, NATIVESCRIPTEXCEPTION_JSVALUE_CTOR_ID,

Calls 7

NewFunction · 0.85
NewStringUTFMethod · 0.80
NewObjectMethod · 0.80
MoveMethod · 0.80
IsNullMethod · 0.45
emptyMethod · 0.45
ThrowMethod · 0.45

Tested by

no test coverage detected