-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchAssignmentExceptionHandler.java
More file actions
43 lines (37 loc) · 1.66 KB
/
MatchAssignmentExceptionHandler.java
File metadata and controls
43 lines (37 loc) · 1.66 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
package cat.udl.eps.softarch.fll.exception;
import java.time.Instant;
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 jakarta.servlet.http.HttpServletRequest;
@RestControllerAdvice
public class MatchAssignmentExceptionHandler {
@ExceptionHandler(MatchAssignmentException.class)
public ResponseEntity<ErrorResponse> handleMatchAssignmentException(
MatchAssignmentException ex,
HttpServletRequest request) {
HttpStatus status = switch (ex.getErrorCode()) {
case ROUND_NOT_FOUND, MATCH_NOT_FOUND, REFEREE_NOT_FOUND -> HttpStatus.NOT_FOUND;
case AVAILABILITY_CONFLICT, MATCH_ALREADY_HAS_REFEREE, DUPLICATE_MATCH_IN_BATCH -> HttpStatus.CONFLICT;
case INVALID_ROLE, INVALID_MATCH_STATE, INVALID_ID_FORMAT -> HttpStatus.UNPROCESSABLE_CONTENT;
};
BatchErrorDetails details = ex.hasBatchDetails()
? new BatchErrorDetails(ex.getIndex(), ex.getMatchId(), ex.getRefereeId(), ex.getErrorCode().name())
: null;
ErrorResponse body = new ErrorResponse(
ex.hasBatchDetails() ? "BATCH_ASSIGNMENT_FAILED" : ex.getErrorCode().name(),
ex.hasBatchDetails() ? "Assignment failed at index " + ex.getIndex() : ex.getMessage(),
Instant.now().toString(),
request.getRequestURI(),
details);
return ResponseEntity.status(status).body(body);
}
public record ErrorResponse(
String error,
String message,
String timestamp,
String path,
BatchErrorDetails details) {}
public record BatchErrorDetails(Integer index, String matchId, String refereeId, String cause) {}
}