From 40fc1a1f13f344e1db31c20d17f12016c8239700 Mon Sep 17 00:00:00 2001 From: Giovanni Moratto Date: Tue, 3 Aug 2021 23:39:27 -0300 Subject: [PATCH] =?UTF-8?q?refatora=C3=A7=C3=A3o=20app=20e=20inclus=C3=A3o?= =?UTF-8?q?=20de=20exception=20para=20not=20readable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../carteira/CarteiraRequest.java | 3 +++ .../core/exceptions/ErroPadronizado.java | 3 +++ .../core/exceptions/ErroPadronizadoEnum.java | 23 +++++++++++++++++++ .../core/exceptions/HandlerAdvice.java | 23 ++++++++++++++++++- src/main/resources/messages.properties | 4 ++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizadoEnum.java diff --git a/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/carteira/CarteiraRequest.java b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/carteira/CarteiraRequest.java index 2dd27b48..4dc7fddf 100644 --- a/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/carteira/CarteiraRequest.java +++ b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/carteira/CarteiraRequest.java @@ -1,7 +1,9 @@ package br.com.zupacademy.giovannimoratto.desafioproposta.carteira; import br.com.zupacademy.giovannimoratto.desafioproposta.cartao.CartaoModel; +import com.fasterxml.jackson.annotation.JsonCreator; +import javax.validation.constraints.Email; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @@ -13,6 +15,7 @@ public class CarteiraRequest { /* Attributes */ @NotBlank + @Email private final String email; @NotNull private final CarteiraType carteira; diff --git a/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizado.java b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizado.java index eee9d2d0..08fa170e 100644 --- a/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizado.java +++ b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizado.java @@ -8,13 +8,16 @@ public class ErroPadronizado { + /* Attributes */ private final Collection mensagens; + /* Constructors */ public ErroPadronizado(Collection mensagens) { super(); this.mensagens = mensagens; } + /* Getters */ public Collection getMensagens() { return mensagens; } diff --git a/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizadoEnum.java b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizadoEnum.java new file mode 100644 index 00000000..e09cf2d0 --- /dev/null +++ b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/ErroPadronizadoEnum.java @@ -0,0 +1,23 @@ +package br.com.zupacademy.giovannimoratto.desafioproposta.core.exceptions; + +/** + * @Author giovanni.moratto + */ + +public class ErroPadronizadoEnum { + + /* Attributes */ + private final String mensagens; + + /* Constructors */ + public ErroPadronizadoEnum(String mensagens) { + super(); + this.mensagens = mensagens; + } + + /* Getters */ + public String getMensagens() { + return mensagens; + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/HandlerAdvice.java b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/HandlerAdvice.java index 009d75df..aa2fb612 100644 --- a/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/HandlerAdvice.java +++ b/src/main/java/br/com/zupacademy/giovannimoratto/desafioproposta/core/exceptions/HandlerAdvice.java @@ -1,10 +1,12 @@ package br.com.zupacademy.giovannimoratto.desafioproposta.core.exceptions; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -13,6 +15,7 @@ import org.springframework.web.server.ResponseStatusException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -32,7 +35,8 @@ public ResponseEntity handle(MethodArgumentNotValidException m BindingResult bindingResult = methodArgumentNotValidException.getBindingResult(); List fieldErrors = bindingResult.getFieldErrors(); fieldErrors.forEach(fieldError -> { - String message = String.format("Campo %s %s", fieldError.getField(), messageSource.getMessage(fieldError, + String message = String.format("Campo %s %s", fieldError.getField(), messageSource.getMessage( + fieldError, LocaleContextHolder.getLocale())); mensagens.add(message); }); @@ -50,4 +54,21 @@ public ResponseEntity handleResponseStatusException(ResponseSt return ResponseEntity.status(responseStatusException.getStatus()).body(erroPadronizado); } + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity handleHttpMessageNotReadable(HttpMessageNotReadableException exception) { + String genericMessage = "Unacceptable JSON " + exception.getMessage(); + String mensagens = genericMessage; + if (exception.getCause() instanceof InvalidFormatException) { + InvalidFormatException ifx = (InvalidFormatException) exception.getCause(); + if (ifx.getTargetType().isEnum()) { + mensagens = String.format("Valor: '%s' inválido para o campo: '%s'. O valor deve ser um dos " + + "seguintes: %s.", + ifx.getValue(), ifx.getPath().get(ifx.getPath().size() - 1).getFieldName(), + Arrays.toString(ifx.getTargetType().getEnumConstants())); + } + } + ErroPadronizadoEnum erroPadronizadoEnum = new ErroPadronizadoEnum(mensagens); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(erroPadronizadoEnum); + } + } diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 311d5b20..33ecf86f 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -14,3 +14,7 @@ Positive.propostaRequest.salario=deve ser maior que 0. # Biometria NotBlank.biometriaRequest.fingerprint=é obrigatória. Base64.biometriaRequest.fingerprint=deve possuir formato Base64. + +typeMismatch. +typeMismatch.java.math.BigDecimal = O valor não é um decimal válido. +typeMismatch.java.time.LocalDateTime = A data não veio formatada corretamente.