Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling and logging fatal errors #542

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.25.9
* Handling and logging fatal errors #480

## 3.25.8
* SolverRemote: Exclude remote servers with open circuits. #526

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=3.25.8-snapshot
version=3.25.9-snapshot
40 changes: 40 additions & 0 deletions src/main/java/com/mageddo/dnsproxyserver/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -46,6 +64,7 @@ void checkHiddenCommands() {
if (this.flags.isCreateTmpDir()) {
this.createTmpDirIfNotExists();
}
log.trace("status=checked");
}

Config findConfig(String[] args) {
Expand All @@ -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() {
Expand All @@ -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);
}
}
}
24 changes: 23 additions & 1 deletion src/test/java/com/mageddo/dnsproxyserver/AppCompTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Loading