From b1f4e95235d63215e490023da73c1ec88c8b9f4d Mon Sep 17 00:00:00 2001 From: Faiz Akram <156657523+faizorg@users.noreply.github.com> Date: Sat, 1 Feb 2025 21:44:51 +0530 Subject: [PATCH 1/2] Maven updated --- .../com/app/controller/HomeController.java | 16 +++++++- .../com/app/exception/CustomException.java | 25 +++++++++++ .../app/exception/GlobalExceptionHandler.java | 41 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/app/exception/CustomException.java create mode 100644 src/main/java/com/app/exception/GlobalExceptionHandler.java diff --git a/src/main/java/com/app/controller/HomeController.java b/src/main/java/com/app/controller/HomeController.java index 035835b..74d6aa3 100644 --- a/src/main/java/com/app/controller/HomeController.java +++ b/src/main/java/com/app/controller/HomeController.java @@ -1,15 +1,29 @@ package com.app.controller; +import com.app.exception.CustomException; +import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController +@RequiredArgsConstructor public class HomeController { + @GetMapping - public ResponseEntity home(){ + public ResponseEntity home() { return new ResponseEntity<>("Running.....", HttpStatus.OK); } + + @GetMapping("exception") + public ResponseEntity exceptionTest(@RequestParam(required = false) Integer id) { + if (id == null) { + throw new CustomException("E0001", "Data Not found", HttpStatus.NOT_FOUND); + } + return new ResponseEntity<>("Running.....", HttpStatus.OK); + } + } diff --git a/src/main/java/com/app/exception/CustomException.java b/src/main/java/com/app/exception/CustomException.java new file mode 100644 index 0000000..da50d39 --- /dev/null +++ b/src/main/java/com/app/exception/CustomException.java @@ -0,0 +1,25 @@ +package com.app.exception; + +import lombok.Getter; +import org.springframework.http.HttpStatus; +@Getter +public class CustomException extends RuntimeException { + + private final String errorCode; // Custom error code (e.g., "E1001") + private final String errorMessage; // Detailed error message + private final HttpStatus httpStatus; // Associated HTTP status code + + /** + * Constructor for CustomException. + * + * @param errorCode The custom error code. + * @param errorMessage The error message. + * @param httpStatus The HTTP status associated with the error. + */ + public CustomException(String errorCode, String errorMessage, HttpStatus httpStatus) { + super(errorMessage); // Pass the message to the superclass (RuntimeException) + this.errorCode = errorCode; + this.errorMessage = errorMessage; + this.httpStatus = httpStatus; + } +} diff --git a/src/main/java/com/app/exception/GlobalExceptionHandler.java b/src/main/java/com/app/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..1b70397 --- /dev/null +++ b/src/main/java/com/app/exception/GlobalExceptionHandler.java @@ -0,0 +1,41 @@ +package com.app.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.HashMap; +import java.util.Map; + +@RestControllerAdvice +public class GlobalExceptionHandler { + /** + * Handles custom exceptions with error codes and messages. + * + * @param ex The CustomException to handle. + * @return A ResponseEntity with structured error response. + */ + @ExceptionHandler(CustomException.class) + public ResponseEntity handleCustomException(CustomException ex) { + Map response = new HashMap<>(); + response.put("errorCode", ex.getErrorCode()); + response.put("message", ex.getErrorMessage()); + + return new ResponseEntity<>(response, ex.getHttpStatus()); + } + + /** + * Handles all other unexpected exceptions. + * + * @param ex The generic Exception to handle. + * @return A ResponseEntity with error details. + */ + @ExceptionHandler(Exception.class) + public ResponseEntity handleGenericException(Exception ex) { + Map response = new HashMap<>(); + response.put("errorCode", "E5000"); + response.put("message", "An unexpected error occurred: " + ex.getMessage()); + return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); + } +} From b7fe93dbd860fafe50d600ff7d7936caf82d4808 Mon Sep 17 00:00:00 2001 From: Faiz Akram <156657523+faizorg@users.noreply.github.com> Date: Sat, 1 Feb 2025 21:49:29 +0530 Subject: [PATCH 2/2] Updated --- src/main/java/com/app/exception/GlobalExceptionHandler.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/app/exception/GlobalExceptionHandler.java b/src/main/java/com/app/exception/GlobalExceptionHandler.java index 1b70397..d64c022 100644 --- a/src/main/java/com/app/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/app/exception/GlobalExceptionHandler.java @@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; @@ -19,6 +20,8 @@ public class GlobalExceptionHandler { @ExceptionHandler(CustomException.class) public ResponseEntity handleCustomException(CustomException ex) { Map response = new HashMap<>(); + response.put("timestamp", LocalDateTime.now()); + response.put("status", ex.getHttpStatus().value()); response.put("errorCode", ex.getErrorCode()); response.put("message", ex.getErrorMessage()); @@ -34,6 +37,8 @@ public ResponseEntity handleCustomException(CustomException ex) { @ExceptionHandler(Exception.class) public ResponseEntity handleGenericException(Exception ex) { Map response = new HashMap<>(); + response.put("timestamp", LocalDateTime.now()); + response.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value()); response.put("errorCode", "E5000"); response.put("message", "An unexpected error occurred: " + ex.getMessage()); return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);