|
1 | 1 | package org.folio.rspec.controller.handler; |
2 | 2 |
|
3 | 3 | import static org.folio.rspec.domain.dto.ErrorCode.INVALID_QUERY_ENUM_VALUE; |
| 4 | +import static org.folio.rspec.domain.dto.ErrorCode.INVALID_QUERY_UUID_VALUE; |
4 | 5 |
|
5 | 6 | import java.util.Arrays; |
| 7 | +import java.util.UUID; |
6 | 8 | import java.util.regex.Pattern; |
7 | 9 | import lombok.RequiredArgsConstructor; |
8 | 10 | import org.folio.rspec.domain.dto.Error; |
@@ -30,32 +32,50 @@ public class MethodArgumentTypeMismatchExceptionHandler implements ServiceExcept |
30 | 32 | public ResponseEntity<ErrorCollection> handleException(Exception e) { |
31 | 33 | var exception = (MethodArgumentTypeMismatchException) e; |
32 | 34 | var requiredType = exception.getRequiredType(); |
33 | | - var errorCollection = new ErrorCollection(); |
34 | 35 |
|
35 | 36 | if (requiredType != null && requiredType.isEnum()) { |
36 | | - errorCollection.addErrorsItem(buildEnumError(exception, requiredType)); |
37 | | - return ResponseEntity.status(HttpStatus.UNPROCESSABLE_CONTENT).body(errorCollection); |
38 | | - } else { |
39 | | - errorCollection.addErrorsItem(buildUnexpectedError(exception)); |
40 | | - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorCollection); |
| 37 | + return buildResponse(HttpStatus.UNPROCESSABLE_CONTENT, buildEnumError(exception, requiredType)); |
41 | 38 | } |
| 39 | + if (requiredType != null && requiredType.isAssignableFrom(UUID.class)) { |
| 40 | + return buildResponse(HttpStatus.BAD_REQUEST, buildUuidError(exception)); |
| 41 | + } |
| 42 | + return buildResponse(HttpStatus.INTERNAL_SERVER_ERROR, buildUnexpectedError(exception)); |
42 | 43 | } |
43 | 44 |
|
44 | 45 | @Override |
45 | 46 | public boolean canHandle(Exception e) { |
46 | 47 | return e instanceof MethodArgumentTypeMismatchException; |
47 | 48 | } |
48 | 49 |
|
| 50 | + private ResponseEntity<ErrorCollection> buildResponse(HttpStatus status, Error error) { |
| 51 | + return ResponseEntity.status(status).body(new ErrorCollection().addErrorsItem(error)); |
| 52 | + } |
| 53 | + |
49 | 54 | private Error buildEnumError(MethodArgumentTypeMismatchException e, Class<?> requiredType) { |
50 | | - var message = buildErrorMessage(e, requiredType.getEnumConstants()); |
| 55 | + var message = buildEnumErrorMessage(e, requiredType.getEnumConstants()); |
51 | 56 | return new Error() |
52 | 57 | .message(message) |
53 | 58 | .code(INVALID_QUERY_ENUM_VALUE.getCode()) |
54 | 59 | .type(INVALID_QUERY_ENUM_VALUE.getType()) |
55 | 60 | .addParametersItem(new Parameter().key(e.getName()).value(String.valueOf(e.getValue()))); |
56 | 61 | } |
57 | 62 |
|
58 | | - private String buildErrorMessage(MethodArgumentTypeMismatchException e, Object[] enumConstants) { |
| 63 | + private Error buildUuidError(MethodArgumentTypeMismatchException e) { |
| 64 | + var inputValue = String.valueOf(e.getValue()); |
| 65 | + var message = buildUuidErrorMessage(inputValue); |
| 66 | + return new Error() |
| 67 | + .message(message) |
| 68 | + .code(INVALID_QUERY_UUID_VALUE.getCode()) |
| 69 | + .type(INVALID_QUERY_UUID_VALUE.getType()) |
| 70 | + .addParametersItem(new Parameter().key(e.getName()).value(inputValue)); |
| 71 | + } |
| 72 | + |
| 73 | + private String buildUuidErrorMessage(String inputValue) { |
| 74 | + return translationService.format(INVALID_QUERY_UUID_VALUE.getMessageKey(), |
| 75 | + INVALID_VALUE_MSG_ARG, inputValue); |
| 76 | + } |
| 77 | + |
| 78 | + private String buildEnumErrorMessage(MethodArgumentTypeMismatchException e, Object[] enumConstants) { |
59 | 79 | return translationService.format(INVALID_QUERY_ENUM_VALUE.getMessageKey(), |
60 | 80 | INVALID_VALUE_MSG_ARG, getInvalidValue(e.getRootCause()), |
61 | 81 | POSSIBLE_VALUES_MSG_ARG, translationService.formatList(Arrays.asList(enumConstants))); |
|
0 commit comments