# 全局异常

package com.cy.myblog.controller.aspect;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.cy.myblog.common.ex.ServiceException;
import com.cy.myblog.common.vo.JsonResult;
import com.cy.myblog.controller.BaseController;
import com.cy.myblog.controller.ex.NoLoginException;
import com.cy.myblog.controller.ex.PageUnfoundException;
import com.cy.myblog.service.ex.ArgumentValidFaildException;
import com.cy.myblog.service.ex.DataUnfoundException;
import com.cy.myblog.service.ex.DuplicationKeyException;
import com.cy.myblog.service.ex.PropertyValidFaildException;

import lombok.extern.slf4j.Slf4j;

//异常(last) -> view
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends BaseController{
	
	@ExceptionHandler({ServiceException.class , ShiroException.class})
	public JsonResult doExceptionHandle(Throwable e , HttpServletRequest r )  {
		log.error("request URL:{} , Exception:{}" ,r.getRequestURL(), e.toString()); //URL 详尽的资源信息
		JsonResult j = new JsonResult();
		j.setState(FAIL);
		
		//shrio 异常
		if(e instanceof UnknownAccountException) {
			setMessage(j, e,"账户不存在");
		}else if(e instanceof LockedAccountException) {
			setMessage(j, e, "账户状态异常");
		}else if(e instanceof IncorrectCredentialsException) {
			setMessage(j, e, "密码不正确");
		}else if(e instanceof AuthorizationException) {
			setMessage(j, e, "没有此操作权限");
		}else
		//自定义 controller 异常
		if(e instanceof NoLoginException) {
			setMessage(j, e, "请登录");
		}else if(e instanceof PageUnfoundException){
			setMessage(j, e, "404 页面找不到了");
		}else
		//自定义 servicer 异常
		if(e instanceof ArgumentValidFaildException) {
			setMessage(j, e, "参数异常");
		}else if(e instanceof DataUnfoundException) {
			setMessage(j, e, "数据不存在");
		}else if(e instanceof DuplicationKeyException) {
			setMessage(j, e, "键值不能重复");
		}else if(e instanceof PropertyValidFaildException) {//校验异常
			setMessage(j, e, "校验异常!");
			PropertyValidFaildException ex = (PropertyValidFaildException) e; 
			j.setData(ex.getErrors());
		}
		else {
			setMessage(j, e, "系统维护中");
			log.error("Exception {}" , e.getMessage());
		}
		return j;
	}
	/** * 如果没有异常描述,使用默认异常描述 * @param j json 结果串,往message添加异常描述 * @param e 异常 * @param message 默认异常描述 */
	private void setMessage(JsonResult j , Throwable e , String message) {
		if(StringUtils.isEmpty(e.getMessage())) {
			j.setMessage(message);
		}else{
			j.setMessage(e.getMessage());
		}
	}
	
}

# 是什么

# 通常有什么

springboot 中

##ExceptionHandlerExceptionResolver

开发中,通常都加上 <mvc:annotaion-driven /> 注解,所以,下面重点讲 加注解后 替换的 ExceptionHandlerExceptionResolver

## ResponseStatusExceptionResolver


ResponseStatusExceptionResolver

ResponseStatus

写在异常类上

写在方法上

## DefaultHandlerExceptionResolver

<mark>处理特定异常的</mark>

## SimpleMappingExceptionResolver


error 为
war 工程中, WEB-INF/views/error.jsp

(这里views配置里面改了,)

debugger

exceptionAttribute 是属性名

exceptionAttribute 默认值 是 exception

因此,可以在 配置里面 改 异常的名字