content: package med.voll.api.infra.errores;
  
  import jakarta.persistence.EntityNotFoundException;
  import jakarta.validation.ValidationException;
  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.ExceptionHandler;
  import org.springframework.web.bind.annotation.ResponseStatus;
  import org.springframework.web.bind.annotation.RestControllerAdvice;
  
  @RestControllerAdvice
  public class TratadorDeErrores {
  
      @ExceptionHandler(EntityNotFoundException.class)
      @ResponseStatus(HttpStatus.NOT_FOUND)
      public ResponseEntity tratarError404(){
          return ResponseEntity.notFound().build();
      }
  
      @ExceptionHandler(MethodArgumentNotValidException.class)
      @ResponseStatus(HttpStatus.BAD_REQUEST)
      public ResponseEntity tratarError400(MethodArgumentNotValidException e){
          var errores = e.getFieldErrors().stream().map(DatosErrorValidacion::new).toList();
          return ResponseEntity.badRequest().body(errores);
      }
  
      @ExceptionHandler(ValidacionDeIntegridad.class)
      public ResponseEntity errorHandlerValidacionesIntegridad(Exception e){
          return ResponseEntity.badRequest().body(e.getMessage());
      }
  
      @ExceptionHandler(ValidationException.class)
      public ResponseEntity errorHandlerValidacionesDeNegocio(Exception e){
          return ResponseEntity.badRequest().body(e.getMessage());
      }
  
      private record DatosErrorValidacion(String campo, String error){
          public DatosErrorValidacion(FieldError error) {
              this(error.getField(), error.getDefaultMessage());
          }
      }
  
  }
  