Advertisement
pperez_awto

ServiceExceptionHandler

Mar 20th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package cl.awto.microservice.poinvoice.interceptor.handler;
  2.  
  3. import cl.awto.microservice.poinvoice.domain.exceptions.AwtoException;
  4. import cl.awto.microservice.poinvoice.domain.exceptions.SecurityException;
  5. import cl.awto.microservice.poinvoice.interceptor.model.Details;
  6. import cl.awto.microservice.poinvoice.interceptor.model.Error;
  7. import cl.awto.microservice.poinvoice.interceptor.model.GenericResponse;
  8. import cl.awto.microservice.poinvoice.interceptor.model.StatusResponse;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.validation.BindingResult;
  13. import org.springframework.validation.FieldError;
  14. import org.springframework.web.bind.MethodArgumentNotValidException;
  15. import org.springframework.web.bind.annotation.ControllerAdvice;
  16. import org.springframework.web.bind.annotation.ExceptionHandler;
  17. import org.springframework.web.bind.annotation.ResponseStatus;
  18.  
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22.  
  23. @Configuration
  24. @ControllerAdvice
  25. public class ServiceExceptionHandler {
  26.  
  27.     @ExceptionHandler(SecurityException.class)
  28.     ResponseEntity<GenericResponse> handleSecurityException(HttpServletRequest req, RuntimeException ex) {
  29.         if (ex instanceof SecurityException) {
  30.             SecurityException response = (SecurityException) ex;
  31.  
  32.             GenericResponse genericResponse = new GenericResponse(
  33.                 new StatusResponse(
  34.                     response.getStatus().getCode(),
  35.                     response.getStatus().name(),
  36.                     response.getStatus().getMessage()
  37.                 ),
  38.                 null
  39.             );
  40.  
  41.             return new ResponseEntity<>(
  42.                 genericResponse,
  43.                 HttpStatus.valueOf(response.getStatus().getHttpCode())
  44.             );
  45.         }
  46.         return new ResponseEntity<>(
  47.             new GenericResponse(null, null),
  48.             HttpStatus.BAD_REQUEST
  49.         );
  50.     }
  51.  
  52.     @ExceptionHandler(RuntimeException.class)
  53.     ResponseEntity<GenericResponse> handleControllerException(HttpServletRequest req, RuntimeException ex) {
  54.         if (ex instanceof AwtoException) {
  55.             AwtoException response = (AwtoException) ex;
  56.  
  57.             GenericResponse genericResponse = new GenericResponse(
  58.                 new StatusResponse(
  59.                     response.getStatus().getCode(),
  60.                     response.getStatus().name(),
  61.                     response.getStatus().getMessage()
  62.                 ),
  63.                 null
  64.             );
  65.  
  66.             return new ResponseEntity<>(
  67.                 genericResponse,
  68.                 HttpStatus.valueOf(response.getStatus().getHttpCode())
  69.             );
  70.         }
  71.         return new ResponseEntity<>(
  72.             new GenericResponse(null, null),
  73.             HttpStatus.BAD_REQUEST
  74.         );
  75.     }
  76.  
  77.     @ExceptionHandler(value = {MethodArgumentNotValidException.class})
  78.     @ResponseStatus(HttpStatus.BAD_REQUEST)
  79.     public ResponseEntity<Error> constraintViolationException(MethodArgumentNotValidException exception) {
  80.         List<Details> details = new ArrayList<>();
  81.  
  82.         //Validate bean
  83.         BindingResult result = exception.getBindingResult();
  84.  
  85.         List<FieldError> fieldErrors = result.getFieldErrors();
  86.  
  87.         for (FieldError fieldError : fieldErrors) {
  88.             details.add(new Details(fieldError.getField(), fieldError.getDefaultMessage()));
  89.         }
  90.  
  91.         return new ResponseEntity(
  92.             new Error("Cuerpo de la petición inválido.", details),
  93.             HttpStatus.INTERNAL_SERVER_ERROR
  94.         );
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement