-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppWideExceptionHandler.java
More file actions
57 lines (50 loc) · 2.4 KB
/
AppWideExceptionHandler.java
File metadata and controls
57 lines (50 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.embula.embula_backend.advisor;
import com.embula.embula_backend.exception.NotFoundException;
import com.embula.embula_backend.util.StandardResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class AppWideExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<StandardResponse> handleNotFoundException(NotFoundException e) {
return new ResponseEntity<>(
new StandardResponse(404, "Not Found", e.getMessage()),
HttpStatus.NOT_FOUND
);
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
org.springframework.http.HttpHeaders headers,
org.springframework.http.HttpStatusCode status,
WebRequest request) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<StandardResponse> handleGlobalException(Exception ex, WebRequest request) {
// Log the actual exception for debugging
System.err.println("=== Global Exception Handler ===");
System.err.println("Exception Type: " + ex.getClass().getName());
System.err.println("Exception Message: " + ex.getMessage());
ex.printStackTrace();
return new ResponseEntity<>(
new StandardResponse(500, "Internal Server Error", ex.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}