Skip to content

Commit

Permalink
[PLAT-15377] Adding a global uncaught exception handler to yugaware
Browse files Browse the repository at this point in the history
Summary: [PLAT-15377] Adding a global uncaught exception handler to yugaware

Test Plan: build locally, phabricator

Reviewers: sanketh, svarshney, nsingh

Reviewed By: svarshney, nsingh

Subscribers: yugaware

Differential Revision: https://phorge.dev.yugabyte.com/D38344
  • Loading branch information
amannijhawan committed Sep 26, 2024
1 parent f692a60 commit d6a19da
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
20 changes: 20 additions & 0 deletions managed/src/main/java/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Yugabyte, Inc.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GlobalExceptionHandler implements Thread.UncaughtExceptionHandler {

private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

// Added for test
public static void setLogger(Logger l) {
logger = l;
}

@Override
public void uncaughtException(Thread t, Throwable e) {
// Log the exception
logger.error("Yugaware uncaught exception in thread '{}'", t.getName(), e);
}
}
2 changes: 2 additions & 0 deletions managed/src/main/java/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public MainModule(Environment environment, Config config) {

@Override
public void configure() {
// Bind the uncaught exception handler at the application startup
Thread.setDefaultUncaughtExceptionHandler(new GlobalExceptionHandler());
bind(StaticInjectorHolder.class).asEagerSingleton();
bind(Long.class)
.annotatedWith(Names.named("AppStartupTimeMs"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,10 @@ private void waitForSubTasks(boolean abortOnFailure) {
anyEx = (anyEx != null) ? anyEx : thisEx;
runnableSubTask.updateTaskDetailsOnError(TaskInfo.State.Aborted, thisEx);
removeCompletedSubTask(iter, runnableSubTask, anyEx);
} catch (Exception e) {
anyEx = e;
runnableSubTask.updateTaskDetailsOnError(TaskInfo.State.Failure, e);
removeCompletedSubTask(iter, runnableSubTask, e);
} catch (Throwable th) {
anyEx = th;
runnableSubTask.updateTaskDetailsOnError(TaskInfo.State.Failure, th);
removeCompletedSubTask(iter, runnableSubTask, th);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions managed/src/test/java/GlobalExceptionHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Yugabyte, Inc.

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.junit.Test;
import org.slf4j.Logger;

public class GlobalExceptionHandlerTest {

@Test
public void testUncaughtExceptionLogsError() throws Exception {
Logger mockLogger = mock(Logger.class);

// Create an instance of the handler
GlobalExceptionHandler handler = new GlobalExceptionHandler();
GlobalExceptionHandler.setLogger(mockLogger);

Thread thread = new Thread(() -> {});
Throwable exception = new RuntimeException("Test exception");

handler.uncaughtException(thread, exception);

// Verify that the logger was called with the expected message
verify(mockLogger)
.error("Yugaware uncaught exception in thread '{}'", thread.getName(), exception);
}
}

0 comments on commit d6a19da

Please sign in to comment.