Skip to content

Commit

Permalink
refatoração app e inclusão de exception para not readable
Browse files Browse the repository at this point in the history
  • Loading branch information
giovnnvm committed Aug 4, 2021
1 parent 230a152 commit 40fc1a1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,6 +15,7 @@ public class CarteiraRequest {

/* Attributes */
@NotBlank
@Email
private final String email;
@NotNull
private final CarteiraType carteira;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

public class ErroPadronizado {

/* Attributes */
private final Collection <String> mensagens;

/* Constructors */
public ErroPadronizado(Collection <String> mensagens) {
super();
this.mensagens = mensagens;
}

/* Getters */
public Collection <String> getMensagens() {
return mensagens;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -32,7 +35,8 @@ public ResponseEntity <ErroPadronizado> handle(MethodArgumentNotValidException m
BindingResult bindingResult = methodArgumentNotValidException.getBindingResult();
List <FieldError> 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);
});
Expand All @@ -50,4 +54,21 @@ public ResponseEntity <ErroPadronizado> handleResponseStatusException(ResponseSt
return ResponseEntity.status(responseStatusException.getStatus()).body(erroPadronizado);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity <ErroPadronizadoEnum> 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);
}

}
4 changes: 4 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 40fc1a1

Please sign in to comment.