package net.hehe.web;

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

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

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

    //数据库相关异常
    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, msg);
        return mav;
    }
}

@@ -16,16 +16,72 @@
 e.web;%0A%0A
+import static org.springframework.util.Assert.notNull;%0A%0A
 import o
@@ -97,16 +97,16 @@
 Logger;%0A
-
 import o
@@ -262,16 +262,75 @@
 esolver;
+%0Aimport org.springframework.beans.factory.InitializingBean;
 %0A%0Aimport
@@ -555,28 +555,51 @@
 ver 
-%7B%0A    private static
+implements InitializingBean %7B%0A    protected
  Log
@@ -636,30 +636,23 @@
 ger(
-ExceptionHandler.c
+this.getC
 lass
+()
 );%0A%0A
@@ -1452,16 +1452,16 @@
 , msg);%0A
-
         
@@ -1477,11 +1477,234 @@
 v;%0A    %7D
+%0A%0A    @Override%0A    public void afterPropertiesSet() throws Exception %7B%0A        checkHandlerConfig();%0A    %7D%0A%0A    protected void checkHandlerConfig() %7B%0A        notNull(this.viewName, %22Property 'viewName' is required%22);%0A    %7D
 %0A%7D%0A

add argument required exception for handler