validops-east-1 commited on
Commit
7c2e593
·
1 Parent(s): 1551ac4
src/main/java/com/erp/system/controller/GlobalExceptionHandler.java CHANGED
@@ -1,10 +1,18 @@
1
  package com.erp.system.controller;
2
 
 
3
  import lombok.extern.slf4j.Slf4j;
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
 
9
  import java.time.LocalDateTime;
10
  import java.util.Map;
@@ -13,15 +21,43 @@ import java.util.Map;
13
  @RestControllerAdvice
14
  public class GlobalExceptionHandler {
15
 
16
- @ExceptionHandler(IllegalArgumentException.class)
17
- public ResponseEntity<Map<String, Object>> handleIllegalArgument(IllegalArgumentException ex) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  log.warn("Bad request: {}", ex.getMessage());
19
  return ResponseEntity.badRequest().body(errorBody(ex.getMessage(), HttpStatus.BAD_REQUEST));
20
  }
21
 
22
  @ExceptionHandler(Exception.class)
23
- public ResponseEntity<Map<String, Object>> handleGeneric(Exception ex) {
24
- log.error("Unhandled exception: {}", ex.getMessage(), ex);
25
  return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
26
  .body(errorBody("Internal server error", HttpStatus.INTERNAL_SERVER_ERROR));
27
  }
 
1
  package com.erp.system.controller;
2
 
3
+ import jakarta.servlet.http.HttpServletRequest;
4
  import lombok.extern.slf4j.Slf4j;
5
  import org.springframework.http.HttpStatus;
6
  import org.springframework.http.ResponseEntity;
7
+ import org.springframework.http.converter.HttpMessageNotReadableException;
8
+ import org.springframework.web.HttpMediaTypeNotAcceptableException;
9
+ import org.springframework.web.HttpRequestMethodNotSupportedException;
10
+ import org.springframework.web.bind.MethodArgumentNotValidException;
11
+ import org.springframework.web.bind.MissingServletRequestParameterException;
12
  import org.springframework.web.bind.annotation.ExceptionHandler;
13
  import org.springframework.web.bind.annotation.RestControllerAdvice;
14
+ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
15
+ import org.springframework.web.servlet.resource.NoResourceFoundException;
16
 
17
  import java.time.LocalDateTime;
18
  import java.util.Map;
 
21
  @RestControllerAdvice
22
  public class GlobalExceptionHandler {
23
 
24
+ @ExceptionHandler(NoResourceFoundException.class)
25
+ public ResponseEntity<Map<String, Object>> handleNotFound(NoResourceFoundException ex, HttpServletRequest request) {
26
+ log.debug("Resource not found: {} {}", request.getMethod(), request.getRequestURI());
27
+ return ResponseEntity.status(HttpStatus.NOT_FOUND)
28
+ .body(errorBody("Resource not found", HttpStatus.NOT_FOUND));
29
+ }
30
+
31
+ @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
32
+ public ResponseEntity<Map<String, Object>> handleMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex) {
33
+ log.warn("Media type not acceptable: {}", ex.getMessage());
34
+ return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE)
35
+ .body(errorBody("Not acceptable", HttpStatus.NOT_ACCEPTABLE));
36
+ }
37
+
38
+ @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
39
+ public ResponseEntity<Map<String, Object>> handleMethodNotAllowed(HttpRequestMethodNotSupportedException ex) {
40
+ log.warn("Method not allowed: {}", ex.getMessage());
41
+ return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
42
+ .body(errorBody("Method not allowed", HttpStatus.METHOD_NOT_ALLOWED));
43
+ }
44
+
45
+ @ExceptionHandler(MethodArgumentNotValidException.class)
46
+ public ResponseEntity<Map<String, Object>> handleValidation(MethodArgumentNotValidException ex) {
47
+ log.warn("Validation failed: {}", ex.getMessage());
48
+ return ResponseEntity.badRequest().body(errorBody("Validation failed", HttpStatus.BAD_REQUEST));
49
+ }
50
+
51
+ @ExceptionHandler({MissingServletRequestParameterException.class, MethodArgumentTypeMismatchException.class,
52
+ HttpMessageNotReadableException.class, IllegalArgumentException.class})
53
+ public ResponseEntity<Map<String, Object>> handleBadRequest(Exception ex) {
54
  log.warn("Bad request: {}", ex.getMessage());
55
  return ResponseEntity.badRequest().body(errorBody(ex.getMessage(), HttpStatus.BAD_REQUEST));
56
  }
57
 
58
  @ExceptionHandler(Exception.class)
59
+ public ResponseEntity<Map<String, Object>> handleGeneric(Exception ex, HttpServletRequest request) {
60
+ log.error("Unhandled exception at {} {}: {}", request.getMethod(), request.getRequestURI(), ex.getMessage(), ex);
61
  return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
62
  .body(errorBody("Internal server error", HttpStatus.INTERNAL_SERVER_ERROR));
63
  }
target/classes/com/erp/system/controller/GlobalExceptionHandler.class CHANGED
Binary files a/target/classes/com/erp/system/controller/GlobalExceptionHandler.class and b/target/classes/com/erp/system/controller/GlobalExceptionHandler.class differ
 
target/classes/org/springframework/web/bind/annotation/ControllerAdvice.class ADDED
Binary file (1.03 kB). View file
 
target/classes/org/springframework/web/bind/annotation/ResponseBody.class ADDED
Binary file (469 Bytes). View file
 
target/classes/org/springframework/web/bind/annotation/RestControllerAdvice.class ADDED
Binary file (1.15 kB). View file