package com.dgtly.apiframework.exception;

import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.domain.ResultType;
import com.dgtly.common.exception.BusinessException;
import com.dgtly.common.exception.DemoModeException;
import com.dgtly.common.utils.ServletUtils;
import com.dgtly.common.utils.security.PermissionUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 * 
 * @author ruoyi
 */
@RestControllerAdvice
public class ApiGlobalExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(ApiGlobalExceptionHandler.class);


    /**
     * 请求方式不支持
     */
    @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    public AjaxResult handleException(HttpRequestMethodNotSupportedException e)
    {
        log.error(e.getMessage(), e);
        return AjaxResult.error("不支持' " + e.getMethod() + "'请求");
    }
    /**
     * 系统异常
     */
    @ExceptionHandler(LessParamException.class)
    public AjaxResult handleException(LessParamException e)
    {
        log.error(e.getMessage(), e);
        return AjaxResult.error(ResultType.LESSPARAM);
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult notFount(RuntimeException e)
    {
        log.error("运行时异常:", e);
        return AjaxResult.error("运行时异常:" + e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e)
    {
        log.error(e.getMessage(), e);
        return AjaxResult.error("服务器错误，请联系管理员");
    }

}
