package com.weather.common.exception;


import com.weather.vo.ApiResult;
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;

/**
 * @author : fengyuhao
 * @desc : 自定义异常处理,拦截所有controller异常 统一处理
 * @update : 修改说明
 * @date : 2020/2/26-02:03

 */
@Slf4j
@ControllerAdvice
@ResponseBody
public class ApiExceptionHandler {

    private  String DEF_MESSAGEE="暂无异常消息";
    private  Integer DEF_CODE=0;
    @ExceptionHandler(ApiException.class)
    public ApiResult handleException(ApiException ex) {

        log.error("自定义异常AppException",ex);
        DEF_CODE=ex.getCode()>0?ex.getCode():ex.getExceptionEnum().getCode();
        DEF_MESSAGEE=ex.getMessage()!=null?ex.getMessage():ex.getExceptionEnum().getMessage();
        return ApiResult.error(DEF_CODE,DEF_MESSAGEE,DEF_MESSAGEE);
    }


}
