-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
97 lines (88 loc) · 4.75 KB
/
Copy pathGlobalExceptionHandler.java
File metadata and controls
97 lines (88 loc) · 4.75 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ch.fhnw.timerecordingbackend.excpetion;
import jakarta.validation.ValidationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
Map<String, Object> body = new HashMap<>();
List<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
.map(error -> error.getField() + ": " + error.getDefaultMessage())
.collect(Collectors.toList());
body.put("timestamp", System.currentTimeMillis());
body.put("status", status.value());
body.put("errors", errors);
body.put("message", "Validation failed for arguments");
return new ResponseEntity<>(body, headers, status);
}
@ExceptionHandler(ValidationException.class)
public ResponseEntity<Object> handleValidationException(ValidationException ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", System.currentTimeMillis());
body.put("status", HttpStatus.BAD_REQUEST.value());
body.put("error", "Bad Request");
body.put("message", ex.getMessage()); // Message from the service layer
body.put("path", request.getDescription(false).substring(4)); // Extract URI
return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Object> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", System.currentTimeMillis());
body.put("status", HttpStatus.BAD_REQUEST.value());
body.put("error", "Bad Request");
body.put("message", ex.getMessage());
body.put("path", request.getDescription(false).substring(4));
return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<Object> handleResponseStatusException(ResponseStatusException ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", System.currentTimeMillis());
body.put("status", ex.getStatusCode().value());
body.put("error", HttpStatus.resolve(ex.getStatusCode().value()).getReasonPhrase());
body.put("message", ex.getReason()); // Use getReason() for the message
body.put("path", request.getDescription(false).substring(4));
return new ResponseEntity<>(body, ex.getStatusCode());
}
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<Object> handleAccessDeniedException(AccessDeniedException ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", System.currentTimeMillis());
body.put("status", HttpStatus.FORBIDDEN.value());
body.put("error", "Forbidden");
body.put("message", ex.getMessage());
body.put("path", request.getDescription(false).substring(4));
return new ResponseEntity<>(body, HttpStatus.FORBIDDEN);
}
// Fallback for other exceptions
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleAllOtherExceptions(Exception ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", System.currentTimeMillis());
body.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
body.put("error", "Internal Server Error");
body.put("message", "An unexpected error occurred: " + ex.getMessage());
body.put("path", request.getDescription(false).substring(4));
// Log the exception for server-side analysis
// logger.error("Unexpected error occurred", ex);
return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}