Skip to content

Commit 4e1a38b

Browse files
committed
fix(контроллеры): перехват 401/403 с редиректом на вход (#94)
Базовый Controller: общий showError(String) поверх getErrorLabel(), showError(Throwable) перехватывает 401/403 в session.clear() + goToLoginOnce(), хук interceptAuthFailure() (LoginFormController перекрывает в false). Navigator.goToLoginOnce() с once-флагом против параллельных 403. Все 8 контроллеров мигрированы. Тесты обоих путей 401/403, неверный пароль остаётся на форме, выход без мелькания.
1 parent eca5dc5 commit 4e1a38b

16 files changed

Lines changed: 618 additions & 180 deletions

‎src/main/java/com/DesktopApplicationClientJava/controllers/LoginFormController.java‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public class LoginFormController extends Controller {
1818
@FXML Label errorLabel;
1919
@FXML Button submitButton;
2020

21+
@Override
22+
protected Label getErrorLabel() {
23+
return errorLabel;
24+
}
25+
26+
@Override
27+
protected boolean interceptAuthFailure() {
28+
return false;
29+
}
30+
2131
@FXML
2232
void onSubmit(ActionEvent event) {
2333
String email = emailField.getText() == null ? "" : emailField.getText().trim();
@@ -42,19 +52,7 @@ void onSubmit(ActionEvent event) {
4252
hideError();
4353
navigator.goTo("/fxml/ResourceList.fxml");
4454
} catch (ServiceException e) {
45-
showError(e.getMessage());
55+
showError(e);
4656
}
4757
}
48-
49-
private void showError(String message) {
50-
errorLabel.setText(message);
51-
errorLabel.setVisible(true);
52-
errorLabel.setManaged(true);
53-
}
54-
55-
private void hideError() {
56-
errorLabel.setText("");
57-
errorLabel.setVisible(false);
58-
errorLabel.setManaged(false);
59-
}
6058
}

‎src/main/java/com/DesktopApplicationClientJava/controllers/ResourceDetailsController.java‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public class ResourceDetailsController extends Controller {
4848
@FXML Button editButton;
4949
@FXML Button deleteButton;
5050

51+
@Override
52+
protected Label getErrorLabel() {
53+
return errorLabel;
54+
}
55+
5156
private UUID resourceId;
5257

5358
/**
@@ -101,7 +106,7 @@ public void load() {
101106
try {
102107
showResource(services.getResourceService().getById(resourceId));
103108
} catch (ServiceException e) {
104-
showError(e.getMessage());
109+
showError(e);
105110
}
106111
}
107112

@@ -120,7 +125,7 @@ void onDownload(FileInfo file) {
120125
Path saved = services.getFileService().downloadFile(resourceId, file.getUuid(), dir.toPath());
121126
statusLabel.setText("Сохранено: " + saved.getFileName());
122127
} catch (ServiceException e) {
123-
showError(e.getMessage());
128+
showError(e);
124129
}
125130
}
126131

@@ -169,18 +174,6 @@ private static String formatSize(Long bytes) {
169174
return String.format("%.1f GB", bytes / (1024.0 * 1024 * 1024));
170175
}
171176

172-
private void showError(String message) {
173-
errorLabel.setText(message);
174-
errorLabel.setVisible(true);
175-
errorLabel.setManaged(true);
176-
}
177-
178-
private void hideError() {
179-
errorLabel.setText("");
180-
errorLabel.setVisible(false);
181-
errorLabel.setManaged(false);
182-
}
183-
184177
private static final class DownloadCell extends TableCell<FileInfo, Void> {
185178
private final Button button = new Button("Скачать");
186179

@@ -210,7 +203,7 @@ void onDelete(ActionEvent event) {
210203
try {
211204
services.getResourceService().delete(resourceId);
212205
} catch (ServiceException e) {
213-
showError(e.getMessage());
206+
showError(e);
214207
return;
215208
}
216209
navigator.goTo("/fxml/ResourceList.fxml");

‎src/main/java/com/DesktopApplicationClientJava/controllers/ResourceFormController.java‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class ResourceFormController extends Controller {
2323
@FXML Button cancelButton;
2424
@FXML Button saveButton;
2525

26+
@Override
27+
protected Label getErrorLabel() {
28+
return errorLabel;
29+
}
30+
2631
private UUID resourceId;
2732

2833
/**
@@ -55,7 +60,7 @@ private void loadExisting() {
5560
titleField.setText(r.getTitle() == null ? "" : r.getTitle());
5661
descriptionArea.setText(r.getDescription() == null ? "" : r.getDescription());
5762
} catch (ServiceException e) {
58-
showError(e.getMessage());
63+
showError(e);
5964
}
6065
}
6166

@@ -90,24 +95,12 @@ void onSave(ActionEvent event) {
9095
}
9196
navigator.goTo("/fxml/ResourceList.fxml");
9297
} catch (ServiceException e) {
93-
showError(e.getMessage());
98+
showError(e);
9499
}
95100
}
96101

97102
@FXML
98103
void onCancel(ActionEvent event) {
99104
navigator.goTo("/fxml/ResourceList.fxml");
100105
}
101-
102-
private void showError(String message) {
103-
errorLabel.setText(message);
104-
errorLabel.setVisible(true);
105-
errorLabel.setManaged(true);
106-
}
107-
108-
private void hideError() {
109-
errorLabel.setText("");
110-
errorLabel.setVisible(false);
111-
errorLabel.setManaged(false);
112-
}
113106
}

‎src/main/java/com/DesktopApplicationClientJava/controllers/ResourceListController.java‎

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.DesktopApplicationClientJava.controllers.utils.Controller;
44
import com.DesktopApplicationClientJava.entities.Resource;
5-
import com.DesktopApplicationClientJava.services.ServiceException;
65
import com.DesktopApplicationClientJava.services.filter.ResourceFilter;
76
import java.time.LocalDateTime;
87
import java.time.format.DateTimeFormatter;
@@ -53,6 +52,11 @@ public class ResourceListController extends Controller {
5352
private long offset = 0;
5453
private boolean canModify = false;
5554

55+
@Override
56+
protected Label getErrorLabel() {
57+
return errorLabel;
58+
}
59+
5660
// ===== Lifecycle =====
5761

5862
@FXML
@@ -184,9 +188,7 @@ protected List<Resource> call() {
184188
task.setOnFailed(
185189
e -> {
186190
var ex = task.getException();
187-
String msg =
188-
ex instanceof ServiceException se ? se.getMessage() : "Не удалось загрузить список";
189-
showError(msg);
191+
showError(ex, "Не удалось загрузить список");
190192
rows.clear();
191193
updatePaginationUi(0);
192194
statusLabel.setText("");
@@ -266,9 +268,7 @@ protected Void call() {
266268
task.setOnFailed(
267269
e -> {
268270
var ex = task.getException();
269-
String msg =
270-
ex instanceof ServiceException se ? se.getMessage() : "Не удалось удалить ресурс";
271-
showError(msg);
271+
showError(ex, "Не удалось удалить ресурс");
272272
statusLabel.setText("");
273273
});
274274

@@ -292,8 +292,16 @@ protected boolean confirmDelete(Resource resource) {
292292

293293
@FXML
294294
private void onLogout(ActionEvent e) {
295-
services.getAuthService().logout();
296-
navigator.goTo("/fxml/LoginForm.fxml");
295+
try {
296+
services.getAuthService().logout();
297+
} catch (RuntimeException ignored) {
298+
// Выход съедает ошибку сервиса без мелькания: редирект ниже выполняется в любом случае.
299+
} finally {
300+
if (session != null) {
301+
session.clear();
302+
}
303+
navigator.goToLoginOnce();
304+
}
297305
}
298306

299307
@FXML
@@ -331,16 +339,4 @@ private void openDetails(Resource resource) {
331339
private static String formatDate(LocalDateTime value) {
332340
return value == null ? "" : DATE_TIME_FORMAT.format(value);
333341
}
334-
335-
private void showError(String message) {
336-
errorLabel.setText(message);
337-
errorLabel.setVisible(true);
338-
errorLabel.setManaged(true);
339-
}
340-
341-
private void hideError() {
342-
errorLabel.setText("");
343-
errorLabel.setVisible(false);
344-
errorLabel.setManaged(false);
345-
}
346342
}

‎src/main/java/com/DesktopApplicationClientJava/controllers/UserCreateFormController.java‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class UserCreateFormController extends Controller {
2121
@FXML private TextField firstNameField;
2222
@FXML private TextField lastNameField;
2323

24+
@Override
25+
protected Label getErrorLabel() {
26+
return errorLabel;
27+
}
28+
2429
@FXML
2530
public void initialize() {
2631
// Способ 1: через setItems
@@ -72,24 +77,12 @@ private void onSubmit(ActionEvent event) {
7277
hideError();
7378
navigator.goTo("/fxml/UserList.fxml");
7479
} catch (ServiceException e) {
75-
showError(e.getMessage());
80+
showError(e);
7681
}
7782
}
7883

7984
@FXML
8085
void onCancel(ActionEvent event) {
8186
navigator.goTo("/fxml/UserList.fxml");
8287
}
83-
84-
private void showError(String message) {
85-
errorLabel.setText(message);
86-
errorLabel.setVisible(true);
87-
errorLabel.setManaged(true);
88-
}
89-
90-
private void hideError() {
91-
errorLabel.setText("");
92-
errorLabel.setVisible(false);
93-
errorLabel.setManaged(false);
94-
}
9588
}

‎src/main/java/com/DesktopApplicationClientJava/controllers/UserDetailsController.java‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.DesktopApplicationClientJava.controllers.utils.Controller;
44
import com.DesktopApplicationClientJava.entities.User;
5-
import com.DesktopApplicationClientJava.services.ServiceException;
65
import java.util.UUID;
76
import javafx.concurrent.Task;
87
import javafx.event.ActionEvent;
@@ -29,6 +28,11 @@ public class UserDetailsController extends Controller {
2928
private UUID userId;
3029
private User current;
3130

31+
@Override
32+
protected Label getErrorLabel() {
33+
return errorLabel;
34+
}
35+
3236
// ===== Lifecycle =====
3337

3438
@FXML
@@ -85,11 +89,7 @@ protected User call() {
8589
e -> {
8690
statusLabel.setText("");
8791
var ex = task.getException();
88-
String msg =
89-
ex instanceof ServiceException se
90-
? se.getMessage()
91-
: "Не удалось загрузить пользователя";
92-
showError(msg);
92+
showError(ex, "Не удалось загрузить пользователя");
9393
});
9494

9595
startDaemon(task, "user-details-load");
@@ -179,11 +179,7 @@ protected Void call() {
179179
ev -> {
180180
setBusy(false);
181181
var ex = task.getException();
182-
String msg =
183-
ex instanceof ServiceException se
184-
? se.getMessage()
185-
: "Не удалось удалить пользователя";
186-
showError(msg);
182+
showError(ex, "Не удалось удалить пользователя");
187183
});
188184

189185
startDaemon(task, "user-delete");
@@ -228,18 +224,6 @@ private void setBusy(boolean busy) {
228224
statusLabel.setText(busy ? "Удаление…" : "");
229225
}
230226

231-
private void showError(String message) {
232-
errorLabel.setText(message);
233-
errorLabel.setVisible(true);
234-
errorLabel.setManaged(true);
235-
}
236-
237-
private void hideError() {
238-
errorLabel.setText("");
239-
errorLabel.setVisible(false);
240-
errorLabel.setManaged(false);
241-
}
242-
243227
private static void startDaemon(Task<?> task, String name) {
244228
var thread = new Thread(task, name);
245229
thread.setDaemon(true);

‎src/main/java/com/DesktopApplicationClientJava/controllers/UserUpdateFormController.java‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.DesktopApplicationClientJava.controllers.utils.Controller;
44
import com.DesktopApplicationClientJava.entities.Role;
55
import com.DesktopApplicationClientJava.entities.User;
6-
import com.DesktopApplicationClientJava.services.ServiceException;
76
import com.DesktopApplicationClientJava.services.data.UpdateUserData;
87
import java.util.UUID;
98
import javafx.collections.FXCollections;
@@ -31,6 +30,11 @@ public class UserUpdateFormController extends Controller {
3130
private UUID userId;
3231
private User current;
3332

33+
@Override
34+
protected Label getErrorLabel() {
35+
return errorLabel;
36+
}
37+
3438
// ===== Lifecycle =====
3539

3640
@FXML
@@ -77,11 +81,7 @@ protected User call() {
7781
task.setOnFailed(
7882
e -> {
7983
var ex = task.getException();
80-
String msg =
81-
ex instanceof ServiceException se
82-
? se.getMessage()
83-
: "Не удалось загрузить пользователя";
84-
showError(msg);
84+
showError(ex, "Не удалось загрузить пользователя");
8585
setFormDisabled(true);
8686
});
8787

@@ -133,11 +133,7 @@ protected User call() {
133133
ev -> {
134134
setFormDisabled(false);
135135
var ex = task.getException();
136-
String msg =
137-
ex instanceof ServiceException se
138-
? se.getMessage()
139-
: "Не удалось сохранить изменения";
140-
showError(msg);
136+
showError(ex, "Не удалось сохранить изменения");
141137
});
142138

143139
startDaemon(task, "user-update-save");
@@ -226,18 +222,6 @@ private void setFormDisabled(boolean disabled) {
226222
saveButton.setDisable(disabled);
227223
}
228224

229-
private void showError(String message) {
230-
errorLabel.setText(message);
231-
errorLabel.setVisible(true);
232-
errorLabel.setManaged(true);
233-
}
234-
235-
private void hideError() {
236-
errorLabel.setText("");
237-
errorLabel.setVisible(false);
238-
errorLabel.setManaged(false);
239-
}
240-
241225
private static String safeTrim(String value) {
242226
return value == null ? null : value.trim();
243227
}

0 commit comments

Comments
 (0)