Skip to content
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
16 changes: 15 additions & 1 deletion src/main/java/com/app/controller/HomeController.java
Original file line number Diff line number Diff line change
@@ -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<String> home(){
public ResponseEntity<String> home() {
return new ResponseEntity<>("Running.....", HttpStatus.OK);
}

@GetMapping("exception")
public ResponseEntity<String> 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);
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/app/exception/CustomException.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/app/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -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<Object> handleCustomException(CustomException ex) {
Map<String, Object> 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<Object> handleGenericException(Exception ex) {
Map<String, Object> 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);
}
}