package cn.superdesk.uniorder.config.exception;

import cn.superdesk.uniorder.common.exception.OpenApiException;
import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.validation.ObjectError;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/4/8 2:48 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/4/8 2:48 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
public class UnifiedOrderRuntimeExceptionHandler implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {


        log.info("\n/**************************************** resolveException start ****************************************/" +
                "\n{}" +
                "\n/**************************************** resolveException   end ****************************************/", ExceptionUtils.getMessage(e));

        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        if (e instanceof UnifiedOrderRuntimeException) {

            modelAndView.addObject("message", ((UnifiedOrderRuntimeException) e).getMessage());
            modelAndView.addObject("code", ((UnifiedOrderRuntimeException) e).getCode());
            modelAndView.addObject("success", false);


        } else if (e instanceof MethodArgumentNotValidException) {

            // 从异常对象中拿到ObjectError对象
            ObjectError objectError = ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors().get(0);

            // 然后提取错误提示信息进行返回
            modelAndView.addObject("message", objectError.getDefaultMessage());
            modelAndView.addObject("code", 500); // HttpStatus.PARAMETER
            modelAndView.addObject("success", false);

        } else if (e instanceof UndeclaredThrowableException) {
            Throwable cause = ((UndeclaredThrowableException)e).getUndeclaredThrowable().getCause();
            if (cause instanceof UnifiedOrderRuntimeException) {
                modelAndView.addObject("message", ((UnifiedOrderRuntimeException) cause).getMessage());
                modelAndView.addObject("code", ((UnifiedOrderRuntimeException) cause).getCode());
                modelAndView.addObject("success", false);
            } else {

                modelAndView.addObject("message", e.getLocalizedMessage());
                modelAndView.addObject("code", 500);
                modelAndView.addObject("success", false);
            }

        } else {

            modelAndView.addObject("message", e.getLocalizedMessage());
            modelAndView.addObject("code", 500);
            modelAndView.addObject("success", false);
        }

        return modelAndView;

    }
}
