From 5f2ccca4239c8d32436a35798e9cd5a0e0f57568 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Sun, 13 Jun 2021 00:44:56 +0500 Subject: [PATCH] Redo fix execution commands exceptions not being logged or sent back to plugins The f62febbf commit mentioned that it solved "the bug where Termux:Tasker would hang indefinitely if Runtime.getRuntime().exec raised an exception, like for invalid or missing interpreter errors and Termux:Tasker wasn't notified of it. Now the errmsg will be used to send any exceptions back to Termux:Tasker and other 3rd party calls." This however was still broken due to local design changes made to TermuxTask after testing was already done. This commit should solve that problem. Moreover, now a notification will be shown if execution commands **fail to start** that are run by plugins that don't expect the result back, like with Termux:Widget or RUN_COMMAND intent. This should make it easier for users to debug problems, since otherwise logcat needs to be looked. But logcat would still need to be looked if commands/scripts fail after they have started due to internal errors. Notifications can be disabled from Termux Settings by disabling the "Plugin Error Notifications" toggle. --- app/src/main/java/com/termux/app/TermuxService.java | 10 ++++++++++ .../main/java/com/termux/shared/shell/TermuxTask.java | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/termux/app/TermuxService.java b/app/src/main/java/com/termux/app/TermuxService.java index 71a4eb81..66d56eda 100644 --- a/app/src/main/java/com/termux/app/TermuxService.java +++ b/app/src/main/java/com/termux/app/TermuxService.java @@ -419,6 +419,11 @@ public final class TermuxService extends Service implements TermuxTask.TermuxTas TermuxTask newTermuxTask = TermuxTask.execute(this, executionCommand, this, false); if (newTermuxTask == null) { Logger.logError(LOG_TAG, "Failed to execute new TermuxTask command for:\n" + executionCommand.getCommandIdAndLabelLogString()); + // If the execution command was started for a plugin, then process the error + if (executionCommand.isPluginExecutionCommand) + PluginUtils.processPluginExecutionCommandError(this, LOG_TAG, executionCommand, false); + else + Logger.logStackTracesWithMessage(LOG_TAG, "(" + executionCommand.errCode + ") " + executionCommand.errmsg, executionCommand.throwableList); return null; } @@ -510,6 +515,11 @@ public final class TermuxService extends Service implements TermuxTask.TermuxTas TermuxSession newTermuxSession = TermuxSession.execute(this, executionCommand, getTermuxTerminalSessionClient(), this, sessionName, executionCommand.isPluginExecutionCommand); if (newTermuxSession == null) { Logger.logError(LOG_TAG, "Failed to execute new TermuxSession command for:\n" + executionCommand.getCommandIdAndLabelLogString()); + // If the execution command was started for a plugin, then process the error + if (executionCommand.isPluginExecutionCommand) + PluginUtils.processPluginExecutionCommandError(this, LOG_TAG, executionCommand, false); + else + Logger.logStackTracesWithMessage(LOG_TAG, "(" + executionCommand.errCode + ") " + executionCommand.errmsg, executionCommand.throwableList); return null; } diff --git a/termux-shared/src/main/java/com/termux/shared/shell/TermuxTask.java b/termux-shared/src/main/java/com/termux/shared/shell/TermuxTask.java index 1234284d..4507b30f 100644 --- a/termux-shared/src/main/java/com/termux/shared/shell/TermuxTask.java +++ b/termux-shared/src/main/java/com/termux/shared/shell/TermuxTask.java @@ -70,8 +70,11 @@ public final class TermuxTask { final String[] commandArray = ShellUtils.setupProcessArgs(executionCommand.executable, executionCommand.arguments); - if (!executionCommand.setState(ExecutionState.EXECUTING)) + if (!executionCommand.setState(ExecutionState.EXECUTING)) { + executionCommand.setStateFailed(ExecutionCommand.RESULT_CODE_FAILED, context.getString(R.string.error_failed_to_execute_termux_task_command, executionCommand.getCommandIdAndLabelLogString()), null); + TermuxTask.processTermuxTaskResult(null, executionCommand); return null; + } Logger.logDebug(LOG_TAG, executionCommand.toString());