From 24276b705b495f7f9ebe0a56214ec80432cb6be7 Mon Sep 17 00:00:00 2001 From: Elvis de Freitas Date: Tue, 6 Aug 2024 22:52:19 -0300 Subject: [PATCH 1/3] handling and logging fatal errors --- .../java/com/mageddo/dnsproxyserver/App.java | 40 +++++++++++++++++++ .../mageddo/dnsproxyserver/AppCompTest.java | 24 ++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/mageddo/dnsproxyserver/App.java b/src/main/java/com/mageddo/dnsproxyserver/App.java index 054adec50..4e32b66cb 100644 --- a/src/main/java/com/mageddo/dnsproxyserver/App.java +++ b/src/main/java/com/mageddo/dnsproxyserver/App.java @@ -7,10 +7,14 @@ import com.mageddo.dnsproxyserver.config.dataprovider.vo.ConfigFlag; import com.mageddo.dnsproxyserver.di.Context; import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; import java.nio.file.Files; import java.nio.file.Paths; +@Slf4j public class App { private final String[] args; @@ -26,7 +30,21 @@ public static void main(String[] args) { } void start() { + try { + log.trace("status=starting"); + this.mustStart(); + } catch (SystemExitException e) { + throw e; + } catch (Throwable e) { + log.error( + "status=fatalError, action=exit, msg={}, class={}", + ExceptionUtils.getMessage(e), ClassUtils.getSimpleName(e), e + ); + this.exitWithError(128); + } + } + void mustStart() { this.flags = ConfigFlag.parse(this.args); this.checkHiddenCommands(); @@ -46,6 +64,7 @@ void checkHiddenCommands() { if (this.flags.isCreateTmpDir()) { this.createTmpDirIfNotExists(); } + log.trace("status=checked"); } Config findConfig(String[] args) { @@ -54,7 +73,9 @@ Config findConfig(String[] args) { } void setupLogs() { + log.trace("status=configuring"); new LogSettings().setupLogs(this.config); + log.trace("status=configured"); } void startContext() { @@ -69,15 +90,34 @@ void checkExitCommands() { if (flags.isHelp() || flags.isVersion()) { exitGracefully(); } + log.trace("status=checked"); } void exitGracefully() { System.exit(0); } + void exitWithError(int errorCode) { + System.exit(errorCode); + } + @SneakyThrows void createTmpDirIfNotExists() { final var tmpDir = Paths.get(System.getProperty("java.io.tmpdir")); Files.createDirectories(tmpDir); } + + Config getConfig() { + return config; + } + + int getDnsServerPort() { + return getConfig().getDnsServerPort(); + } + + static class SystemExitException extends RuntimeException { + public SystemExitException(String reason) { + super(reason); + } + } } diff --git a/src/test/java/com/mageddo/dnsproxyserver/AppCompTest.java b/src/test/java/com/mageddo/dnsproxyserver/AppCompTest.java index c3f0dcab6..b74515212 100644 --- a/src/test/java/com/mageddo/dnsproxyserver/AppCompTest.java +++ b/src/test/java/com/mageddo/dnsproxyserver/AppCompTest.java @@ -7,6 +7,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.never; @@ -68,9 +69,30 @@ void mustCreateTmpDirIfNotExists() { } + @Test + void mustHandleFatalErrors() { + // arrange + final var args = new String[]{"--create-tmp-dir"}; + this.setupStub(args); + + doThrow(new IllegalAccessError("mocked fatal error")) + .when(this.app) + .checkHiddenCommands() + ; + doNothing() + .when(this.app) + .exitWithError(anyInt()) + ; + + // act + this.app.start(); + + verify(this.app).exitWithError(anyInt()); + + } RuntimeException mockExitMethod() { - final var expectedException = new RuntimeException("must exit"); + final var expectedException = new App.SystemExitException("testing"); doThrow(expectedException) .when(this.app) .exitGracefully() From 46ed56fb92c276551573f9219b46c704e130da94 Mon Sep 17 00:00:00 2001 From: Elvis de Freitas Date: Tue, 6 Aug 2024 22:52:39 -0300 Subject: [PATCH 2/3] release notes --- RELEASE-NOTES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 2ca348c1e..a62b685cd 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,3 +1,6 @@ +## 3.25.9 +* Handling and logging fatal errors #480 + ## 3.25.8 * SolverRemote: Exclude remote servers with open circuits. #526 From 8e745b3e89edc7d288fdd77ab50c89f22b432260 Mon Sep 17 00:00:00 2001 From: Elvis de Freitas Date: Tue, 6 Aug 2024 22:53:10 -0300 Subject: [PATCH 3/3] [Gradle Release Plugin] - new version commit: '3.25.9-snapshot'. --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 109b26195..733b32502 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=3.25.8-snapshot +version=3.25.9-snapshot