-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleExceptionHandler.java
More file actions
23 lines (20 loc) · 1018 Bytes
/
ArticleExceptionHandler.java
File metadata and controls
23 lines (20 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.javaspringcourse.exception;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.stream.Collectors;
@RestControllerAdvice
public class ArticleExceptionHandler {
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleBadGatewayException(ConstraintViolationException ex) {
return new ErrorResponse("Неверно заполнены поля.",
ex.getConstraintViolations().stream()
.collect(Collectors.toMap(
violation -> violation.getPropertyPath().toString(),
ConstraintViolation::getMessage)));
}
}