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

feat(core): basic spring hawk #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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

This file was deleted.

39 changes: 0 additions & 39 deletions hawk-java/src/main/java/org/catcher/Hawk.java
Copy link
Member

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?

This file was deleted.

56 changes: 0 additions & 56 deletions hawk-java/src/main/java/org/playground/PlaygroundApp.java

This file was deleted.

59 changes: 59 additions & 0 deletions hawk-spring/src/main/java/so/java/hawk/catcher/HawkCatcher.java
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
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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) -> {
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
71 changes: 71 additions & 0 deletions hawk-spring/src/main/java/so/java/hawk/catcher/PlaygroundApp.java
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
Copy link
Member

Choose a reason for hiding this comment

The 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();
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
1 change: 1 addition & 0 deletions hawk-spring/src/main/resources/application.properties
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() {
}

}