-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLAT-15377] Adding a global uncaught exception handler to yugaware
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
1 parent
f692a60
commit d6a19da
Showing
4 changed files
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |