这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 922f358

Browse files
Fixed: Manually add stack trace of caller function that called ResultReturner.returnData() to exception messages as exceptions thrown inside Runnable lambda thread will not include it by default
For example, exceptions like following don't tell which API call failed and where. ``` Error in ResultReturner: java.io.IOException: Connection refused at android.net.LocalSocketImpl.connectLocal(Native Method) at android.net.LocalSocketImpl.connect(LocalSocketImpl.java:259) at android.net.LocalSocket.connect(LocalSocket.java:162) at com.termux.api.util.ResultReturner.lambda$returnData$0(ResultReturner.java:244) at com.termux.api.util.ResultReturner$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) at java.lang.Thread.run(Thread.java:1119) ``` Now it will be something like this ``` Error in ResultReturner: java.io.IOException: Connection refused at android.net.LocalSocketImpl.connectLocal(Native Method) at android.net.LocalSocketImpl.connect(LocalSocketImpl.java:259) at android.net.LocalSocket.connect(LocalSocket.java:162) at com.termux.api.util.ResultReturner.lambda$returnData$0(ResultReturner.java:244) at com.termux.api.util.ResultReturner$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) at java.lang.Thread.run(Thread.java:1119) Suppressed: java.lang.Exception: Called by: at com.termux.api.util.ResultReturner.returnData(ResultReturner.java:234) at com.termux.api.apis.BatteryStatusAPI.onReceive(BatteryStatusAPI.java:27) at com.termux.api.TermuxApiReceiver.doWork(TermuxApiReceiver.java:91) at com.termux.api.TermuxApiReceiver.onReceive(TermuxApiReceiver.java:65) at android.app.ActivityThread.handleReceiver(ActivityThread.java:5029) at android.app.ActivityThread.-$$Nest$mhandleReceiver(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2577) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loopOnce(Looper.java:248) at android.os.Looper.loop(Looper.java:338) at android.app.ActivityThread.main(ActivityThread.java:9067) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:932) ```
1 parent 97b4100 commit 922f358

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

app/src/main/java/com/termux/api/util/ResultReturner.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ public static LocalSocketAddress getApiLocalSocketAddress(@NonNull Context conte
223223
}
224224
}
225225

226+
public static boolean shouldRunThreadForResultRunnable(Object context) {
227+
return !(context instanceof IntentService);
228+
}
229+
226230
/**
227231
* Run in a separate thread, unless the context is an IntentService.
228232
*/
@@ -231,6 +235,10 @@ public static void returnData(Object context, final Intent intent, final ResultW
231235
final Activity activity = (Activity) ((context instanceof Activity) ? context : null);
232236
final PendingResult asyncResult = receiver != null ? receiver.goAsync() : null;
233237

238+
// Store caller function stack trace to add to exception messages thrown inside `Runnable`
239+
// lambda in case its run in a thread as it will not be included by default.
240+
final Throwable callerStackTrace = shouldRunThreadForResultRunnable(context) ? new Exception("Called by:") : null;
241+
234242
final Runnable runnable = () -> {
235243
PrintWriter writer = null;
236244
LocalSocket outputSocket = null;
@@ -276,6 +284,8 @@ public static void returnData(Object context, final Intent intent, final ResultW
276284
}
277285
} catch (Throwable t) {
278286
String message = "Error in " + LOG_TAG;
287+
if (callerStackTrace != null)
288+
t.addSuppressed(callerStackTrace);
279289
Logger.logStackTraceWithMessage(LOG_TAG, message, t);
280290

281291
TermuxPluginUtils.sendPluginCommandErrorNotification(ResultReturner.context, LOG_TAG,
@@ -308,11 +318,11 @@ public static void returnData(Object context, final Intent intent, final ResultW
308318
}
309319
};
310320

311-
if (context instanceof IntentService) {
312-
runnable.run();
313-
} else {
321+
if (shouldRunThreadForResultRunnable(context)) {
314322
new Thread(runnable).start();
315-
}
323+
} else {
324+
runnable.run();
325+
}
316326
}
317327

318328
public static void setContext(Context context) {

0 commit comments

Comments
 (0)