Spaces:
Sleeping
Sleeping
File size: 2,977 Bytes
1c8e50c |
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 |
package in.garvit.tasks.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
/**
* Global Exception Handler for Task Service
* Handles all exceptions thrown by controllers and services
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* Handle generic exceptions
* @param ex the exception
* @param request the web request
* @return error response
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, Object>> handleGlobalException(
Exception ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("message", ex.getMessage());
body.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
body.put("error", "Internal Server Error");
return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
/**
* Handle TaskNotFoundException
* @param ex the exception
* @param request the web request
* @return error response
*/
@ExceptionHandler(TaskNotFoundException.class)
public ResponseEntity<Map<String, Object>> handleTaskNotFoundException(
TaskNotFoundException ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("message", ex.getMessage());
body.put("status", HttpStatus.NOT_FOUND.value());
body.put("error", "Task Not Found");
return new ResponseEntity<>(body, HttpStatus.NOT_FOUND);
}
/**
* Handle IllegalArgumentException
* @param ex the exception
* @param request the web request
* @return error response
*/
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Map<String, Object>> handleIllegalArgumentException(
IllegalArgumentException ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("message", ex.getMessage());
body.put("status", HttpStatus.BAD_REQUEST.value());
body.put("error", "Bad Request");
return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
}
/**
* Handle UnauthorizedActionException
* @param ex the exception
* @param request the web request
* @return error response
*/
@ExceptionHandler(UnauthorizedActionException.class)
public ResponseEntity<Map<String, Object>> handleUnauthorizedActionException(
UnauthorizedActionException ex, WebRequest request) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("message", ex.getMessage());
body.put("status", HttpStatus.FORBIDDEN.value());
body.put("error", "Forbidden");
return new ResponseEntity<>(body, HttpStatus.FORBIDDEN);
}
}
|