Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit bc7190f

Browse files
dev/codeforces/ Добавил ControllerAdvice для Codeforces Exceptions
1 parent 2abd0bc commit bc7190f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

Comments
 (0)