termux-app

Форк
0
118 строк · 3.8 Кб
1
package com.termux.shared.errors;
2

3
import android.app.Activity;
4

5
import androidx.annotation.NonNull;
6

7
import com.termux.shared.logger.Logger;
8

9
import java.util.Arrays;
10
import java.util.Collections;
11
import java.util.HashMap;
12
import java.util.List;
13

14
/** The {@link Class} that defines error messages and codes. */
15
public class Errno {
16

17
    private static final HashMap<String, Errno> map = new HashMap<>();
18

19
    public static final String TYPE = "Error";
20

21

22
    public static final Errno ERRNO_SUCCESS = new Errno(TYPE, Activity.RESULT_OK, "Success");
23
    public static final Errno ERRNO_CANCELLED = new Errno(TYPE, Activity.RESULT_CANCELED, "Cancelled");
24
    public static final Errno ERRNO_MINOR_FAILURES = new Errno(TYPE, Activity.RESULT_FIRST_USER, "Minor failure");
25
    public static final Errno ERRNO_FAILED = new Errno(TYPE, Activity.RESULT_FIRST_USER + 1, "Failed");
26

27
    /** The errno type. */
28
    protected final String type;
29
    /** The errno code. */
30
    protected final int code;
31
    /** The errno message. */
32
    protected final String message;
33

34
    private static final String LOG_TAG = "Errno";
35

36

37
    public Errno(@NonNull final String type, final int code, @NonNull final String message) {
38
        this.type = type;
39
        this.code = code;
40
        this.message = message;
41
        map.put(type + ":" + code, this);
42
    }
43

44
    @NonNull
45
    @Override
46
    public String toString() {
47
        return "type=" + type + ", code=" + code + ", message=\"" + message + "\"";
48
    }
49

50
    @NonNull
51
    public String getType() {
52
        return type;
53
    }
54

55
    public int getCode() {
56
        return code;
57
    }
58

59
    @NonNull
60
    public String getMessage() {
61
        return 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
     */
72
    public static Errno valueOf(String type, Integer code) {
73
        if (type == null || type.isEmpty() || code == null) return null;
74
        return map.get(type + ":" + code);
75
    }
76

77

78

79
    public Error getError() {
80
        return new Error(getType(), getCode(), getMessage());
81
    }
82

83
    public Error getError(Object... args) {
84
        try {
85
            return new Error(getType(), getCode(), String.format(getMessage(), args));
86
        } catch (Exception e) {
87
            Logger.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
89
            return new Error(getType(), getCode(), getMessage() + ": " + Arrays.toString(args));
90
        }
91
    }
92

93
    public Error getError(Throwable throwable, Object... args) {
94
        if (throwable == null)
95
            return getError(args);
96
        else
97
            return getError(Collections.singletonList(throwable), args);
98
    }
99

100
    public Error getError(List<Throwable> throwablesList, Object... args) {
101
        try {
102
            if (throwablesList == null)
103
                return new Error(getType(), getCode(), String.format(getMessage(), args));
104
            else
105
                return new Error(getType(), getCode(), String.format(getMessage(), args), throwablesList);
106
        } catch (Exception e) {
107
            Logger.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
109
            return new Error(getType(), getCode(), getMessage() + ": " + Arrays.toString(args), throwablesList);
110
        }
111
    }
112

113
    public boolean equalsErrorTypeAndCode(Error error) {
114
        if (error == null) return false;
115
        return type.equals(error.getType()) && code == error.getCode();
116
    }
117

118
}
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.