-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
83 lines (73 loc) · 3.86 KB
/
GlobalExceptionHandler.java
File metadata and controls
83 lines (73 loc) · 3.86 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
package com.swyth.emergencyservice.exception;
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.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.server.ServerWebExchange;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* GlobalExceptionHandler is a centralized exception handler class that provides
* consistent error responses for exceptions occurring throughout the application.
*
* This class uses the @ControllerAdvice annotation to enable global exception
* handling across the application's controllers. It contains methods annotated
* with @ExceptionHandler to handle specific exceptions and tailor the error
* responses accordingly.
*/
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* Handles WebExchangeBindException to provide detailed error responses for bad formatted requests.
*
* This method constructs a standardized error response containing a timestamp, HTTP status code,
* error type, request path, and validation errors. The validation errors include details about
* fields that failed validation along with their corresponding error messages.
*
* @param ex the WebExchangeBindException that occurred, containing details of the validation errors
* @param exchange the ServerWebExchange object representing the current web exchange, used to retrieve the request path
* @return a ResponseEntity containing the error response with validation error details and an HTTP status code of 400 (Bad Request)
*/
// Handle bad formatted requests
@ExceptionHandler(WebExchangeBindException.class)
public ResponseEntity<Map<String, Object>> handleWebExchangeBindException(WebExchangeBindException ex, ServerWebExchange exchange) {
Map<String, Object> response = new HashMap<>();
// Add basic information excluding "message" and "trace"
response.put("timestamp", LocalDateTime.now());
response.put("status", HttpStatus.BAD_REQUEST.value());
response.put("error", "Bad Request");
response.put("path", exchange.getRequest().getPath().toString());
// Add validation errors
Map<String, String> validationErrors = new HashMap<>();
ex.getFieldErrors().forEach(error -> {
validationErrors.put(error.getField(), error.getDefaultMessage());
});
response.put("errors", validationErrors);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
/**
* Handles the BedUnavailableException and constructs a custom error response.
*
* This method is triggered whenever a BedUnavailableException is thrown in the
* application. It returns a standardized error response with relevant details such as
* timestamp, HTTP status code, error type, and a descriptive message.
*
* @param exception the BedUnavailableException that occurred
* @return a ResponseEntity containing the error response with details and an HTTP status code of 404 (Not Found)
*/
// Handle BedUnavailableException
@ExceptionHandler(BedUnavailableException.class)
public ResponseEntity<Object> handleBedUnavailableException(BedUnavailableException exception) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.NOT_FOUND.value());
body.put("error", "Resource Not Found");
body.put("message", exception.getMessage());
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
}