-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchAssignmentExceptionHandler.java
More file actions
59 lines (53 loc) · 2.58 KB
/
MatchAssignmentExceptionHandler.java
File metadata and controls
59 lines (53 loc) · 2.58 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cat.udl.eps.softarch.demo.exception;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.dao.PessimisticLockingFailureException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import cat.udl.eps.softarch.demo.api.dto.AssignmentErrorResponse;
import cat.udl.eps.softarch.demo.controller.MatchAssignmentController;
@RestControllerAdvice(assignableTypes = MatchAssignmentController.class)
public class MatchAssignmentExceptionHandler {
@ExceptionHandler(AssignmentValidationException.class)
public ResponseEntity<AssignmentErrorResponse> handleAssignmentValidationException(
AssignmentValidationException exception) {
HttpStatus status = mapStatus(exception.getErrorCode());
AssignmentErrorResponse response = new AssignmentErrorResponse(
exception.getErrorCode().name(),
exception.getMessage());
return ResponseEntity.status(status).body(response);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<AssignmentErrorResponse> handleMethodArgumentNotValidException(
MethodArgumentNotValidException exception) {
String message = exception.getBindingResult().getFieldErrors().stream()
.findFirst()
.map(error -> error.getDefaultMessage())
.orElse("Invalid request payload");
AssignmentErrorResponse response = new AssignmentErrorResponse(
AssignmentErrorCode.INVALID_ID_FORMAT.name(),
message);
return ResponseEntity.unprocessableEntity().body(response);
}
@ExceptionHandler({
OptimisticLockingFailureException.class,
PessimisticLockingFailureException.class,
CannotAcquireLockException.class
})
public ResponseEntity<AssignmentErrorResponse> handleConcurrencyException(RuntimeException exception) {
AssignmentErrorResponse response = new AssignmentErrorResponse(
AssignmentErrorCode.MATCH_ALREADY_HAS_REFEREE.name(),
"Concurrent assignment conflict detected");
return ResponseEntity.status(HttpStatus.CONFLICT).body(response);
}
private HttpStatus mapStatus(AssignmentErrorCode errorCode) {
return switch (errorCode) {
case MATCH_NOT_FOUND, REFEREE_NOT_FOUND -> HttpStatus.NOT_FOUND;
case MATCH_ALREADY_HAS_REFEREE, AVAILABILITY_CONFLICT -> HttpStatus.CONFLICT;
case INVALID_ROLE, INVALID_MATCH_STATE, INVALID_ID_FORMAT -> HttpStatus.UNPROCESSABLE_ENTITY;
};
}
}