package com.sw.mes.config;

import com.sw.mes.core.exception.FeignException;
import com.sw.mes.core.response.ResultCode;
import com.sw.mes.core.response.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author wudanjun
 * @Description：
 * @Date: Create in 10:24 2018/5/16
 * @Modified By:
 */
@Slf4j
@ControllerAdvice
public class RestExceptionHandler {
//    /**
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(MissingPathVariableException.class)
//    @ResponseBody
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    private Result missingPathVariableException(MissingPathVariableException e) {
//        return ResultGenerator.fail(e.getVariableName() + "不能为空");
//    }
//
//
//    @ExceptionHandler(MissingServletRequestParameterException.class)
//    @ResponseBody
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    private Result missingServletRequestParameterException(MissingServletRequestParameterException e) {
//        return ResultGenerator.fail(e.getParameterName() + "不能为空");
//    }
//
//    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
//    @ResponseBody
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    private Result MethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
//        return ResultGenerator.fail(e.getParameter().getParameterName() + "参数类型错误");
//    }

    /**
     * 远程服务异常消息
     *
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler(FeignException.class)
    @ResponseBody
    public Object handleBusinessException(HttpServletRequest request, Exception ex) {
        ex.printStackTrace();
        return ResultGenerator.fail(ResultCode.ESERVER.code, ex.getMessage());
    }

    @ExceptionHandler
    @ResponseBody
    public Object handleException(HttpServletRequest request, Exception ex) {
        log.error("系统异常错误：", ex);
        return ResultGenerator.fail("请求错误：" + ex.getMessage());
    }

}
