Ver Código Fonte

全局异常逻辑优化,局部异常和错误页面逻辑有待完善。

huangbo 6 anos atrás
pai
commit
04562b7464

+ 6 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/IpuRestDemoStart.java

@ -2,6 +2,7 @@ package com.ai.ipu.server.demo;
2 2

3 3
import org.springframework.context.support.ClassPathXmlApplicationContext;
4 4

5
import com.ai.ipu.basic.util.IpuBaseException;
5 6
import com.ai.ipu.restful.boot.IpuRestApplication;
6 7
import com.ai.ipu.restful.spring.SpringManager;
7 8

@ -13,10 +14,15 @@ import com.ai.ipu.restful.spring.SpringManager;
13 14
 */
14 15
public class IpuRestDemoStart {
15 16
    public final static String DUBBO_CONSUMER_CONFIG = "dubbo-consumer-simple.xml";
17
    public final static String EXCEPTION_MESSAGES_CONFIG = "exception_messages";
16 18
    
17 19
    public static void main(String[] args) {
20
        /*在spring中注册dubbo消费者配置*/
18 21
        SpringManager.registerSpringContext(
19 22
                new ClassPathXmlApplicationContext(new String[] { DUBBO_CONSUMER_CONFIG }));
23
        /*注册dubbo异常信息编码配置*/
24
        IpuBaseException.registerCode(EXCEPTION_MESSAGES_CONFIG);
25
        /*启动SpringBoot服务*/
20 26
        IpuRestApplication.start(args);
21 27
    }
22 28
}

+ 44 - 13
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/common/ExceptionController.java

@ -5,38 +5,69 @@ import org.springframework.web.bind.annotation.RequestMapping;
5 5

6 6
import com.ai.ipu.basic.util.IpuUtility;
7 7
import com.ai.ipu.data.JMap;
8
import com.ai.ipu.data.impl.JsonMap;
8 9

10
/**
11
 * @author huangbo@asiainfo.com
12
 * @team IPU
13
 * @date 2019年1月5日下午18:05:42
14
 * @desc 各类异常处理范例
15
 */
9 16
@Controller
10 17
@RequestMapping("/excep")
11 18
public class ExceptionController {
12 19
    
20
    /**
21
     * @author huangbo@asiainfo.com
22
     * @title: defaultException
23
     * @desc: 抛出默认异常的范例
24
     */
25
    @RequestMapping("/default")
26
    public JMap defaultException(JMap data) throws Exception {
27
        if(true){
28
            IpuUtility.error("抛出默认异常");
29
        }
30
        return data;
31
    }
32
    
33
    /**
34
     * @author huangbo@asiainfo.com
35
     * @title: customException
36
     * @desc: 抛出定制异常的范例
37
     */
13 38
    @SuppressWarnings("unused")
14 39
    @RequestMapping("/custom")
15 40
    public JMap customException(JMap data) throws Exception {
41
        JMap result = new JsonMap();
16 42
        if(true){
17 43
            throw new Exception("抛出定制异常");
18 44
        }
19
        return data;
45
        return result;
20 46
    }
21 47
    
22
    @RequestMapping("/default")
23
    public JMap defaultException(JMap data) throws Exception {
48
    /**
49
     * @author huangbo@asiainfo.com
50
     * @title: codeException
51
     * @desc: 抛出异常编码的范例
52
     */
53
    @RequestMapping("/code")
54
    public JMap codeException(JMap data) throws Exception {
24 55
        if(true){
25
            IpuUtility.error("抛出默认异常");
56
            IpuUtility.errorCode("100");
26 57
        }
27 58
        return data;
28 59
    }
29 60
    
30

31 61
    /**
32 62
     * @author huangbo@asiainfo.com
33
     * @title: uiExceptionHandler
34
     * @desc: 局部异常,对应全局异常
63
     * @title: codeMatcherException
64
     * @desc: 抛出有可变参数的异常编码的范例
35 65
     */
36
    /*@ExceptionHandler
37
    public ModelAndView uiExceptionHandler(Exception ex){
38
        ModelAndView mv = new ModelAndView("/custom");
39
        mv.addObject("exception", ex);
40
        return mv;
41
    }*/
66
    @RequestMapping("/matcher")
67
    public JMap codeMatcherException(JMap data) throws Exception {
68
        if(true){
69
            IpuUtility.errorCode("200", "Request");
70
        }
71
        return data;
72
    }
42 73
}

+ 59 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/common/LocalExceptionController.java

@ -0,0 +1,59 @@
1
package com.ai.ipu.server.demo.control.common;
2

3
import org.apache.commons.lang3.exception.ExceptionUtils;
4
import org.springframework.stereotype.Controller;
5
import org.springframework.web.bind.annotation.ExceptionHandler;
6
import org.springframework.web.bind.annotation.RequestMapping;
7
import org.springframework.web.servlet.ModelAndView;
8

9
import com.ai.ipu.basic.string.StringUtil;
10
import com.ai.ipu.basic.util.IpuBaseException;
11
import com.ai.ipu.basic.util.IpuUtility;
12
import com.ai.ipu.data.JMap;
13
import com.ai.ipu.restful.util.IpuRestConstant;
14

15
/**
16
 * @author huangbo@asiainfo.com
17
 * @team IPU
18
 * @date 2019年3月1日上午12:18:00
19
 * @desc 局部异常的处理范例
20
 */
21
@Controller
22
@RequestMapping("/excep")
23
public class LocalExceptionController {
24
    
25
    /**
26
     * @author huangbo@asiainfo.com
27
     * @title: localException
28
     * @desc: 抛出局部异常的范例
29
     */
30
    @RequestMapping("/local")
31
    public JMap localException(JMap data) throws Exception {
32
        if(true){
33
            IpuUtility.error("抛出局部异常");
34
        }
35
        return data;
36
    }
37
    
38
    /**
39
     * @author huangbo@asiainfo.com
40
     * @title: localExceptionHandler
41
     * @desc: 捕获局部异常,对应全局异常
42
     */
43
    @ExceptionHandler
44
    public ModelAndView localExceptionHandler(Exception e){
45
        ModelAndView mv = new ModelAndView("/custom"); //ModelAndView只能对应Jsp吗?
46
        if(IpuBaseException.class.isAssignableFrom(e.getClass())){
47
            String resultCode = ((IpuBaseException)e).getCode();
48
            resultCode = StringUtil.isEmpty(resultCode) ? IpuRestConstant.ResultCode.ERROR_CODE : resultCode;
49
            mv.addObject("error_msg", e.getMessage());
50
            mv.addObject("error_code", resultCode);
51
            mv.addObject("error_trace", ExceptionUtils.getStackTrace(e));
52
            return mv;
53
        }else{
54
            mv.addObject("error_msg", e.getMessage());
55
            mv.addObject("error_trace", ExceptionUtils.getStackTrace(e));
56
            return mv;
57
        }
58
    }
59
}

+ 22 - 4
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/control/ui/UiController.java

@ -8,18 +8,18 @@ import javax.servlet.http.HttpServletResponse;
8 8
import org.springframework.stereotype.Controller;
9 9
import org.springframework.web.bind.annotation.RequestMapping;
10 10

11

12 11
/**
13
 * @author huangbo
12
 * @author huangbo@asiainfo.com
13
 * @team IPU
14 14
 * @date 2017年11月26日 下午11:50:04
15
 * @desc 
15
 * @desc 常用页面范例
16 16
 */
17 17
@Controller
18 18
@RequestMapping("/ui")
19 19
public class UiController {
20 20

21 21
	@RequestMapping("/login")
22
    public String login() {
22
    public String pageLogin() {
23 23
        return "../login.html";
24 24
    }
25 25
	
@ -31,4 +31,22 @@ public class UiController {
31 31
		buff.append("/index.html");
32 32
		response.sendRedirect(buff.toString());
33 33
	}
34
	
35
	@RequestMapping("/404")
36
    public void page404(HttpServletRequest request, HttpServletResponse response) throws IOException  {
37
        StringBuilder buff = new StringBuilder();
38
        buff.append("../ipuui/");
39
        buff.append("demo");
40
        buff.append("/index.html");
41
        response.sendRedirect(buff.toString());
42
    }
43
	
44
	@RequestMapping("/error")
45
    public void pageError(HttpServletRequest request, HttpServletResponse response) throws IOException  {
46
        StringBuilder buff = new StringBuilder();
47
        buff.append("../ipuui/");
48
        buff.append("demo");
49
        buff.append("/index.html");
50
        response.sendRedirect(buff.toString());
51
    }
34 52
}

+ 1 - 1
ipu-rest-demo/src/main/resources/exception_messages_zh_CN.properties

@ -1,2 +1,2 @@
1 1
100=条件参数不能为空
2
200=[%v]不是主键,条件参数需要全部为主键
2
200=[%v]参数异常