Skip to content

Commit 10acd5d

Browse files
Add HOST_NAME validation in HelloController and improve error handling for message/file sending in HelloModel.
1 parent e04e60b commit 10acd5d

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/main/java/com/example/HelloController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public class HelloController {
2222

2323
private static final String HOST_NAME = System.getenv("HOST_NAME");
2424

25+
static {
26+
if (HOST_NAME == null || HOST_NAME.isBlank()) {
27+
throw new IllegalStateException(
28+
"Environment variable HOST_NAME must be set to the server URL."
29+
);
30+
}
31+
}
32+
2533

2634

2735

src/main/java/com/example/HelloModel.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
import java.io.File;
1515

16-
import java.io.FileNotFoundException;
17-
import java.io.IOException;
18-
import java.util.*;
1916
import java.util.concurrent.CompletableFuture;
2017

2118

@@ -78,6 +75,12 @@ public void sendMessage() {
7875
CompletableFuture.runAsync(() -> {
7976
// Nätverksanropet sker här i bakgrunden
8077
connection.send(message, currentTopic.get());
78+
try {
79+
connection.send(message, currentTopic.get());
80+
} catch (Exception e) {
81+
System.err.println("Failed to send message: " + e.getMessage());
82+
83+
}
8184
});
8285
}
8386

@@ -88,18 +91,26 @@ public void sendMessage() {
8891
*/
8992
public void sendFile() {
9093
File file = fileToSend.get();
94+
if (file == null) {
95+
System.err.println("Cannot send file: no file selected.");
96+
return;
97+
}
9198
final NtfyMessageDto localSentDto = new NtfyMessageDto(file.getName(), currentTopic.get(), "file", true);
9299
runOnFx(() -> {
93100
messages.add(localSentDto);
94101
fileToSend.set(null); // Rensa filbilagan i UI
95102
});
96103

97-
// 2. Utför nätverksanropet på en bakgrundstråd
98104

99105
// 2. Utför nätverksanropet på en bakgrundstråd
100106
CompletableFuture.runAsync(() -> {
101107
// Nätverksanropet sker här i bakgrunden
102108
connection.sendFile(file, currentTopic.get());
109+
try {
110+
connection.sendFile(file, currentTopic.get());
111+
} catch( Exception e){
112+
System.err.println("Failed to send file: " + e.getMessage());
113+
}
103114
});
104115
}
105116

0 commit comments

Comments
 (0)