-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(core): basic spring hawk #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package so.java.hawk.catcher; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
/** | ||
* HawkCatcher Class | ||
* | ||
* Provides methods for global error handling, logging errors and warnings, and reporting status. | ||
*/ | ||
@Component | ||
public class HawkCatcher { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(HawkCatcher.class); | ||
private static int errorCount = 0; | ||
private static int warningCount = 0; | ||
Comment on lines
+18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why these increments are needed? I'd suggest to remove all unused code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, please, do not forget to add docs to each class property |
||
|
||
/** | ||
* Initializes the global error handler and logging mechanisms. | ||
* Sets up uncaught exception handler for the entire JVM. | ||
*/ | ||
@PostConstruct | ||
public void initialize() { | ||
Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we re-use hawk.java here? |
||
logError("Uncaught exception in thread " + thread.getName() + ": " + exception.getMessage()); | ||
}); | ||
} | ||
|
||
/** | ||
* Logs error messages to the console and increments the error counter. | ||
* | ||
* @param message The error message to log. | ||
*/ | ||
public static void logError(String message) { | ||
errorCount++; | ||
logger.error("Error: " + message); | ||
} | ||
|
||
/** | ||
* Logs warning messages to the console and increments the warning counter. | ||
* | ||
* @param message The warning message to log. | ||
*/ | ||
public static void logWarning(String message) { | ||
warningCount++; | ||
logger.warn("Warning: " + message); | ||
} | ||
|
||
/** | ||
* Reports the current status of errors and warnings. | ||
*/ | ||
public static void reportStatus() { | ||
String statusMessage = String.format("Current Status:%nErrors: %d%nWarnings: %d", errorCount, warningCount); | ||
logger.info(statusMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package so.java.hawk.catcher; | ||
|
||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
|
||
/** | ||
* PlaygroundApp Class | ||
* <p> | ||
* The main application class for running HawkCatcher example. | ||
* Initializes the HawkCatcher and demonstrates logging errors, warnings, and reporting status. | ||
*/ | ||
@SpringBootApplication | ||
@ComponentScan(basePackages = {"so.java.hawk.catcher", "so.java.hawk"}) // Add the packages to scan | ||
public class PlaygroundApp { | ||
|
||
private final HawkCatcher hawkCatcher; | ||
|
||
/** | ||
* Constructor for PlaygroundApp. | ||
* | ||
* @param hawkCatcher HawkCatcher instance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from where app entry point should receive hawk instance? |
||
*/ | ||
public PlaygroundApp(HawkCatcher hawkCatcher) { | ||
this.hawkCatcher = hawkCatcher; | ||
} | ||
|
||
/** | ||
* Main method to run the application. | ||
* | ||
* @param args Command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
SpringApplication.run(PlaygroundApp.class, args); | ||
} | ||
|
||
/** | ||
* CommandLineRunner bean to execute actions after application startup. | ||
* Demonstrates logging warnings, handling exceptions, and reporting status. | ||
* | ||
* @return CommandLineRunner instance | ||
*/ | ||
@Bean | ||
CommandLineRunner run() { | ||
return args -> { | ||
// Log a warning manually | ||
hawkCatcher.logWarning("This is a test warning."); | ||
|
||
// Run a critical operation and handle any exceptions | ||
try { | ||
performCriticalOperation(); | ||
} catch (Exception e) { | ||
hawkCatcher.logError("Caught an exception in critical operation: " + e.getMessage()); | ||
} | ||
|
||
// Report current error status to check if there were issues | ||
hawkCatcher.reportStatus(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unexpected method. Let's start with simple global exception handling. |
||
}; | ||
} | ||
|
||
/** | ||
* Perform a critical operation (example: division by zero). | ||
* This will cause an exception to demonstrate error handling. | ||
*/ | ||
private void performCriticalOperation() { | ||
// Example error: division by zero | ||
int result = 10 / 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package so.java.hawk.handler; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import so.java.hawk.catcher.HawkCatcher; | ||
|
||
/** | ||
* GlobalExceptionHandler Class | ||
* | ||
* Handles global exceptions for the application. It catches any unhandled exceptions, | ||
* logs them using HawkCatcher, and returns a generic error response. | ||
*/ | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
private final HawkCatcher hawkCatcher; | ||
|
||
/** | ||
* Constructor for GlobalExceptionHandler. | ||
* | ||
* @param hawkCatcher HawkCatcher instance | ||
*/ | ||
public GlobalExceptionHandler(HawkCatcher hawkCatcher) { | ||
this.hawkCatcher = hawkCatcher; | ||
} | ||
|
||
/** | ||
* Global exception handler for any Exception thrown within the application. | ||
* Logs the exception and sends an HTTP 500 response with the error message. | ||
* | ||
* @param exception The exception that was thrown | ||
* @return A ResponseEntity containing the error message and HTTP status | ||
*/ | ||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<String> handleException(Exception exception) { | ||
hawkCatcher.logError("Handled exception: " + exception.getMessage()); | ||
return new ResponseEntity<>("An error occurred: " + exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.application.name=springHawk |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.springhawk; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class SpringHawkApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why hawk.java package is removed?