Skip to content

Commit 360cf67

Browse files
authored
Handling and logging fatal errors (#542)
* handling and logging fatal errors * release notes * [Gradle Release Plugin] - new version commit: '3.25.9-snapshot'.
1 parent 92900ba commit 360cf67

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

RELEASE-NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.25.9
2+
* Handling and logging fatal errors #480
3+
14
## 3.25.8
25
* SolverRemote: Exclude remote servers with open circuits. #526
36

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=3.25.8-snapshot
1+
version=3.25.9-snapshot

src/main/java/com/mageddo/dnsproxyserver/App.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag;
88
import com.mageddo.dnsproxyserver.di.Context;
99
import lombok.SneakyThrows;
10+
import lombok.extern.slf4j.Slf4j;
11+
import org.apache.commons.lang3.ClassUtils;
12+
import org.apache.commons.lang3.exception.ExceptionUtils;
1013

1114
import java.nio.file.Files;
1215
import java.nio.file.Paths;
1316

17+
@Slf4j
1418
public class App {
1519

1620
private final String[] args;
@@ -26,7 +30,21 @@ public static void main(String[] args) {
2630
}
2731

2832
void start() {
33+
try {
34+
log.trace("status=starting");
35+
this.mustStart();
36+
} catch (SystemExitException e) {
37+
throw e;
38+
} catch (Throwable e) {
39+
log.error(
40+
"status=fatalError, action=exit, msg={}, class={}",
41+
ExceptionUtils.getMessage(e), ClassUtils.getSimpleName(e), e
42+
);
43+
this.exitWithError(128);
44+
}
45+
}
2946

47+
void mustStart() {
3048
this.flags = ConfigFlag.parse(this.args);
3149

3250
this.checkHiddenCommands();
@@ -46,6 +64,7 @@ void checkHiddenCommands() {
4664
if (this.flags.isCreateTmpDir()) {
4765
this.createTmpDirIfNotExists();
4866
}
67+
log.trace("status=checked");
4968
}
5069

5170
Config findConfig(String[] args) {
@@ -54,7 +73,9 @@ Config findConfig(String[] args) {
5473
}
5574

5675
void setupLogs() {
76+
log.trace("status=configuring");
5777
new LogSettings().setupLogs(this.config);
78+
log.trace("status=configured");
5879
}
5980

6081
void startContext() {
@@ -69,15 +90,34 @@ void checkExitCommands() {
6990
if (flags.isHelp() || flags.isVersion()) {
7091
exitGracefully();
7192
}
93+
log.trace("status=checked");
7294
}
7395

7496
void exitGracefully() {
7597
System.exit(0);
7698
}
7799

100+
void exitWithError(int errorCode) {
101+
System.exit(errorCode);
102+
}
103+
78104
@SneakyThrows
79105
void createTmpDirIfNotExists() {
80106
final var tmpDir = Paths.get(System.getProperty("java.io.tmpdir"));
81107
Files.createDirectories(tmpDir);
82108
}
109+
110+
Config getConfig() {
111+
return config;
112+
}
113+
114+
int getDnsServerPort() {
115+
return getConfig().getDnsServerPort();
116+
}
117+
118+
static class SystemExitException extends RuntimeException {
119+
public SystemExitException(String reason) {
120+
super(reason);
121+
}
122+
}
83123
}

src/test/java/com/mageddo/dnsproxyserver/AppCompTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.junit.jupiter.api.Assertions.assertEquals;
88
import static org.junit.jupiter.api.Assertions.assertThrows;
99
import static org.mockito.ArgumentMatchers.any;
10+
import static org.mockito.ArgumentMatchers.anyInt;
1011
import static org.mockito.Mockito.doNothing;
1112
import static org.mockito.Mockito.doThrow;
1213
import static org.mockito.Mockito.never;
@@ -68,9 +69,30 @@ void mustCreateTmpDirIfNotExists() {
6869

6970
}
7071

72+
@Test
73+
void mustHandleFatalErrors() {
74+
// arrange
75+
final var args = new String[]{"--create-tmp-dir"};
76+
this.setupStub(args);
77+
78+
doThrow(new IllegalAccessError("mocked fatal error"))
79+
.when(this.app)
80+
.checkHiddenCommands()
81+
;
82+
doNothing()
83+
.when(this.app)
84+
.exitWithError(anyInt())
85+
;
86+
87+
// act
88+
this.app.start();
89+
90+
verify(this.app).exitWithError(anyInt());
91+
92+
}
7193

7294
RuntimeException mockExitMethod() {
73-
final var expectedException = new RuntimeException("must exit");
95+
final var expectedException = new App.SystemExitException("testing");
7496
doThrow(expectedException)
7597
.when(this.app)
7698
.exitGracefully()

0 commit comments

Comments
 (0)