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..d64c022 --- /dev/null +++ b/src/main/java/com/app/exception/GlobalExceptionHandler.java @@ -0,0 +1,46 @@ +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.time.LocalDateTime; +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("timestamp", LocalDateTime.now()); + response.put("status", ex.getHttpStatus().value()); + 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("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); + } +}