Change errors formatter with field: error format

This commit is contained in:
Florian RICHER 2024-01-02 22:57:16 +01:00
parent 640d1313f9
commit 3fb4c227d6

View file

@ -16,13 +16,11 @@ import java.util.stream.Collectors
class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
override fun handleMethodArgumentNotValid(ex: MethodArgumentNotValidException, headers: HttpHeaders, status: HttpStatusCode, request: WebRequest): ResponseEntity<Any>? {
val body: MutableMap<String, List<String?>> = HashMap()
val body: MutableMap<String, Map<String, String?>> = HashMap()
val errors = ex.bindingResult
.fieldErrors
.stream()
.map { obj: FieldError -> obj.defaultMessage }
.collect(Collectors.toList())
.associate { obj: FieldError -> obj.field to obj.defaultMessage }
body["errors"] = errors