DefaultExceptionHandler.java 4.38 KB
package com.huaheng.framework.web.exception;

import com.alibaba.fastjson.JSONException;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.framework.web.domain.RetCode;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.ObjectError;
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 com.huaheng.common.exception.DemoModeException;
import com.huaheng.common.utils.security.PermissionUtils;
import com.huaheng.framework.web.domain.AjaxResult;

import javax.servlet.http.HttpServletResponse;

/**
 * 自定义异常处理器
 * 
 * @author huaheng
 */
@RestControllerAdvice
public class DefaultExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(DefaultExceptionHandler.class);
    
    /**
     * 权限校验失败
     */
    @ExceptionHandler(AuthorizationException.class)
    public AjaxResult handleAuthorizationException(AuthorizationException ex)
    {
        log.error("权限异常:" + ex.getMessage(), ex);
        return AjaxResult.setResult(RetCode.UNAUTHEN, PermissionUtils.getMsg(ex.getMessage()), null);
    }
    
    /**
     * JSON异常统一处理
     */
    @ExceptionHandler(value = JSONException.class)
    public AjaxResult jsonExceptionHandler(JSONException ex) {
        log.error("转化JSON异常:" + ex.getMessage(), ex);
        return AjaxResult.error("转化JSON异常:" + ex.getMessage());
    }

    /**
     * 方法参数验证异常的处理
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public AjaxResult validatedExceptionHandler(MethodArgumentNotValidException ex) {
        StringBuilder sb = new StringBuilder();
        for (ObjectError errorItem : ex.getBindingResult().getAllErrors()) {
             sb.append(" " + errorItem.getDefaultMessage() + ",");
        }
        sb.deleteCharAt(sb.length() - 1);
        log.error("方法参数验证出现异常:" + sb.toString(), ex);
        return AjaxResult.error("方法参数验证出现异常:" + sb.toString());
    }

    /**
     * 业务异常的处理
     */
    @ExceptionHandler(value = ServiceException.class)
    public AjaxResult serviceExceptionHandler(ServiceException ex) {
        log.error("业务处理出现异常:" + ex.getMessage(), ex);
        return AjaxResult.error("业务处理出现异常:" + ex.getMessage());
    }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    public AjaxResult handleException(HttpRequestMethodNotSupportedException ex)
    {
        log.error("不支持' " + ex.getMethod() + "'请求:" + ex.getMessage(), ex);
        return AjaxResult.error("网络请求异常:不支持' " + ex.getMethod() + "'请求");
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult notFount(RuntimeException ex)
    {
       StringBuilder sb = new StringBuilder();
        StackTraceElement[] stacks = ex.getStackTrace();
        int length = stacks.length > 3 ? 3 : stacks.length;
        for (int i=0; i< length; i++) {
            sb.append("文件:" + stacks[i].getFileName());
            sb.append("类名:" + stacks[i].getClassName());
            sb.append("方法:" + stacks[i].getMethodName());
            sb.append("行数:" + stacks[i].getLineNumber());
            sb.append(System.getProperty("line.separator"));
        }
        log.error("运行时异常:" + ex.getMessage(), ex);
        log.error(sb.toString());
        return AjaxResult.error("运行时异常:" + ex.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception ex)
    {
        log.error("服务器错误:" + ex.getMessage(), ex);
        return AjaxResult.setResult(RetCode.INTERNAL_SERVER_ERROR ,"服务器错误:" + ex.getMessage(), ex);
    }
    
    /**
     * 演示模式异常
     */
    @ExceptionHandler(DemoModeException.class)
    public AjaxResult demoModeException(DemoModeException e)
    {
        return AjaxResult.error("演示模式,不允许操作");
    }

}