|
| 1 | +package com.cf.cfteam.advicers.codeforces; |
| 2 | + |
| 3 | +import com.cf.cfteam.exceptions.codeforces.GroupNotFoundException; |
| 4 | +import com.cf.cfteam.exceptions.security.UserNotFoundException; |
| 5 | +import jakarta.validation.GroupDefinitionException; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
| 9 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 10 | + |
| 11 | +import java.time.LocalDateTime; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +@ControllerAdvice |
| 16 | +public class CodeforcesExceptionHandler { |
| 17 | + private static final String ID = "id"; |
| 18 | + |
| 19 | + @ExceptionHandler(GroupNotFoundException.class) |
| 20 | + public ResponseEntity<Object> handleUserNotFoundException(GroupNotFoundException ex) { |
| 21 | + return buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(ID, ex.getId())); |
| 22 | + } |
| 23 | + |
| 24 | + private ResponseEntity<Object> buildErrorResponse(String message, HttpStatus status, Map<String, Object> details) { |
| 25 | + Map<String, Object> errorResponse = new HashMap<>(); |
| 26 | + errorResponse.put("timestamp", LocalDateTime.now()); |
| 27 | + errorResponse.put("status", status.value()); |
| 28 | + errorResponse.put("error", status.getReasonPhrase()); |
| 29 | + errorResponse.put("message", message); |
| 30 | + |
| 31 | + if (details != null) { |
| 32 | + errorResponse.put("details", details); |
| 33 | + } |
| 34 | + |
| 35 | + return new ResponseEntity<>(errorResponse, status); |
| 36 | + } |
| 37 | +} |
0 commit comments