Skip to content

Commit

Permalink
print stack trace only in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vtrifonov committed Jun 19, 2019
1 parent b1f87d0 commit 0539b62
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 32 deletions.
12 changes: 9 additions & 3 deletions test-app/app/src/debug/java/com/tns/ErrorReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public static void verifyStoragePermissions(Activity activity) {
checkSelfPermissionMethod = ActivityCompat.class.getMethod("checkSelfPermission", Context.class, String.class);
} catch (NoSuchMethodException e) {
// method wasn't found, so there is no need to handle permissions explicitly
e.printStackTrace();
if (Util.isDebuggableApp(activity)) {
e.printStackTrace();
}
return;
}

Expand All @@ -104,7 +106,9 @@ public static void verifyStoragePermissions(Activity activity) {
}
} catch (Exception e) {
Toast.makeText(activity, "Couldn't resolve permissions", Toast.LENGTH_LONG).show();
e.printStackTrace();
if (Util.isDebuggableApp(activity)) {
e.printStackTrace();
}
return;
}
}
Expand Down Expand Up @@ -410,7 +414,9 @@ public void onClick(View v) {
} catch (Exception e) {
String err = "Could not write logcat report to sdcard. Make sure you have allowed access to external storage!";
Toast.makeText(activity, err, Toast.LENGTH_LONG).show();
e.printStackTrace();
if (Util.isDebuggableApp(container.getContext())) {
e.printStackTrace();
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion test-app/app/src/debug/java/com/tns/ErrorReportActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis

resetCheckingForPermissions();
} catch (Exception e) {
e.printStackTrace();
if (Util.isDebuggableApp(this)) {
e.printStackTrace();
}
Toast.makeText(this, "Couldn't resolve permissions", Toast.LENGTH_LONG).show();
resetCheckingForPermissions();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ public void uncaughtException(Thread thread, Throwable ex) {

if (Runtime.isInitialized()) {
try {
ex.printStackTrace();
if (Util.isDebuggableApp(context)) {
ex.printStackTrace();
}

Runtime runtime = Runtime.getCurrentRuntime();

if (runtime != null) {
runtime.passUncaughtExceptionToJs(ex, errorMessage);
}
} catch (Throwable t) {
t.printStackTrace();
if (Util.isDebuggableApp(context)) {
t.printStackTrace();
}
}
}

Expand All @@ -55,7 +59,9 @@ public void uncaughtException(Thread thread, Throwable ex) {
res = (Boolean) startActivity.invoke(null, context, errorMessage);
} catch (Exception e) {
android.util.Log.v("Error", errorMessage);
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
};
android.util.Log.v("Application Error", "ErrorActivity default implementation not found. Reinstall android platform to fix.");
}
}
Expand Down
36 changes: 27 additions & 9 deletions test-app/app/src/main/java/com/tns/RuntimeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,18 @@ public static Runtime initRuntime(Context context) {
if (logger.isEnabled()) {
logger.write("Error while getting current proxy thumb");
}
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

String nativeLibDir = null;
try {
nativeLibDir = context.getPackageManager().getApplicationInfo(appName, 0).nativeLibraryDir;
} catch (NameNotFoundException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

boolean isDebuggable = Util.isDebuggableApp(context);
Expand Down Expand Up @@ -186,7 +190,9 @@ public static Runtime initRuntime(Context context) {

v8Inspector.waitForDebugger(shouldBreak);
} catch (IOException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

// if app is in debuggable mode run livesync service
Expand All @@ -213,7 +219,9 @@ public static Runtime initRuntime(Context context) {
if (logger.isEnabled()) {
logger.write("Cannot initialize application instance.");
}
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}

if (appConfig.handleTimeZoneChanges()) {
Expand Down Expand Up @@ -320,15 +328,25 @@ public static void initLiveSync(Runtime runtime, Logger logger, Context context)
Method startServerMethod = NativeScriptSyncService.getMethod("startServer");
startServerMethod.invoke(syncService);
} catch (ClassNotFoundException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (InvocationTargetException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
} catch (InstantiationException e) {
e.printStackTrace();
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions test-app/app/src/main/java/com/tns/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;

import androidx.core.content.pm.PackageInfoCompat;

Expand All @@ -30,7 +31,7 @@ public static boolean isDebuggableApp(Context context) {
flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags;
} catch (NameNotFoundException e) {
flags = 0;
e.printStackTrace();
Log.w("JS", String.format("Error getting application flags: %s", e.getMessage()));
}

boolean isDebuggableApp = ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
Expand All @@ -47,7 +48,7 @@ static boolean runPlugin(Logger logger, Context context) {
pluginClassName = metadataBundle.getString("com.tns.internal.Plugin");
}
} catch (Exception e) {
if (logger.isEnabled()) {
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}
Expand All @@ -57,7 +58,7 @@ static boolean runPlugin(Logger logger, Context context) {
Plugin p = (Plugin) liveSyncPluginClass.newInstance();
success = p.execute(context);
} catch (Exception e) {
if (logger.isEnabled()) {
if (Util.isDebuggableApp(context)) {
e.printStackTrace();
}
}
Expand Down
5 changes: 2 additions & 3 deletions test-app/runtime/src/main/java/com/tns/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public AppConfig(File appDir) {
MarkingMode markingMode = MarkingMode.valueOf(markingModeString);
values[KnownKeys.MarkingMode.ordinal()] = markingMode;
} catch (Exception e) {
e.printStackTrace();
Log.v("JS", "Failed to parse marking mode. The default " + ((MarkingMode)KnownKeys.MarkingMode.getDefaultValue()).name() + " will be used.");
Log.e("JS", String.format("Failed to parse marking mode: %s. The default %s will be used!", e.getMessage(), ((MarkingMode)KnownKeys.MarkingMode.getDefaultValue()).name()));
}
}
if (androidObject.has(KnownKeys.HandleTimeZoneChanges.getName())) {
Expand All @@ -123,7 +122,7 @@ public AppConfig(File appDir) {
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("JS", String.format("Error reading application configurations from %s: %s ", packageInfo.getPath(), e.getMessage()));
}
}

Expand Down
16 changes: 7 additions & 9 deletions test-app/runtime/src/main/java/com/tns/DexFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tns;

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
Expand Down Expand Up @@ -173,7 +175,7 @@ public Class<?> resolveClass(String baseClassName, String name, String className
result = df.loadClass(desiredDexClassName, classLoader);
}
} catch (IOException e) {
e.printStackTrace();
Log.w("JS", String.format("Error resolving class %s: %s. Fall back to DexClassLoader."));
// fall back to DexClassLoader
DexClassLoader dexClassLoader = new DexClassLoader(jarFilePath, this.odexDir.getAbsolutePath(), null, classLoader);
result = dexClassLoader.loadClass(fullClassName);
Expand Down Expand Up @@ -317,11 +319,9 @@ private void saveNewDexThumb(String newDexThumb, File dexDir) {
out.close();
}
} catch (FileNotFoundException e) {
logger.write("Error while writting current proxy thumb");
e.printStackTrace();
Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage()));
} catch (IOException e) {
logger.write("Error while writting current proxy thumb");
e.printStackTrace();
Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage()));
}
}

Expand Down Expand Up @@ -366,11 +366,9 @@ private String getCachedProxyThumb(File proxyDir) {
return cachedThumb;
}
} catch (FileNotFoundException e) {
logger.write("Error while getting current proxy thumb");
e.printStackTrace();
Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage()));
} catch (IOException e) {
logger.write("Error while getting current proxy thumb");
e.printStackTrace();
Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage()));
}

return null;
Expand Down
3 changes: 2 additions & 1 deletion test-app/runtime/src/main/java/com/tns/Module.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tns;

import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -55,7 +56,7 @@ private static String resolvePath(String path, String baseDir) {
file = resolvePathHelper(path, baseDir);
} catch (IOException e) {
//probably cache failed because of getCanonicalPath
e.printStackTrace();
Log.w("JS", String.format("Error resolving path:", e.getMessage()));
}

String resolvedPath;
Expand Down

0 comments on commit 0539b62

Please sign in to comment.