|
| 1 | +package com.example.demo.exception; |
| 2 | + |
| 3 | +import com.example.demo.dto.ApiResponse; |
| 4 | +import org.springframework.http.HttpStatus; |
| 5 | +import org.springframework.http.ResponseEntity; |
| 6 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 7 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 8 | +import org.springframework.web.context.request.WebRequest; |
| 9 | +import org.springframework.web.servlet.NoHandlerFoundException; |
| 10 | + |
| 11 | +import java.time.LocalDateTime; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +@RestControllerAdvice |
| 16 | +public class GlobalExceptionHandler { |
| 17 | + |
| 18 | + @ExceptionHandler(NoHandlerFoundException.class) |
| 19 | + public ResponseEntity<ApiResponse<Object>> handleNotFound(NoHandlerFoundException ex, WebRequest request) { |
| 20 | + ApiResponse<Object> response = ApiResponse.builder() |
| 21 | + .message("Endpoint not found: " + ex.getRequestURL()) |
| 22 | + .timestamp(LocalDateTime.now()) |
| 23 | + .status(HttpStatus.NOT_FOUND.value()) |
| 24 | + .build(); |
| 25 | + |
| 26 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); |
| 27 | + } |
| 28 | + |
| 29 | + @ExceptionHandler(IllegalArgumentException.class) |
| 30 | + public ResponseEntity<ApiResponse<Object>> handleIllegalArgument(IllegalArgumentException ex) { |
| 31 | + ApiResponse<Object> response = ApiResponse.builder() |
| 32 | + .message("Invalid request: " + ex.getMessage()) |
| 33 | + .timestamp(LocalDateTime.now()) |
| 34 | + .status(HttpStatus.BAD_REQUEST.value()) |
| 35 | + .build(); |
| 36 | + |
| 37 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); |
| 38 | + } |
| 39 | + |
| 40 | + @ExceptionHandler(RuntimeException.class) |
| 41 | + public ResponseEntity<ApiResponse<Object>> handleRuntimeException(RuntimeException ex) { |
| 42 | + ApiResponse<Object> response = ApiResponse.builder() |
| 43 | + .message("Internal server error: " + ex.getMessage()) |
| 44 | + .timestamp(LocalDateTime.now()) |
| 45 | + .status(HttpStatus.INTERNAL_SERVER_ERROR.value()) |
| 46 | + .build(); |
| 47 | + |
| 48 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); |
| 49 | + } |
| 50 | + |
| 51 | + @ExceptionHandler(Exception.class) |
| 52 | + public ResponseEntity<ApiResponse<Object>> handleGenericException(Exception ex) { |
| 53 | + Map<String, Object> errorDetails = new HashMap<>(); |
| 54 | + errorDetails.put("exception", ex.getClass().getSimpleName()); |
| 55 | + errorDetails.put("cause", ex.getCause() != null ? ex.getCause().getMessage() : "Unknown"); |
| 56 | + |
| 57 | + ApiResponse<Object> response = ApiResponse.builder() |
| 58 | + .message("An unexpected error occurred: " + ex.getMessage()) |
| 59 | + .timestamp(LocalDateTime.now()) |
| 60 | + .status(HttpStatus.INTERNAL_SERVER_ERROR.value()) |
| 61 | + .data(errorDetails) |
| 62 | + .build(); |
| 63 | + |
| 64 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); |
| 65 | + } |
| 66 | + |
| 67 | + @ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) |
| 68 | + public ResponseEntity<ApiResponse<Object>> handleMethodNotSupported( |
| 69 | + org.springframework.web.HttpRequestMethodNotSupportedException ex) { |
| 70 | + |
| 71 | + Map<String, Object> errorDetails = new HashMap<>(); |
| 72 | + errorDetails.put("method", ex.getMethod()); |
| 73 | + errorDetails.put("supportedMethods", ex.getSupportedMethods()); |
| 74 | + |
| 75 | + ApiResponse<Object> response = ApiResponse.builder() |
| 76 | + .message("HTTP method not supported: " + ex.getMethod()) |
| 77 | + .timestamp(LocalDateTime.now()) |
| 78 | + .status(HttpStatus.METHOD_NOT_ALLOWED.value()) |
| 79 | + .data(errorDetails) |
| 80 | + .build(); |
| 81 | + |
| 82 | + return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(response); |
| 83 | + } |
| 84 | + |
| 85 | + @ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class) |
| 86 | + public ResponseEntity<ApiResponse<Object>> handleMissingParameter( |
| 87 | + org.springframework.web.bind.MissingServletRequestParameterException ex) { |
| 88 | + |
| 89 | + Map<String, Object> errorDetails = new HashMap<>(); |
| 90 | + errorDetails.put("parameterName", ex.getParameterName()); |
| 91 | + errorDetails.put("parameterType", ex.getParameterType()); |
| 92 | + |
| 93 | + ApiResponse<Object> response = ApiResponse.builder() |
| 94 | + .message("Missing required parameter: " + ex.getParameterName()) |
| 95 | + .timestamp(LocalDateTime.now()) |
| 96 | + .status(HttpStatus.BAD_REQUEST.value()) |
| 97 | + .data(errorDetails) |
| 98 | + .build(); |
| 99 | + |
| 100 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); |
| 101 | + } |
| 102 | + |
| 103 | + @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) |
| 104 | + public ResponseEntity<ApiResponse<Object>> handleValidationException( |
| 105 | + org.springframework.web.bind.MethodArgumentNotValidException ex) { |
| 106 | + |
| 107 | + Map<String, Object> errorDetails = new HashMap<>(); |
| 108 | + ex.getBindingResult().getFieldErrors() |
| 109 | + .forEach(error -> errorDetails.put(error.getField(), error.getDefaultMessage())); |
| 110 | + |
| 111 | + ApiResponse<Object> response = ApiResponse.builder() |
| 112 | + .message("Validation failed") |
| 113 | + .timestamp(LocalDateTime.now()) |
| 114 | + .status(HttpStatus.BAD_REQUEST.value()) |
| 115 | + .data(errorDetails) |
| 116 | + .build(); |
| 117 | + |
| 118 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response); |
| 119 | + } |
| 120 | +} |
0 commit comments