package net.hehe.web;

import static org.springframework.util.Assert.notNull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * author: TaoLei
 * date: 2015/11/21.
 * description:
 */
public class ExceptionHandler extends AbstractHandlerExceptionResolver {
    protected Logger log = LoggerFactory.getLogger(this.getClass());

    //数据库相关异常
    private String foreignErr = "a foreign key constraint fails";
    private String foreignMsg = "该资源被其它业务占用,无法删除";
    private MappingToJsonView viewName;

    public void setViewName(MappingToJsonView viewName) {
        this.viewName = viewName;
    }

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
                                              Object handler, Exception e) {
        log.error("Catch Exception: ", e);
        ModelAndView mav = new ModelAndView(viewName);
        mav.addObject(ResponseStatus.SUCCESS, false);
        String msg = e.getLocalizedMessage();
        if (msg.indexOf(foreignErr) > 0) {
            msg = foreignMsg;
        }
        mav.addObject(ResponseStatus.ERROR, msg);
        return mav;
    }
}

@@ -20,64 +20,8 @@
 b;%0A%0A
-import static org.springframework.util.Assert.notNull;%0A%0A
 impo
@@ -210,67 +210,8 @@
 ver;
-%0Aimport org.springframework.beans.factory.InitializingBean;
 %0A%0Aim

delete redundant package