package com.bjtdba.order.handler;

import com.bjtdba.utils.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class ExceptionHandle {
    private  static final Logger loger= (Logger) LoggerFactory.getLogger(ExceptionHandle.class);
    @ExceptionHandler(value = Exception.class)
    public Object handler(Exception exception,HttpServletRequest request){

        loger.info(exception.getMessage(),exception);
        if (exception instanceof NullPointerException){
//            CustomException customException = (CustomException) exception;
            exception.printStackTrace();
            return ResponseUtil.fail(500, "系统异常");
        }else if(exception instanceof HttpMessageNotReadableException){
            exception.printStackTrace();
            return ResponseUtil.fail(400, "请求body参数为空");
        }else if(exception instanceof MethodArgumentNotValidException ){
            exception.printStackTrace();
            return ResponseUtil.fail(405, "请示方式错误");
        }else if(exception instanceof HttpRequestMethodNotSupportedException){
            exception.printStackTrace();
            return ResponseUtil.fail(405, "请示方式错误");
        }  else if(exception instanceof  Exception){
            exception.printStackTrace();
            return ResponseUtil.fail(500, "系统异常");
        }else {
            return ResponseUtil.fail(120, "异常信息：" + exception.getMessage());
        }
    }



}
