mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-08 11:34:07 +08:00
Changed: Make sure full path is included in FileUtilsErrnos
Previously, `FileUtilsErrno` had some errors that didn't include the full path passed to the `FileUtils` functions and caller had to manually append the path to the error. This was done due to `termux-tasker` plugin config activity was using these errors in the executable and working directory text fields and we had to keep the error short as possible to reduce clutter. Now by default, the path will be included so that its not missing for other cases and the `FileUtils.getShortFileUtilsError()` function is provided to get a shorter version from the original error if its possible to do so if caller like `termux-tasker` requires it.
This commit is contained in:
@@ -8,11 +8,14 @@ import com.termux.shared.logger.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/** The {@link Class} that defines error messages and codes. */
|
||||
public class Errno {
|
||||
|
||||
private static final HashMap<String, Errno> map = new HashMap<>();
|
||||
|
||||
public static final String TYPE = "Error";
|
||||
|
||||
|
||||
@@ -35,6 +38,7 @@ public class Errno {
|
||||
this.type = type;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
map.put(type + ":" + code, this);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -56,6 +60,17 @@ public class Errno {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link Errno} of a specific type and code.
|
||||
*
|
||||
* @param type The unique type of the {@link Errno}.
|
||||
* @param code The unique code of the {@link Errno}.
|
||||
*/
|
||||
public static Errno valueOf(String type, Integer code) {
|
||||
if (type == null || type.isEmpty() || code == null) return null;
|
||||
return map.get(type + ":" + code);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Error getError() {
|
||||
@@ -73,12 +88,18 @@ public class Errno {
|
||||
}
|
||||
|
||||
public Error getError(Throwable throwable, Object... args) {
|
||||
return getError(Collections.singletonList(throwable), args);
|
||||
if (throwable == null)
|
||||
return getError(args);
|
||||
else
|
||||
return getError(Collections.singletonList(throwable), args);
|
||||
}
|
||||
|
||||
public Error getError(List<Throwable> throwablesList, Object... args) {
|
||||
try {
|
||||
return new Error(getType(), getCode(), String.format(getMessage(), args), throwablesList);
|
||||
if (throwablesList == null)
|
||||
return new Error(getType(), getCode(), String.format(getMessage(), args));
|
||||
else
|
||||
return new Error(getType(), getCode(), String.format(getMessage(), args), throwablesList);
|
||||
} catch (Exception e) {
|
||||
Logger.logWarn(LOG_TAG, "Exception raised while calling String.format() for error message of errno " + this + " with args" + Arrays.toString(args) + "\n" + e.getMessage());
|
||||
// Return unformatted message as a backup
|
||||
|
@@ -12,6 +12,8 @@ import java.util.List;
|
||||
|
||||
public class Error implements Serializable {
|
||||
|
||||
/** The optional error label. */
|
||||
private String label;
|
||||
/** The error type. */
|
||||
private String type;
|
||||
/** The error code. */
|
||||
@@ -76,7 +78,18 @@ public class Error implements Serializable {
|
||||
this.code = Errno.ERRNO_SUCCESS.getCode();
|
||||
|
||||
this.message = message;
|
||||
this.throwablesList = throwablesList;
|
||||
|
||||
if (throwablesList != null)
|
||||
this.throwablesList = throwablesList;
|
||||
}
|
||||
|
||||
public Error setLabel(String label) {
|
||||
this.label = label;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package com.termux.shared.models.errors;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** The {@link Class} that defines FileUtils error messages and codes. */
|
||||
public class FileUtilsErrno extends Errno {
|
||||
|
||||
@@ -18,19 +21,20 @@ public class FileUtilsErrno extends Errno {
|
||||
|
||||
|
||||
/* Errors for invalid or not found files at path (150-200) */
|
||||
public static final Errno ERRNO_FILE_NOT_FOUND_AT_PATH = new Errno(TYPE, 150, "The %1$s is not found at path \"%2$s\".");
|
||||
public static final Errno ERRNO_FILE_NOT_FOUND_AT_PATH = new Errno(TYPE, 150, "The %1$s not found at path \"%2$s\".");
|
||||
public static final Errno ERRNO_FILE_NOT_FOUND_AT_PATH_SHORT = new Errno(TYPE, 151, "The %1$s not found at path.");
|
||||
|
||||
public static final Errno ERRNO_NO_REGULAR_FILE_FOUND = new Errno(TYPE, 151, "Regular file not found at %1$s path.");
|
||||
public static final Errno ERRNO_NOT_A_REGULAR_FILE = new Errno(TYPE, 152, "The %1$s at path \"%2$s\" is not a regular file.");
|
||||
public static final Errno ERRNO_NON_REGULAR_FILE_FOUND = new Errno(TYPE, 152, "Non-regular file found at %1$s path \"%2$s\".");
|
||||
public static final Errno ERRNO_NON_REGULAR_FILE_FOUND_SHORT = new Errno(TYPE, 153, "Non-regular file found at %1$s path.");
|
||||
public static final Errno ERRNO_NON_DIRECTORY_FILE_FOUND = new Errno(TYPE, 154, "Non-directory file found at %1$s path \"%2$s\".");
|
||||
public static final Errno ERRNO_NON_DIRECTORY_FILE_FOUND_SHORT = new Errno(TYPE, 155, "Non-directory file found at %1$s path.");
|
||||
public static final Errno ERRNO_NON_SYMLINK_FILE_FOUND = new Errno(TYPE, 156, "Non-symlink file found at %1$s path \"%2$s\".");
|
||||
public static final Errno ERRNO_NON_SYMLINK_FILE_FOUND_SHORT = new Errno(TYPE, 157, "Non-symlink file found at %1$s path.");
|
||||
|
||||
public static final Errno ERRNO_NON_REGULAR_FILE_FOUND = new Errno(TYPE, 153, "Non-regular file found at %1$s path.");
|
||||
public static final Errno ERRNO_NON_DIRECTORY_FILE_FOUND = new Errno(TYPE, 154, "Non-directory file found at %1$s path.");
|
||||
public static final Errno ERRNO_NON_SYMLINK_FILE_FOUND = new Errno(TYPE, 155, "Non-symlink file found at %1$s path.");
|
||||
public static final Errno ERRNO_FILE_NOT_AN_ALLOWED_FILE_TYPE = new Errno(TYPE, 158, "The %1$s found at path \"%2$s\" is not one of allowed file types \"%3$s\".");
|
||||
|
||||
public static final Errno ERRNO_FILE_NOT_AN_ALLOWED_FILE_TYPE = new Errno(TYPE, 156, "The %1$s found at path \"%2$s\" is not one of allowed file types \"%3$s\".");
|
||||
|
||||
public static final Errno ERRNO_VALIDATE_FILE_EXISTENCE_AND_PERMISSIONS_FAILED_WITH_EXCEPTION = new Errno(TYPE, 157, "Validating file existence and permissions of %1$s at path \"%2$s\" failed.\nException: %3$s");
|
||||
public static final Errno ERRNO_VALIDATE_DIRECTORY_EXISTENCE_AND_PERMISSIONS_FAILED_WITH_EXCEPTION = new Errno(TYPE, 158, "Validating directory existence and permissions of %1$s at path \"%2$s\" failed.\nException: %3$s");
|
||||
public static final Errno ERRNO_VALIDATE_FILE_EXISTENCE_AND_PERMISSIONS_FAILED_WITH_EXCEPTION = new Errno(TYPE, 159, "Validating file existence and permissions of %1$s at path \"%2$s\" failed.\nException: %3$s");
|
||||
public static final Errno ERRNO_VALIDATE_DIRECTORY_EXISTENCE_AND_PERMISSIONS_FAILED_WITH_EXCEPTION = new Errno(TYPE, 160, "Validating directory existence and permissions of %1$s at path \"%2$s\" failed.\nException: %3$s");
|
||||
|
||||
|
||||
|
||||
@@ -72,13 +76,31 @@ public class FileUtilsErrno extends Errno {
|
||||
|
||||
/* Errors for invalid file permissions (400-450) */
|
||||
public static final Errno ERRNO_INVALID_FILE_PERMISSIONS_STRING_TO_CHECK = new Errno(TYPE, 400, "The file permission string to check is invalid.");
|
||||
public static final Errno ERRNO_FILE_NOT_READABLE = new Errno(TYPE, 401, "The %1$s at path is not readable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_WRITABLE = new Errno(TYPE, 402, "The %1$s at path is not writable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_EXECUTABLE = new Errno(TYPE, 403, "The %1$s at path is not executable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_READABLE = new Errno(TYPE, 401, "The %1$s at path \"%2$s\" is not readable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_READABLE_SHORT = new Errno(TYPE, 402, "The %1$s at path is not readable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_WRITABLE = new Errno(TYPE, 403, "The %1$s at path \"%2$s\" is not writable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_WRITABLE_SHORT = new Errno(TYPE, 404, "The %1$s at path is not writable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_EXECUTABLE = new Errno(TYPE, 405, "The %1$s at path \"%2$s\" is not executable. Permission Denied.");
|
||||
public static final Errno ERRNO_FILE_NOT_EXECUTABLE_SHORT = new Errno(TYPE, 406, "The %1$s at path is not executable. Permission Denied.");
|
||||
|
||||
|
||||
FileUtilsErrno(final String type, final int code, final String message) {
|
||||
super(type, code, message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Defines the {@link Errno} mapping to get a shorter version of {@link FileUtilsErrno}. */
|
||||
public static Map<Errno, Errno> ERRNO_SHORT_MAPPING = new HashMap<Errno, Errno>() {{
|
||||
put(ERRNO_FILE_NOT_FOUND_AT_PATH, ERRNO_FILE_NOT_FOUND_AT_PATH_SHORT);
|
||||
|
||||
put(ERRNO_NON_REGULAR_FILE_FOUND, ERRNO_NON_REGULAR_FILE_FOUND_SHORT);
|
||||
put(ERRNO_NON_DIRECTORY_FILE_FOUND, ERRNO_NON_DIRECTORY_FILE_FOUND_SHORT);
|
||||
put(ERRNO_NON_SYMLINK_FILE_FOUND, ERRNO_NON_SYMLINK_FILE_FOUND_SHORT);
|
||||
|
||||
put(ERRNO_FILE_NOT_READABLE, ERRNO_FILE_NOT_READABLE_SHORT);
|
||||
put(ERRNO_FILE_NOT_WRITABLE, ERRNO_FILE_NOT_WRITABLE_SHORT);
|
||||
put(ERRNO_FILE_NOT_EXECUTABLE, ERRNO_FILE_NOT_EXECUTABLE_SHORT);
|
||||
}};
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user