Skip to content

Commit dd4482b

Browse files
authored
Merge branch 'master' into vmutafov/sbg-interfaces-handling-fix
2 parents b70d8e9 + 6aa1b74 commit dd4482b

14 files changed

Lines changed: 114 additions & 42 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright (c) 2015-2018 Progress Software Corporation
189+
Copyright (c) 2015-2019 Progress Software Corporation
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

test-app/app/src/main/assets/app/mainpage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require("./tests/testPrimitiveTypeConversion");
3434
require("./tests/numericConversionTests");
3535
require("./tests/inheritanceChainResolutionTest");
3636
require("./tests/exceptionHandlingTests");
37+
require("./tests/discardedExceptionsTest");
3738
require("./tests/dispatchCallbacksOnUiThreadTests");
3839
require("./tests/stringConversionTests");
3940
require("./tests/testsForTypescript");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
describe("Tests discarded exception ", function () {
2+
var originalOnDiscardedError = null;
3+
4+
beforeEach(function() {
5+
originalOnDiscardedError = global.__onDiscardedError;
6+
});
7+
8+
it("should report discarded exception", function () {
9+
var reportedException = null;
10+
11+
global.__onDiscardedError = (error) => {
12+
reportedException = error;
13+
}
14+
15+
var test = new com.tns.tests.DiscardedExceptionTest();
16+
test.reportSupressedException();
17+
18+
expect(reportedException).not.toBe(null);
19+
expect(reportedException.nativeException).not.toBe(null);
20+
expect(reportedException.nativeException.getMessage()).toBe('Exception to suppress');
21+
expect(reportedException.stackTrace).toContain('Error on "main" thread for reportSupressedException');
22+
});
23+
24+
afterEach(function() {
25+
global.__onDiscardedError = originalOnDiscardedError;
26+
});
27+
});

test-app/app/src/main/java/com/tns/NativeScriptUncaughtExceptionHandler.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public NativeScriptUncaughtExceptionHandler(Logger logger, Context context) {
2121
public void uncaughtException(Thread thread, Throwable ex) {
2222
String currentThreadMessage = "An uncaught Exception occurred on \"" + thread.getName() + "\" thread.\n";
2323

24-
String errorMessage = currentThreadMessage + getErrorMessage(ex);
24+
String errorMessage = currentThreadMessage + Runtime.getStackTraceErrorMessage(ex);
2525

2626
if (Runtime.isInitialized()) {
2727
try {
@@ -64,27 +64,4 @@ public void uncaughtException(Thread thread, Throwable ex) {
6464
defaultHandler.uncaughtException(thread, ex);
6565
}
6666
}
67-
68-
private static String getErrorMessage(Throwable ex) {
69-
String content;
70-
java.io.PrintStream ps = null;
71-
72-
try {
73-
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
74-
ps = new java.io.PrintStream(baos);
75-
ex.printStackTrace(ps);
76-
77-
try {
78-
content = baos.toString("US-ASCII");
79-
} catch (java.io.UnsupportedEncodingException e) {
80-
content = e.getMessage();
81-
}
82-
} finally {
83-
if (ps != null) {
84-
ps.close();
85-
}
86-
}
87-
88-
return content;
89-
}
9067
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.tns.tests;
2+
3+
public class DiscardedExceptionTest {
4+
public void reportSupressedException() {
5+
try {
6+
throw new Exception("Exception to suppress");
7+
} catch (Throwable ex) {
8+
com.tns.Runtime.passSuppressedExceptionToJs(ex, "reportSupressedException");
9+
}
10+
}
11+
}

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,8 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
660660

661661
if (this.suppressCallJSMethodExceptions) {
662662
w.writeln("\t\t} catch (Throwable t) {");
663-
w.writeln("\t\t\tandroid.util.Log.w(\"Error\", t);");
663+
w.writeln("\t\t\tcom.tns.Runtime.passSuppressedExceptionToJs(t, \"" + m.getName() + "\");");
664+
w.writeln("\t\t\tandroid.util.Log.w(\"Warning\", \"NativeScript discarding uncaught JS exception!\");");
664665
if (!ret.equals(Type.VOID)) {
665666
w.write("\t\t\t");
666667
w.write("return ");

test-app/runtime/src/main/cpp/NativeScriptException.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ void NativeScriptException::OnUncaughtError(Local<Message> message, Local<Value>
133133
e.ReThrowToJava();
134134
}
135135

136-
void NativeScriptException::CallJsFuncWithErr(Local<Value> errObj) {
136+
void NativeScriptException::CallJsFuncWithErr(Local<Value> errObj, jboolean isDiscarded) {
137137
auto isolate = Isolate::GetCurrent();
138138
HandleScope scope(isolate);
139139

140140
auto context = isolate->GetCurrentContext();
141141
auto globalHandle = context->Global();
142142

143-
auto handler = globalHandle->Get(V8StringConstants::GetUncaughtError(isolate));
143+
auto handler = isDiscarded ?
144+
globalHandle->Get(V8StringConstants::GetDiscardedError(isolate)) : globalHandle->Get(V8StringConstants::GetUncaughtError(isolate));
144145
auto isEmpty = handler.IsEmpty();
145146
auto isFunction = handler->IsFunction();
146147

test-app/runtime/src/main/cpp/NativeScriptException.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class NativeScriptException {
3636
static void OnUncaughtError(v8::Local<v8::Message> message, v8::Local<v8::Value> error);
3737

3838
/*
39-
* Calls the global "__onUncaughtError" if such is provided
39+
* Calls the global "__onUncaughtError" or "__onDiscardedError" if such is provided
4040
*/
41-
static void CallJsFuncWithErr(v8::Local<v8::Value> errObj);
41+
static void CallJsFuncWithErr(v8::Local<v8::Value> errObj, jboolean isDiscarded);
4242

4343
private:
4444
/*

test-app/runtime/src/main/cpp/Runtime.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,13 @@ bool Runtime::TryCallGC() {
371371
return success;
372372
}
373373

374-
void Runtime::PassUncaughtExceptionToJsNative(JNIEnv* env, jobject obj, jthrowable exception, jstring stackTrace) {
374+
void Runtime::PassExceptionToJsNative(JNIEnv* env, jobject obj, jthrowable exception, jstring stackTrace, jboolean isDiscarded) {
375375
auto isolate = m_isolate;
376376

377377
//create error message
378-
string errMsg = "The application crashed because of an uncaught exception. You can look at \"stackTrace\" or \"nativeException\" for more detailed information about the exception.";
378+
string errMsg = isDiscarded ? "An exception was caught and discarded. You can look at \"stackTrace\" or \"nativeException\" for more detailed information about the exception.":
379+
"The application crashed because of an uncaught exception. You can look at \"stackTrace\" or \"nativeException\" for more detailed information about the exception.";
380+
379381
auto errObj = Exception::Error(ArgConverter::ConvertToV8String(isolate, errMsg)).As<Object>();
380382

381383
//create a new native exception js object
@@ -399,7 +401,7 @@ void Runtime::PassUncaughtExceptionToJsNative(JNIEnv* env, jobject obj, jthrowab
399401
errObj->Set(V8StringConstants::GetStackTrace(isolate), ArgConverter::jstringToV8String(isolate, stackTrace));
400402

401403
//pass err to JS
402-
NativeScriptException::CallJsFuncWithErr(errObj);
404+
NativeScriptException::CallJsFuncWithErr(errObj, isDiscarded);
403405
}
404406

405407
void Runtime::PassUncaughtExceptionFromWorkerToMainHandler(Local<v8::String> message, Local<v8::String> stackTrace, Local<v8::String> filename, int lineno) {

test-app/runtime/src/main/cpp/Runtime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Runtime {
5252
void AdjustAmountOfExternalAllocatedMemory();
5353
bool NotifyGC(JNIEnv* env, jobject obj);
5454
bool TryCallGC();
55-
void PassUncaughtExceptionToJsNative(JNIEnv* env, jobject obj, jthrowable exception, jstring stackTrace);
55+
void PassExceptionToJsNative(JNIEnv* env, jobject obj, jthrowable exception, jstring stackTrace, jboolean isDiscarded);
5656
void PassUncaughtExceptionFromWorkerToMainHandler(v8::Local<v8::String> message, v8::Local<v8::String> stackTrace, v8::Local<v8::String> filename, int lineno);
5757
void ClearStartupData(JNIEnv* env, jobject obj);
5858
void DestroyRuntime();

0 commit comments

Comments
 (0)