mirror of
https://github.com/fankes/termux-app.git
synced 2025-10-22 19:59:20 +08:00
Changed!: Move to package-by-feature hierarchy for classes not using it since termux-shared is growing too big and layers are getting out of hand
This commit is contained in:
110
termux-shared/src/main/java/com/termux/shared/errors/Errno.java
Normal file
110
termux-shared/src/main/java/com/termux/shared/errors/Errno.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package com.termux.shared.errors;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
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";
|
||||
|
||||
|
||||
public static final Errno ERRNO_SUCCESS = new Errno(TYPE, Activity.RESULT_OK, "Success");
|
||||
public static final Errno ERRNO_CANCELLED = new Errno(TYPE, Activity.RESULT_CANCELED, "Cancelled");
|
||||
public static final Errno ERRNO_MINOR_FAILURES = new Errno(TYPE, Activity.RESULT_FIRST_USER, "Minor failure");
|
||||
public static final Errno ERRNO_FAILED = new Errno(TYPE, Activity.RESULT_FIRST_USER + 1, "Failed");
|
||||
|
||||
/** The errno type. */
|
||||
protected String type;
|
||||
/** The errno code. */
|
||||
protected final int code;
|
||||
/** The errno message. */
|
||||
protected final String message;
|
||||
|
||||
private static final String LOG_TAG = "Errno";
|
||||
|
||||
|
||||
public Errno(final String type, final int code, final String message) {
|
||||
this.type = type;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
map.put(type + ":" + code, this);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return "type=" + type + ", code=" + code + ", message=\"" + message + "\"";
|
||||
}
|
||||
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
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() {
|
||||
return new Error(getType(), getCode(), getMessage());
|
||||
}
|
||||
|
||||
public Error getError(Object... args) {
|
||||
try {
|
||||
return new Error(getType(), getCode(), String.format(getMessage(), args));
|
||||
} 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
|
||||
return new Error(getType(), getCode(), getMessage() + ": " + Arrays.toString(args));
|
||||
}
|
||||
}
|
||||
|
||||
public Error getError(Throwable throwable, Object... args) {
|
||||
if (throwable == null)
|
||||
return getError(args);
|
||||
else
|
||||
return getError(Collections.singletonList(throwable), args);
|
||||
}
|
||||
|
||||
public Error getError(List<Throwable> throwablesList, Object... args) {
|
||||
try {
|
||||
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
|
||||
return new Error(getType(), getCode(), getMessage() + ": " + Arrays.toString(args), throwablesList);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
297
termux-shared/src/main/java/com/termux/shared/errors/Error.java
Normal file
297
termux-shared/src/main/java/com/termux/shared/errors/Error.java
Normal file
@@ -0,0 +1,297 @@
|
||||
package com.termux.shared.errors;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.termux.shared.logger.Logger;
|
||||
import com.termux.shared.markdown.MarkdownUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
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. */
|
||||
private int code;
|
||||
/** The error message. */
|
||||
private String message;
|
||||
/** The error exceptions. */
|
||||
private List<Throwable> throwablesList = new ArrayList<>();
|
||||
|
||||
private static final String LOG_TAG = "Error";
|
||||
|
||||
|
||||
public Error() {
|
||||
InitError(null, null, null, null);
|
||||
}
|
||||
|
||||
public Error(String type, Integer code, String message, List<Throwable> throwablesList) {
|
||||
InitError(type, code, message, throwablesList);
|
||||
}
|
||||
|
||||
public Error(String type, Integer code, String message, Throwable throwable) {
|
||||
InitError(type, code, message, Collections.singletonList(throwable));
|
||||
}
|
||||
|
||||
public Error(String type, Integer code, String message) {
|
||||
InitError(type, code, message, null);
|
||||
}
|
||||
|
||||
public Error(Integer code, String message, List<Throwable> throwablesList) {
|
||||
InitError(null, code, message, throwablesList);
|
||||
}
|
||||
|
||||
public Error(Integer code, String message, Throwable throwable) {
|
||||
InitError(null, code, message, Collections.singletonList(throwable));
|
||||
}
|
||||
|
||||
public Error(Integer code, String message) {
|
||||
InitError(null, code, message, null);
|
||||
}
|
||||
|
||||
public Error(String message, Throwable throwable) {
|
||||
InitError(null, null, message, Collections.singletonList(throwable));
|
||||
}
|
||||
|
||||
public Error(String message, List<Throwable> throwablesList) {
|
||||
InitError(null, null, message, throwablesList);
|
||||
}
|
||||
|
||||
public Error(String message) {
|
||||
InitError(null, null, message, null);
|
||||
}
|
||||
|
||||
private void InitError(String type, Integer code, String message, List<Throwable> throwablesList) {
|
||||
if (type != null && !type.isEmpty())
|
||||
this.type = type;
|
||||
else
|
||||
this.type = Errno.TYPE;
|
||||
|
||||
if (code != null && code > Errno.ERRNO_SUCCESS.getCode())
|
||||
this.code = code;
|
||||
else
|
||||
this.code = Errno.ERRNO_SUCCESS.getCode();
|
||||
|
||||
this.message = message;
|
||||
|
||||
if (throwablesList != null)
|
||||
this.throwablesList = throwablesList;
|
||||
}
|
||||
|
||||
public Error setLabel(String label) {
|
||||
this.label = label;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void prependMessage(String message) {
|
||||
if (message != null && isStateFailed())
|
||||
this.message = message + this.message;
|
||||
}
|
||||
|
||||
public void appendMessage(String message) {
|
||||
if (message != null && isStateFailed())
|
||||
this.message = this.message + message;
|
||||
}
|
||||
|
||||
public List<Throwable> getThrowablesList() {
|
||||
return Collections.unmodifiableList(throwablesList);
|
||||
}
|
||||
|
||||
|
||||
public synchronized boolean setStateFailed(@NonNull Error error) {
|
||||
return setStateFailed(error.getType(), error.getCode(), error.getMessage(), null);
|
||||
}
|
||||
|
||||
public synchronized boolean setStateFailed(@NonNull Error error, Throwable throwable) {
|
||||
return setStateFailed(error.getType(), error.getCode(), error.getMessage(), Collections.singletonList(throwable));
|
||||
}
|
||||
public synchronized boolean setStateFailed(@NonNull Error error, List<Throwable> throwablesList) {
|
||||
return setStateFailed(error.getType(), error.getCode(), error.getMessage(), throwablesList);
|
||||
}
|
||||
|
||||
public synchronized boolean setStateFailed(int code, String message) {
|
||||
return setStateFailed(this.type, code, message, null);
|
||||
}
|
||||
|
||||
public synchronized boolean setStateFailed(int code, String message, Throwable throwable) {
|
||||
return setStateFailed(this.type, code, message, Collections.singletonList(throwable));
|
||||
}
|
||||
|
||||
public synchronized boolean setStateFailed(int code, String message, List<Throwable> throwablesList) {
|
||||
return setStateFailed(this.type, code, message, throwablesList);
|
||||
}
|
||||
|
||||
public synchronized boolean setStateFailed(String type, int code, String message, List<Throwable> throwablesList) {
|
||||
this.message = message;
|
||||
this.throwablesList = throwablesList;
|
||||
|
||||
if (type != null && !type.isEmpty())
|
||||
this.type = type;
|
||||
|
||||
if (code > Errno.ERRNO_SUCCESS.getCode()) {
|
||||
this.code = code;
|
||||
return true;
|
||||
} else {
|
||||
Logger.logWarn(LOG_TAG, "Ignoring invalid error code value \"" + code + "\". Force setting it to RESULT_CODE_FAILED \"" + Errno.ERRNO_FAILED.getCode() + "\"");
|
||||
this.code = Errno.ERRNO_FAILED.getCode();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isStateFailed() {
|
||||
return code > Errno.ERRNO_SUCCESS.getCode();
|
||||
}
|
||||
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return getErrorLogString(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Log the {@link Error} and show a toast for the minimal {@link String} for the {@link Error}.
|
||||
*
|
||||
* @param context The {@link Context} for operations.
|
||||
* @param logTag The log tag to use for logging.
|
||||
* @param error The {@link Error} to convert.
|
||||
*/
|
||||
public static void logErrorAndShowToast(Context context, String logTag, Error error) {
|
||||
if (error == null) return;
|
||||
error.logErrorAndShowToast(context, logTag);
|
||||
}
|
||||
|
||||
public void logErrorAndShowToast(Context context, String logTag) {
|
||||
Logger.logErrorExtended(logTag, getErrorLogString());
|
||||
Logger.showToast(context, getMinimalErrorLogString(), true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a log friendly {@link String} for {@link Error} error parameters.
|
||||
*
|
||||
* @param error The {@link Error} to convert.
|
||||
* @return Returns the log friendly {@link String}.
|
||||
*/
|
||||
public static String getErrorLogString(final Error error) {
|
||||
if (error == null) return "null";
|
||||
return error.getErrorLogString();
|
||||
}
|
||||
|
||||
public String getErrorLogString() {
|
||||
StringBuilder logString = new StringBuilder();
|
||||
|
||||
logString.append(getCodeString());
|
||||
logString.append("\n").append(getTypeAndMessageLogString());
|
||||
if (this.throwablesList != null)
|
||||
logString.append("\n").append(geStackTracesLogString());
|
||||
|
||||
return logString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a minimal log friendly {@link String} for {@link Error} error parameters.
|
||||
*
|
||||
* @param error The {@link Error} to convert.
|
||||
* @return Returns the log friendly {@link String}.
|
||||
*/
|
||||
public static String getMinimalErrorLogString(final Error error) {
|
||||
if (error == null) return "null";
|
||||
return error.getMinimalErrorLogString();
|
||||
}
|
||||
|
||||
public String getMinimalErrorLogString() {
|
||||
StringBuilder logString = new StringBuilder();
|
||||
|
||||
logString.append(getCodeString());
|
||||
logString.append(getTypeAndMessageLogString());
|
||||
|
||||
return logString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a minimal {@link String} for {@link Error} error parameters.
|
||||
*
|
||||
* @param error The {@link Error} to convert.
|
||||
* @return Returns the {@link String}.
|
||||
*/
|
||||
public static String getMinimalErrorString(final Error error) {
|
||||
if (error == null) return "null";
|
||||
return error.getMinimalErrorString();
|
||||
}
|
||||
|
||||
public String getMinimalErrorString() {
|
||||
StringBuilder logString = new StringBuilder();
|
||||
|
||||
logString.append("(").append(getCode()).append(") ");
|
||||
logString.append(getType()).append(": ").append(getMessage());
|
||||
|
||||
return logString.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a markdown {@link String} for {@link Error}.
|
||||
*
|
||||
* @param error The {@link Error} to convert.
|
||||
* @return Returns the markdown {@link String}.
|
||||
*/
|
||||
public static String getErrorMarkdownString(final Error error) {
|
||||
if (error == null) return "null";
|
||||
return error.getErrorMarkdownString();
|
||||
}
|
||||
|
||||
public String getErrorMarkdownString() {
|
||||
StringBuilder markdownString = new StringBuilder();
|
||||
|
||||
markdownString.append(MarkdownUtils.getSingleLineMarkdownStringEntry("Error Code", getCode(), "-"));
|
||||
markdownString.append("\n").append(MarkdownUtils.getMultiLineMarkdownStringEntry(
|
||||
(Errno.TYPE.equals(getType()) ? "Error Message" : "Error Message (" + getType() + ")"), message, "-"));
|
||||
markdownString.append("\n\n").append(geStackTracesMarkdownString());
|
||||
|
||||
return markdownString.toString();
|
||||
}
|
||||
|
||||
|
||||
public String getCodeString() {
|
||||
return Logger.getSingleLineLogStringEntry("Error Code", code, "-");
|
||||
}
|
||||
|
||||
public String getTypeAndMessageLogString() {
|
||||
return Logger.getMultiLineLogStringEntry(Errno.TYPE.equals(type) ? "Error Message" : "Error Message (" + type + ")", message, "-");
|
||||
}
|
||||
|
||||
public String geStackTracesLogString() {
|
||||
return Logger.getStackTracesString("StackTraces:", Logger.getStackTracesStringArray(throwablesList));
|
||||
}
|
||||
|
||||
public String geStackTracesMarkdownString() {
|
||||
return Logger.getStackTracesMarkdownString("StackTraces", Logger.getStackTracesStringArray(throwablesList));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.termux.shared.errors;
|
||||
|
||||
/** The {@link Class} that defines function error messages and codes. */
|
||||
public class FunctionErrno extends Errno {
|
||||
|
||||
public static final String TYPE = "Function Error";
|
||||
|
||||
|
||||
/* Errors for null or empty parameters (100-150) */
|
||||
public static final Errno ERRNO_NULL_OR_EMPTY_PARAMETER = new Errno(TYPE, 100, "The %1$s parameter passed to \"%2$s\" is null or empty.");
|
||||
public static final Errno ERRNO_NULL_OR_EMPTY_PARAMETERS = new Errno(TYPE, 101, "The %1$s parameters passed to \"%2$s\" are null or empty.");
|
||||
public static final Errno ERRNO_UNSET_PARAMETER = new Errno(TYPE, 102, "The %1$s parameter passed to \"%2$s\" must be set.");
|
||||
public static final Errno ERRNO_UNSET_PARAMETERS = new Errno(TYPE, 103, "The %1$s parameters passed to \"%2$s\" must be set.");
|
||||
public static final Errno ERRNO_INVALID_PARAMETER = new Errno(TYPE, 104, "The %1$s parameter passed to \"%2$s\" is invalid.\"%3$s\"");
|
||||
public static final Errno ERRNO_PARAMETER_NOT_INSTANCE_OF = new Errno(TYPE, 104, "The %1$s parameter passed to \"%2$s\" is not an instance of %3$s.");
|
||||
|
||||
|
||||
FunctionErrno(final String type, final int code, final String message) {
|
||||
super(type, code, message);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user