content: package com.bbgcine.overview.web.exception;
  
  import com.fasterxml.jackson.annotation.JsonInclude;
  import jakarta.servlet.http.HttpServletRequest;
  import lombok.*;
  import org.springframework.http.HttpStatus;
  import org.springframework.validation.BindingResult;
  import org.springframework.validation.FieldError;
  
  import java.util.HashMap;
  import java.util.Map;
  
  @Getter
  @Setter
  @ToString
  @JsonInclude(JsonInclude.Include.NON_NULL)
  @NoArgsConstructor
  public class ErrorMessage {
  
      private String path;
      private String method;
      private int status;
      private String statusText;
      private String message;
      private Map<String, String> errors;
  
      public ErrorMessage(HttpServletRequest request, HttpStatus status, String message) {
          this.path = request.getRequestURI();
          this.method = request.getMethod();
          this.status = status.value();
          this.statusText = status.getReasonPhrase();
          this.message = message;
      }
  
      public ErrorMessage(HttpServletRequest request, HttpStatus status, String message, BindingResult result) {
          this.path = request.getRequestURI();
          this.method = request.getMethod();
          this.status = status.value();
          this.statusText = status.getReasonPhrase();
          this.message = message;
          addErrors(result);
      }
  
      private void addErrors(BindingResult result) {
          this.errors = new HashMap<>();
          for (FieldError fieldError : result.getFieldErrors()) {
              this.errors.put(fieldError.getField(), fieldError.getDefaultMessage());
          }
      }
  }