Skip to content

Commit

Permalink
get error message from violation
Browse files Browse the repository at this point in the history
  • Loading branch information
ce-pong committed Feb 26, 2024
1 parent 8567b83 commit 99a1b3a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.context.request.WebRequest;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


Expand All @@ -32,12 +33,16 @@ public ApiErrorResponse handleInternalServerException(InternalServerException in
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ApiErrorResponse handleValidationExceptions(MethodArgumentNotValidException exception, WebRequest request) {
List<String> error = exception.getFieldErrors()
.stream()
.map(f -> f.getField() + " " + f.getDefaultMessage())
.toList();

return new ApiErrorResponse(
LocalDateTime.now(),
HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.getReasonPhrase(),
"Validation error",
String.join(",",error),
request.getDescription(false)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void createTicket_withInvalidData_ShouldReturn400() throws Exception {
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.message").value("Validation error"));
.andExpect(jsonPath("$.message").isNotEmpty());
}


Expand Down

0 comments on commit 99a1b3a

Please sign in to comment.