|
1 | 1 | package com.cf.cfteam.advicers.codeforces; |
2 | 2 |
|
3 | | -import com.cf.cfteam.exceptions.codeforces.GroupNotFoundException; |
| 3 | +import com.cf.cfteam.exceptions.codeforces.*; |
4 | 4 | import com.cf.cfteam.utils.ErrorResponseBuilder; |
5 | 5 | import org.springframework.http.HttpStatus; |
6 | 6 | import org.springframework.http.ResponseEntity; |
7 | 7 | import org.springframework.web.bind.annotation.ControllerAdvice; |
8 | 8 | import org.springframework.web.bind.annotation.ExceptionHandler; |
9 | 9 |
|
| 10 | +import java.util.HashMap; |
10 | 11 | import java.util.Map; |
11 | 12 |
|
12 | 13 | @ControllerAdvice |
13 | 14 | public class CodeforcesExceptionHandler { |
14 | 15 | private static final String ID = "id"; |
| 16 | + private static final String LOGIN = "login"; |
| 17 | + private static final String TEAM_ID = "teamId"; |
| 18 | + private static final String PLAYER_ID = "playerId"; |
15 | 19 |
|
16 | 20 | @ExceptionHandler(GroupNotFoundException.class) |
17 | 21 | public ResponseEntity<Object> handleUserNotFoundException(GroupNotFoundException ex) { |
18 | 22 | return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(ID, ex.getId())); |
19 | 23 | } |
20 | 24 |
|
| 25 | + @ExceptionHandler(TeamNotFoundException.class) |
| 26 | + public ResponseEntity<Object> handleTeamNotFoundException(TeamNotFoundException ex) { |
| 27 | + return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(ID, ex.getId())); |
| 28 | + } |
| 29 | + |
| 30 | + @ExceptionHandler(PlayerNotFoundException.class) |
| 31 | + public ResponseEntity<Object> handlePayerNotFoundException(PlayerNotFoundException ex) { |
| 32 | + return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(ID, ex.getId())); |
| 33 | + } |
| 34 | + |
| 35 | + @ExceptionHandler(PlayerAlreadyInTeamException.class) |
| 36 | + public ResponseEntity<Object> handlePlayerAlreadyInTeamException(PlayerAlreadyInTeamException ex) { |
| 37 | + return ErrorResponseBuilder.buildErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND, Map.of(LOGIN, ex.getLogin())); |
| 38 | + } |
| 39 | + |
| 40 | + @ExceptionHandler(PlayerNotFromTeamException.class) |
| 41 | + public ResponseEntity<Object> handlePlayerNotFromTeamException(PlayerNotFromTeamException ex) { |
| 42 | + Map<String, Object> details = new HashMap<>(); |
| 43 | + if (ex.getPlayerId() != null) { |
| 44 | + details.put(PLAYER_ID, ex.getPlayerId()); |
| 45 | + } |
| 46 | + if (ex.getTeamId() != null) { |
| 47 | + details.put(TEAM_ID, ex.getTeamId()); |
| 48 | + } |
| 49 | + |
| 50 | + return ErrorResponseBuilder.buildErrorResponse( |
| 51 | + ex.getMessage(), |
| 52 | + HttpStatus.NOT_FOUND, |
| 53 | + details |
| 54 | + ); |
| 55 | + } |
21 | 56 | } |
0 commit comments