Skip to content

Commit 345ee86

Browse files
committed
do not show ErrorActivity when debugger is active (debugger client is connected) but send the exception details to the debuger client
1 parent 09914a4 commit 345ee86

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

src/jni/JsDebugger.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void JsDebugger::DebugBreak()
8282

8383
bool JsDebugger::IsDebuggerActive()
8484
{
85-
return s_jsDebugger == nullptr && enabled;
85+
return s_jsDebugger != nullptr && enabled;
8686
}
8787

8888
void JsDebugger::ProcessDebugMessages()
@@ -210,6 +210,40 @@ void JsDebugger::ConsoleMessage(const v8::FunctionCallbackInfo<v8::Value>& args)
210210
}
211211
}
212212

213+
void JsDebugger::ConsoleMessage(const string& message, const string& level, const string& srcFileName, int lineNumber, int columnNumber)
214+
{
215+
if (s_jsDebugger == nullptr || !enabled)
216+
{
217+
return;
218+
}
219+
220+
try
221+
{
222+
JEnv env;
223+
JniLocalRef jniText(env.NewStringUTF(message.c_str()));
224+
JniLocalRef jniLevel(env.NewStringUTF(level.c_str()));
225+
JniLocalRef jniSrcFileName(env.NewStringUTF(srcFileName.c_str()));
226+
227+
env.CallVoidMethod(s_jsDebugger, s_EnqueueConsoleMessage, (jstring) jniText, (jstring) jniLevel, lineNumber, columnNumber, (jstring) jniSrcFileName);
228+
}
229+
catch (NativeScriptException& e)
230+
{
231+
e.ReThrowToV8();
232+
}
233+
catch (std::exception e)
234+
{
235+
stringstream ss;
236+
ss << "Error: c++ exception: " << e.what() << endl;
237+
NativeScriptException nsEx(ss.str());
238+
nsEx.ReThrowToV8();
239+
}
240+
catch (...)
241+
{
242+
NativeScriptException nsEx(std::string("Error: c++ exception!"));
243+
nsEx.ReThrowToV8();
244+
}
245+
}
246+
213247
void JsDebugger::SendCommandToV8(uint16_t *cmd, int length)
214248
{
215249
auto isolate = s_isolate;

src/jni/JsDebugger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ namespace tns
3131

3232
static void ConsoleMessageCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
3333

34+
static void ConsoleMessage(const std::string& message, const std::string& level = "log", const std::string& srcFileName = "",
35+
const int lineNumber = 0, const int columnNumber = 0);
36+
3437
private:
3538
JsDebugger();
3639

src/jni/NativePlatform.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,28 @@ void NativePlatform::PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, j
263263
}
264264
}
265265

266+
string stackTraceText = ArgConverter::jstringToString(stackTrace);
267+
errMsg += "\n" + stackTraceText;
268+
266269
//create a JS error object
267270
errObj->Set(V8StringConstants::GetNativeException(), nativeExceptionObject);
268271
errObj->Set(V8StringConstants::GetStackTrace(), ArgConverter::jstringToV8String(stackTrace));
269272

270-
//pass err to JS
271-
NativeScriptException::CallJsFuncWithErr(errObj);
273+
274+
if (JsDebugger::IsDebuggerActive())
275+
{
276+
__android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", "debuger active throwing exception with message %s", errMsg.c_str());
277+
278+
JsDebugger::ConsoleMessage(errMsg, "error");
279+
//throwing unhandled error to debugger so catch all and catch unhandled exceptions setting on client can be triggered
280+
DEBUG_WRITE_FORCE("throwing unhandled error to debugger");
281+
NativeScriptException(errMsg).ReThrowToV8();
282+
}
283+
else
284+
{
285+
//pass err to JS
286+
NativeScriptException::CallJsFuncWithErr(errObj);
287+
}
272288
}
273289

274290
void NativePlatform::AppInitCallback(const v8::FunctionCallbackInfo<v8::Value>& args)

src/src/com/tns/NativeScriptUncaughtExceptionHandler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,30 @@ public NativeScriptUncaughtExceptionHandler(Logger logger, Context context)
2222
@Override
2323
public void uncaughtException(Thread thread, Throwable ex)
2424
{
25+
String errorMessage = ErrorReport.getErrorMessage(ex);
26+
2527
if (Platform.isInitialized())
2628
{
2729
try
2830
{
2931
ex.printStackTrace();
30-
Platform.passUncaughtExceptionToJsNative(ex, ErrorReport.getErrorMessage(ex));
32+
Platform.passUncaughtExceptionToJsNative(ex, errorMessage);
33+
34+
if (JsDebugger.isJsDebugerActive())
35+
{
36+
return;
37+
}
3138
}
3239
catch (Throwable t)
3340
{
3441
t.printStackTrace();
3542
}
3643
}
3744

38-
String errorMessage = ErrorReport.getErrorMessage(ex);
39-
4045
if (logger.isEnabled())
46+
{
4147
logger.write("Uncaught Exception Message=" + errorMessage);
48+
}
4249

4350
if (!ErrorReport.startActivity(context, errorMessage) && defaultHandler != null)
4451
{

0 commit comments

Comments
 (0)