diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/controller/ApiExceptionHandler.java b/server/src/main/java/com/usatiuk/tjv/y/server/controller/ApiExceptionHandler.java index de1c400..a6c874f 100644 --- a/server/src/main/java/com/usatiuk/tjv/y/server/controller/ApiExceptionHandler.java +++ b/server/src/main/java/com/usatiuk/tjv/y/server/controller/ApiExceptionHandler.java @@ -47,9 +47,13 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(DataIntegrityViolationException.class) protected ResponseEntity handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) { - return handleExceptionInternal(ex, - new ErrorTo(List.of("Something is wrong with your request"), HttpStatus.BAD_REQUEST.value()), - new HttpHeaders(), HttpStatus.BAD_REQUEST, request); + if (ex.getRootCause() instanceof ConstraintViolationException) { + return handleConstraintViolation((ConstraintViolationException) ex.getRootCause(), request); + } else { + return handleExceptionInternal(ex, + new ErrorTo(List.of("Something is wrong with your request"), HttpStatus.BAD_REQUEST.value()), + new HttpHeaders(), HttpStatus.BAD_REQUEST, request); + } } @ExceptionHandler(AuthenticationException.class) diff --git a/server/src/main/java/com/usatiuk/tjv/y/server/service/ChatServiceImpl.java b/server/src/main/java/com/usatiuk/tjv/y/server/service/ChatServiceImpl.java index f505020..7f2336c 100644 --- a/server/src/main/java/com/usatiuk/tjv/y/server/service/ChatServiceImpl.java +++ b/server/src/main/java/com/usatiuk/tjv/y/server/service/ChatServiceImpl.java @@ -48,7 +48,11 @@ public class ChatServiceImpl implements ChatService { chat.setCreator(entityManager.getReference(Person.class, authentication.getName())); chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map( - p -> entityManager.getReference(Person.class, p) + p -> { + if (entityManager.find(Person.class, p) == null) + throw new NotFoundException("User with uuid " + p + " not found"); + return entityManager.getReference(Person.class, p); + } ).toList()); chat.setName(chatCreateTo.name()); @@ -67,7 +71,11 @@ public class ChatServiceImpl implements ChatService { throw new BadInputException("Chat must have members other than its creator"); chat.setMembers(new ArrayList<>(Arrays.stream(chatCreateTo.memberUuids()).map( - p -> entityManager.getReference(Person.class, p) + p -> { + if (entityManager.find(Person.class, p) == null) + throw new NotFoundException("User with uuid " + p + " not found"); + return entityManager.getReference(Person.class, p); + } ).toList())); chat.setName(chatCreateTo.name()); diff --git a/server/src/test/java/com/usatiuk/tjv/y/server/controller/ChatControllerTest.java b/server/src/test/java/com/usatiuk/tjv/y/server/controller/ChatControllerTest.java index dc9011a..11d05ed 100644 --- a/server/src/test/java/com/usatiuk/tjv/y/server/controller/ChatControllerTest.java +++ b/server/src/test/java/com/usatiuk/tjv/y/server/controller/ChatControllerTest.java @@ -44,6 +44,19 @@ public class ChatControllerTest extends DemoDataDbTest { Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent()); } + @Test + void chatCreateWithBadUuid404() { + var response = restTemplate.exchange(addr + "/chat", HttpMethod.POST, + new HttpEntity<>(new ChatCreateTo("chatnew", new String[]{person1.getUuid(), "asdfasdfasdf"}), createAuthHeaders(person1Auth)), + ErrorTo.class); + + Assertions.assertNotNull(response); + Assertions.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + + var toResponse = response.getBody(); + Assertions.assertNotNull(toResponse); + } + @Test void shouldGetChat() { for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) {