package easydo.technology.config.exception;

import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;


/**
 * 处理Controller抛出的异常*
 * @author tianyf
 */
@RestControllerAdvice
@Log4j2
public class BizExceptionHandler {

    /**
     * 为 @NotNull / @NotEmpty / @NotBlank 提供异常后的消息抛出*
     * @param bindException
     * @return
     */
    @ExceptionHandler({BindException.class})
    public ResponseEntity<Object> throwCustomException(BindException bindException){
        log.error("[ @Vaild异常捕获 ] " + bindException.getMessage());
        Map<String, Object> vo = new HashMap<>();
        vo.put("message", bindException.getBindingResult().getFieldError().getDefaultMessage());
        vo.put("code", HttpStatus.BAD_REQUEST.value());
        return new ResponseEntity(vo, HttpStatus.OK);
    }
}
