content: package net.alex9849.cocktailpi.endpoints;
  
  import net.alex9849.motorlib.exception.HX711Exception;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  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 org.springframework.web.context.request.WebRequest;
  
  import java.util.Date;
  
  @ControllerAdvice
  public class ExceptionResponder {
  
      @ExceptionHandler(value = {IllegalArgumentException.class, IllegalStateException.class})
      public ResponseEntity<ErrorDetails> handleArgumentAndStateException(Exception ex, WebRequest request) {
          ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(),
                  request.getDescription(false));
          return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
      }
  
      @ExceptionHandler(value = {Exception.class})
      public ResponseEntity<ErrorDetails> handleException(Exception ex, WebRequest request) {
          ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(),
                  request.getDescription(false));
          ex.printStackTrace();
          return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
      }
  
      @ResponseStatus(HttpStatus.BAD_REQUEST)
      @ExceptionHandler(MethodArgumentNotValidException.class)
      public ResponseEntity<ErrorDetails> handleValidationExceptions(MethodArgumentNotValidException ex, WebRequest request) {
          String errorMsg;
          if(ex.hasFieldErrors()) {
              errorMsg = ex.getFieldError().getField() + " " + ex.getFieldError().getDefaultMessage();
          } else {
              errorMsg = ex.getMessage();
          }
          ErrorDetails errorDetails = new ErrorDetails(new Date(), errorMsg, request.getDescription(false));
          return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
      }
  
      public class ErrorDetails {
          private Date timestamp;
          private String message;
          private String details;
  
          public ErrorDetails(Date timestamp, String message, String details) {
              super();
              this.timestamp = timestamp;
              this.message = message;
              this.details = details;
          }
  
          public Date getTimestamp() {
              return timestamp;
          }
  
          public String getMessage() {
              return message;
          }
  
  
          public String getDetails() {
              return details;
          }
      }
  }
  