From 38396be256f063c852ff2c96f0beb0ba20987f79 Mon Sep 17 00:00:00 2001 From: Jonas Souza Date: Tue, 21 May 2024 22:43:16 -0300 Subject: [PATCH] refactor: custom validation response --- .../in/rest/exceptions/FieldMessage.java | 13 ++++++++++++ .../exceptions/ResourceExceptionHandler.java | 20 +++++++++++++++++++ .../in/rest/exceptions/ValidationError.java | 14 +++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/FieldMessage.java create mode 100644 src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ValidationError.java diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/FieldMessage.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/FieldMessage.java new file mode 100644 index 0000000..6c10673 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/FieldMessage.java @@ -0,0 +1,13 @@ +package br.com.fiap.grupo30.fastfood.adapters.in.rest.exceptions; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class FieldMessage { + private String fieldName; + private String message; +} diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java index 4a5a5bd..2b27d98 100644 --- a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ResourceExceptionHandler.java @@ -5,6 +5,8 @@ import java.time.Instant; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -74,4 +76,22 @@ public ResponseEntity userCantChangeOrderAfterSubmit( err.setPath(request.getRequestURI()); return ResponseEntity.status(status).body(err); } + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity validation( + MethodArgumentNotValidException e, HttpServletRequest request) { + HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY; + ValidationError err = new ValidationError(); + err.setTimestamp(Instant.now()); + err.setStatus(status.value()); + err.setError("Validation exception"); + err.setMessage(e.getMessage()); + err.setPath(request.getRequestURI()); + + for (FieldError f : e.getBindingResult().getFieldErrors()) { + err.addError(f.getField(), f.getDefaultMessage()); + } + + return ResponseEntity.status(status).body(err); + } } diff --git a/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ValidationError.java b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ValidationError.java new file mode 100644 index 0000000..8325f48 --- /dev/null +++ b/src/main/java/br/com/fiap/grupo30/fastfood/adapters/in/rest/exceptions/ValidationError.java @@ -0,0 +1,14 @@ +package br.com.fiap.grupo30.fastfood.adapters.in.rest.exceptions; + +import java.util.ArrayList; +import java.util.List; +import lombok.Getter; + +@Getter +public class ValidationError extends StandardError { + private final List errors = new ArrayList<>(); + + public void addError(String fieldName, String message) { + errors.add(new FieldMessage(fieldName, message)); + } +}