Skip to content

Commit 4494bc6

Browse files
Implement Errno system
This commit adds onto 679e0de If an exception is thrown, the exception message might not contain the full errors. Individual failures may get added to suppressed throwables. FileUtils functions previously just returned the exception message as errmsg which did not contain full error info. Now `Error` class has been implemented which will used to return errors, including suppressed throwables. Each `Error` object will have an error type, code, message and a list of throwables in case multiple throwables need to returned, in addition to the suppressed throwables list in each throwable. A supportive `Errno` base class has been implemented as well which other errno classes can inherit of which some have been added. Each `Errno` object will have an error type, code and message and can be converted to an `Error` object if needed. Requirement for `Context` object has been removed from FileUtils so that they can be called from anywhere in code instead of having to pass around `Context` objects. Previously, `string.xml` was used to store error messages in case multi language support had to be added in future since error messages are displayed to users and not just for dev usage. However, now this will have to handled in java code if needed, based on locale. The termux related file utils have also been moved from FileUtils to TermuxFileUtils
1 parent 679e0de commit 4494bc6

File tree

13 files changed

+904
-433
lines changed

13 files changed

+904
-433
lines changed

app/src/main/java/com/termux/app/TermuxInstaller.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.termux.shared.file.FileUtils;
1515
import com.termux.shared.interact.DialogUtils;
1616
import com.termux.shared.logger.Logger;
17+
import com.termux.shared.models.errors.Error;
1718
import com.termux.shared.termux.TermuxConstants;
1819

1920
import java.io.BufferedReader;
@@ -88,21 +89,21 @@ public void run() {
8889
try {
8990
Logger.logInfo(LOG_TAG, "Installing " + TermuxConstants.TERMUX_APP_NAME + " bootstrap packages.");
9091

91-
String errmsg;
92+
Error error;
9293

9394
final String STAGING_PREFIX_PATH = TermuxConstants.TERMUX_STAGING_PREFIX_DIR_PATH;
9495
final File STAGING_PREFIX_FILE = new File(STAGING_PREFIX_PATH);
9596

9697
// Delete prefix staging directory or any file at its destination
97-
errmsg = FileUtils.deleteFile(activity, "prefix staging directory", STAGING_PREFIX_PATH, true);
98-
if (errmsg != null) {
99-
throw new RuntimeException(errmsg);
98+
error = FileUtils.deleteFile("prefix staging directory", STAGING_PREFIX_PATH, true);
99+
if (error != null) {
100+
throw new RuntimeException(error.toString());
100101
}
101102

102103
// Delete prefix directory or any file at its destination
103-
errmsg = FileUtils.deleteFile(activity, "prefix directory", PREFIX_FILE_PATH, true);
104-
if (errmsg != null) {
105-
throw new RuntimeException(errmsg);
104+
error = FileUtils.deleteFile("prefix directory", PREFIX_FILE_PATH, true);
105+
if (error != null) {
106+
throw new RuntimeException(error.toString());
106107
}
107108

108109
Logger.logInfo(LOG_TAG, "Extracting bootstrap zip to prefix staging directory \"" + STAGING_PREFIX_PATH + "\".");
@@ -125,14 +126,14 @@ public void run() {
125126
String newPath = STAGING_PREFIX_PATH + "/" + parts[1];
126127
symlinks.add(Pair.create(oldPath, newPath));
127128

128-
ensureDirectoryExists(activity, new File(newPath).getParentFile());
129+
ensureDirectoryExists(new File(newPath).getParentFile());
129130
}
130131
} else {
131132
String zipEntryName = zipEntry.getName();
132133
File targetFile = new File(STAGING_PREFIX_PATH, zipEntryName);
133134
boolean isDirectory = zipEntry.isDirectory();
134135

135-
ensureDirectoryExists(activity, isDirectory ? targetFile : targetFile.getParentFile());
136+
ensureDirectoryExists(isDirectory ? targetFile : targetFile.getParentFile());
136137

137138
if (!isDirectory) {
138139
try (FileOutputStream outStream = new FileOutputStream(targetFile)) {
@@ -173,7 +174,7 @@ public void run() {
173174
activity.finish();
174175
}).setPositiveButton(R.string.bootstrap_error_try_again, (dialog, which) -> {
175176
dialog.dismiss();
176-
FileUtils.deleteFile(activity, "prefix directory", PREFIX_FILE_PATH, true);
177+
FileUtils.deleteFile("prefix directory", PREFIX_FILE_PATH, true);
177178
TermuxInstaller.setupBootstrapIfNeeded(activity, whenDone);
178179
}).show();
179180
} catch (WindowManager.BadTokenException e1) {
@@ -201,12 +202,13 @@ static void setupStorageSymlinks(final Context context) {
201202
new Thread() {
202203
public void run() {
203204
try {
204-
String errmsg;
205+
Error error;
205206
File storageDir = TermuxConstants.TERMUX_STORAGE_HOME_DIR;
206207

207-
errmsg = FileUtils.clearDirectory(context, "~/storage", storageDir.getAbsolutePath());
208-
if (errmsg != null) {
209-
Logger.logErrorAndShowToast(context, LOG_TAG, errmsg);
208+
error = FileUtils.clearDirectory("~/storage", storageDir.getAbsolutePath());
209+
if (error != null) {
210+
Logger.logErrorAndShowToast(context, LOG_TAG, error.getMessage());
211+
Logger.logErrorExtended(LOG_TAG, error.toString());
210212
return;
211213
}
212214

@@ -249,12 +251,12 @@ public void run() {
249251
}.start();
250252
}
251253

252-
private static void ensureDirectoryExists(Context context, File directory) {
253-
String errmsg;
254+
private static void ensureDirectoryExists(File directory) {
255+
Error error;
254256

255-
errmsg = FileUtils.createDirectoryFile(context, directory.getAbsolutePath());
256-
if (errmsg != null) {
257-
throw new RuntimeException(errmsg);
257+
error = FileUtils.createDirectoryFile(directory.getAbsolutePath());
258+
if (error != null) {
259+
throw new RuntimeException(error.toString());
258260
}
259261
}
260262

app/src/main/java/com/termux/app/utils/CrashUtils.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.termux.R;
1212
import com.termux.shared.activities.ReportActivity;
13+
import com.termux.shared.models.errors.Error;
1314
import com.termux.shared.notification.NotificationUtils;
1415
import com.termux.shared.file.FileUtils;
1516
import com.termux.shared.models.ReportInfo;
@@ -62,25 +63,25 @@ public void run() {
6263
if (!FileUtils.regularFileExists(TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, false))
6364
return;
6465

65-
String errmsg;
66+
Error error;
6667
StringBuilder reportStringBuilder = new StringBuilder();
6768

6869
// Read report string from crash log file
69-
errmsg = FileUtils.readStringFromFile(context, "crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportStringBuilder, false);
70-
if (errmsg != null) {
71-
Logger.logError(logTag, errmsg);
70+
error = FileUtils.readStringFromFile("crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportStringBuilder, false);
71+
if (error != null) {
72+
Logger.logErrorExtended(logTag, error.toString());
7273
return;
7374
}
7475

7576
// Move crash log file to backup location if it exists
76-
FileUtils.moveRegularFile(context, "crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, TermuxConstants.TERMUX_CRASH_LOG_BACKUP_FILE_PATH, true);
77-
if (errmsg != null) {
78-
Logger.logError(logTag, errmsg);
77+
error = FileUtils.moveRegularFile("crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, TermuxConstants.TERMUX_CRASH_LOG_BACKUP_FILE_PATH, true);
78+
if (error != null) {
79+
Logger.logErrorExtended(logTag, error.toString());
7980
}
8081

8182
String reportString = reportStringBuilder.toString();
8283

83-
if (reportString == null || reportString.isEmpty())
84+
if (reportString.isEmpty())
8485
return;
8586

8687
// Send a notification to show the crash log which when clicked will open the {@link ReportActivity}

termux-shared/src/main/java/com/termux/shared/crash/CrashHandler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.termux.shared.file.FileUtils;
88
import com.termux.shared.logger.Logger;
99
import com.termux.shared.markdown.MarkdownUtils;
10+
import com.termux.shared.models.errors.Error;
1011
import com.termux.shared.termux.TermuxConstants;
1112
import com.termux.shared.termux.TermuxUtils;
1213

@@ -57,17 +58,17 @@ public static void logCrash(final Context context, final Thread thread, final Th
5758
reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Thread", thread.toString(), "-"));
5859
reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Timestamp", TermuxUtils.getCurrentTimeStamp(), "-"));
5960

60-
reportString.append("\n\n").append(Logger.getStackTracesMarkdownString("Stacktrace", Logger.getStackTraceStringArray(throwable)));
61+
reportString.append("\n\n").append(Logger.getStackTracesMarkdownString("Stacktrace", Logger.getStackTracesStringArray(throwable)));
6162
reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(context, true));
6263
reportString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(context));
6364

6465
// Log report string to logcat
6566
Logger.logError(reportString.toString());
6667

6768
// Write report string to crash log file
68-
String errmsg = FileUtils.writeStringToFile(context, "crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportString.toString(), false);
69-
if (errmsg != null) {
70-
Logger.logError(LOG_TAG, errmsg);
69+
Error error = FileUtils.writeStringToFile("crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportString.toString(), false);
70+
if (error != null) {
71+
Logger.logErrorExtended(LOG_TAG, error.toString());
7172
}
7273
}
7374

0 commit comments

Comments
 (0)