termux-app
118 строк · 3.8 Кб
1package com.termux.shared.errors;
2
3import android.app.Activity;
4
5import androidx.annotation.NonNull;
6
7import com.termux.shared.logger.Logger;
8
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.HashMap;
12import java.util.List;
13
14/** The {@link Class} that defines error messages and codes. */
15public class Errno {
16
17private static final HashMap<String, Errno> map = new HashMap<>();
18
19public static final String TYPE = "Error";
20
21
22public static final Errno ERRNO_SUCCESS = new Errno(TYPE, Activity.RESULT_OK, "Success");
23public static final Errno ERRNO_CANCELLED = new Errno(TYPE, Activity.RESULT_CANCELED, "Cancelled");
24public static final Errno ERRNO_MINOR_FAILURES = new Errno(TYPE, Activity.RESULT_FIRST_USER, "Minor failure");
25public static final Errno ERRNO_FAILED = new Errno(TYPE, Activity.RESULT_FIRST_USER + 1, "Failed");
26
27/** The errno type. */
28protected final String type;
29/** The errno code. */
30protected final int code;
31/** The errno message. */
32protected final String message;
33
34private static final String LOG_TAG = "Errno";
35
36
37public Errno(@NonNull final String type, final int code, @NonNull final String message) {
38this.type = type;
39this.code = code;
40this.message = message;
41map.put(type + ":" + code, this);
42}
43
44@NonNull
45@Override
46public String toString() {
47return "type=" + type + ", code=" + code + ", message=\"" + message + "\"";
48}
49
50@NonNull
51public String getType() {
52return type;
53}
54
55public int getCode() {
56return code;
57}
58
59@NonNull
60public String getMessage() {
61return message;
62}
63
64
65
66/**
67* Get the {@link Errno} of a specific type and code.
68*
69* @param type The unique type of the {@link Errno}.
70* @param code The unique code of the {@link Errno}.
71*/
72public static Errno valueOf(String type, Integer code) {
73if (type == null || type.isEmpty() || code == null) return null;
74return map.get(type + ":" + code);
75}
76
77
78
79public Error getError() {
80return new Error(getType(), getCode(), getMessage());
81}
82
83public Error getError(Object... args) {
84try {
85return new Error(getType(), getCode(), String.format(getMessage(), args));
86} catch (Exception e) {
87Logger.logWarn(LOG_TAG, "Exception raised while calling String.format() for error message of errno " + this + " with args" + Arrays.toString(args) + "\n" + e.getMessage());
88// Return unformatted message as a backup
89return new Error(getType(), getCode(), getMessage() + ": " + Arrays.toString(args));
90}
91}
92
93public Error getError(Throwable throwable, Object... args) {
94if (throwable == null)
95return getError(args);
96else
97return getError(Collections.singletonList(throwable), args);
98}
99
100public Error getError(List<Throwable> throwablesList, Object... args) {
101try {
102if (throwablesList == null)
103return new Error(getType(), getCode(), String.format(getMessage(), args));
104else
105return new Error(getType(), getCode(), String.format(getMessage(), args), throwablesList);
106} catch (Exception e) {
107Logger.logWarn(LOG_TAG, "Exception raised while calling String.format() for error message of errno " + this + " with args" + Arrays.toString(args) + "\n" + e.getMessage());
108// Return unformatted message as a backup
109return new Error(getType(), getCode(), getMessage() + ": " + Arrays.toString(args), throwablesList);
110}
111}
112
113public boolean equalsErrorTypeAndCode(Error error) {
114if (error == null) return false;
115return type.equals(error.getType()) && code == error.getCode();
116}
117
118}
119