From ae78e6d5fe590ac6e2a1e095d0d25122dbcc6bb8 Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Thu, 22 Aug 2024 13:47:33 -0600 Subject: [PATCH] improve Error Handling on BlockNodeApp, webServer Start, added a new UnhandledIOException that is a runtime wrapper for the checked IOException Signed-off-by: Alfredo Gutierrez --- .../java/com/hedera/block/server/Server.java | 8 ++++--- .../exception/UnhandledIOException.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 server/src/main/java/com/hedera/block/server/exception/UnhandledIOException.java diff --git a/server/src/main/java/com/hedera/block/server/Server.java b/server/src/main/java/com/hedera/block/server/Server.java index cd9e07c9..ee11cd39 100644 --- a/server/src/main/java/com/hedera/block/server/Server.java +++ b/server/src/main/java/com/hedera/block/server/Server.java @@ -16,6 +16,7 @@ package com.hedera.block.server; +import com.hedera.block.server.exception.UnhandledIOException; import edu.umd.cs.findbugs.annotations.NonNull; import java.io.IOException; @@ -38,12 +39,13 @@ public static void main(final String[] args) { @NonNull final BlockNodeAppInjectionComponent daggerComponent = DaggerBlockNodeAppInjectionComponent.create(); - @NonNull final BlockNodeApp server = daggerComponent.getBlockNodeApp(); + @NonNull final BlockNodeApp blockNodeApp = daggerComponent.getBlockNodeApp(); try { - server.startServer(); + blockNodeApp.startServer(); } catch (IOException e) { - throw new RuntimeException(e); + LOGGER.log((System.Logger.Level.ERROR), "Failed to start server", e); + throw new UnhandledIOException(e); } } } diff --git a/server/src/main/java/com/hedera/block/server/exception/UnhandledIOException.java b/server/src/main/java/com/hedera/block/server/exception/UnhandledIOException.java new file mode 100644 index 00000000..b1e5daee --- /dev/null +++ b/server/src/main/java/com/hedera/block/server/exception/UnhandledIOException.java @@ -0,0 +1,22 @@ +package com.hedera.block.server.exception; + +import java.io.Serial; + +/** + * UnhandledIOException is a RuntimeException that wraps an IOException. + * This can be used to rethrow checked IOExceptions as unchecked exceptions. + */ +public class UnhandledIOException extends RuntimeException { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * Constructs a new UnhandledIOException with the specified cause. + * + * @param cause the underlying IOException that caused this exception + */ + public UnhandledIOException(final Throwable cause) { + super(cause); + } +}