-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandling.java
More file actions
99 lines (89 loc) · 4.94 KB
/
ErrorHandling.java
File metadata and controls
99 lines (89 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// java examples.general.ErrorHandling YOUR_TOKEN
//
// Детальная обработка всех типов ошибок.
package examples.general;
import com.lolzteam.LolzteamClient;
import com.lolzteam.LolzteamException;
import com.lolzteam.models.*;
public class ErrorHandling {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("Usage: ErrorHandling <TOKEN>");
System.exit(1);
}
String token = args[0];
LolzteamClient client = LolzteamClient.create(token);
// 1. Успешный запрос — typed response
System.out.println("--- successful request ---");
try {
UsersGetResponse resp = client.forum().usersGet(1L, null);
User user = resp.getUser();
System.out.println("user: " + user.getUsername());
} catch (LolzteamException e) {
handleError(e);
}
// 2. 404 — несуществующий пользователь
System.out.println("\n--- not found (404) ---");
try {
UsersGetResponse resp = client.forum().usersGet(999999999L, null);
System.out.println("user: " + resp.getUser());
} catch (LolzteamException e) {
handleError(e);
}
// 3. 401 — невалидный токен
System.out.println("\n--- auth error (401) ---");
try {
LolzteamClient badClient = LolzteamClient.create("invalid_token_12345");
UsersGetResponse resp = badClient.forum().usersGet(1L, null);
System.out.println("user: " + resp.getUser());
} catch (LolzteamException e) {
handleError(e);
}
// 4. Использование getStatusCode() для программной обработки
System.out.println("\n--- getStatusCode() helper ---");
try {
client.forum().usersGet(999999999L, null);
} catch (LolzteamException e) {
int code = e.getStatusCode();
System.out.println("HTTP status: " + code);
switch (code) {
case 401: System.out.println(" -> проверьте токен"); break;
case 403: System.out.println(" -> недостаточно прав"); break;
case 404: System.out.println(" -> ресурс не найден"); break;
case 429: System.out.println(" -> слишком много запросов"); break;
default: System.out.println(" -> неизвестная ошибка"); break;
}
}
System.out.println("\ndone");
}
static void handleError(LolzteamException err) {
if (err instanceof LolzteamException.AuthException) {
System.out.println("[AUTH] Ошибка авторизации: " + err.getMessage());
System.out.println(" Проверьте токен.");
} else if (err instanceof LolzteamException.ForbiddenException) {
System.out.println("[FORBIDDEN] Доступ запрещён: " + err.getMessage());
System.out.println(" У токена нет прав на этот ресурс.");
} else if (err instanceof LolzteamException.NotFoundException) {
System.out.println("[NOT FOUND] Не найдено: " + err.getMessage());
} else if (err instanceof LolzteamException.RateLimitedException) {
LolzteamException.RateLimitedException rle = (LolzteamException.RateLimitedException) err;
System.out.println("[RATE LIMIT] Лимит после " + rle.getAttempts() + " попыток.");
System.out.println(" Клиент уже сделал ретрай с backoff.");
} else if (err instanceof LolzteamException.RetryExhaustedException) {
LolzteamException.RetryExhaustedException ree = (LolzteamException.RetryExhaustedException) err;
System.out.println("[RETRY EXHAUSTED] Все " + ree.getAttempts() + " попыток исчерпаны.");
System.out.println(" Последняя ошибка: " + ree.getLastError().getMessage());
} else if (err instanceof LolzteamException.ConfigException) {
System.out.println("[CONFIG] Ошибка конфигурации: " + err.getMessage());
} else if (err instanceof LolzteamException.ApiException) {
LolzteamException.ApiException ae = (LolzteamException.ApiException) err;
System.out.println("[API] HTTP " + ae.getStatusCode() + ": " + ae.getBody());
} else if (err instanceof LolzteamException.HttpException) {
System.out.println("[HTTP] Сетевая ошибка: " + err.getMessage());
} else if (err instanceof LolzteamException.JsonException) {
System.out.println("[JSON] Ошибка парсинга: " + err.getMessage());
} else {
System.out.println("[UNKNOWN] " + err.getMessage());
}
}
}