Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cl.awto.microservice.poinvoice.interceptor.handler;
- import cl.awto.microservice.poinvoice.domain.exceptions.AwtoException;
- import cl.awto.microservice.poinvoice.domain.exceptions.SecurityException;
- import cl.awto.microservice.poinvoice.interceptor.model.Details;
- import cl.awto.microservice.poinvoice.interceptor.model.Error;
- import cl.awto.microservice.poinvoice.interceptor.model.GenericResponse;
- import cl.awto.microservice.poinvoice.interceptor.model.StatusResponse;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.validation.BindingResult;
- 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;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import javax.servlet.http.HttpServletRequest;
- import java.util.ArrayList;
- import java.util.List;
- @Configuration
- @ControllerAdvice
- public class ServiceExceptionHandler {
- @ExceptionHandler(SecurityException.class)
- ResponseEntity<GenericResponse> handleSecurityException(HttpServletRequest req, RuntimeException ex) {
- if (ex instanceof SecurityException) {
- SecurityException response = (SecurityException) ex;
- GenericResponse genericResponse = new GenericResponse(
- new StatusResponse(
- response.getStatus().getCode(),
- response.getStatus().name(),
- response.getStatus().getMessage()
- ),
- null
- );
- return new ResponseEntity<>(
- genericResponse,
- HttpStatus.valueOf(response.getStatus().getHttpCode())
- );
- }
- return new ResponseEntity<>(
- new GenericResponse(null, null),
- HttpStatus.BAD_REQUEST
- );
- }
- @ExceptionHandler(RuntimeException.class)
- ResponseEntity<GenericResponse> handleControllerException(HttpServletRequest req, RuntimeException ex) {
- if (ex instanceof AwtoException) {
- AwtoException response = (AwtoException) ex;
- GenericResponse genericResponse = new GenericResponse(
- new StatusResponse(
- response.getStatus().getCode(),
- response.getStatus().name(),
- response.getStatus().getMessage()
- ),
- null
- );
- return new ResponseEntity<>(
- genericResponse,
- HttpStatus.valueOf(response.getStatus().getHttpCode())
- );
- }
- return new ResponseEntity<>(
- new GenericResponse(null, null),
- HttpStatus.BAD_REQUEST
- );
- }
- @ExceptionHandler(value = {MethodArgumentNotValidException.class})
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- public ResponseEntity<Error> constraintViolationException(MethodArgumentNotValidException exception) {
- List<Details> details = new ArrayList<>();
- //Validate bean
- BindingResult result = exception.getBindingResult();
- List<FieldError> fieldErrors = result.getFieldErrors();
- for (FieldError fieldError : fieldErrors) {
- details.add(new Details(fieldError.getField(), fieldError.getDefaultMessage()));
- }
- return new ResponseEntity(
- new Error("Cuerpo de la petición inválido.", details),
- HttpStatus.INTERNAL_SERVER_ERROR
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement