自动化立体仓库 - WMS系统
skyouc
昨天 e26b2f48c63d8b6a7757e1e8c4f04e006ccfbfe9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.zy.common.config;
 
import com.core.common.R;
import com.core.exception.CoolException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
/**
 * Created by vincent on 2019-06-09
 */
@RestControllerAdvice
public class CoolExceptionHandler {
 
    @ExceptionHandler(Exception.class)
    public Object handlerException(HandlerMethod handler, Exception e, HttpServletRequest request) {
        e.printStackTrace();
        if (useAlt(request)) {
            return altError("500", "服务器内部错误");
        }
        return R.error();
    }
 
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Object handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e,
            HttpServletRequest request) {
        if (useAlt(request)) {
            return altError("405", "HTTP方法不支持");
        }
        return R.error();
    }
 
    @ExceptionHandler(CoolException.class)
    public Object handleRRException(CoolException e, HttpServletRequest request) {
        if (useAlt(request)) {
            String[] split = e.getMessage().split("-");
            String code = null;
            String msg = e.getMessage();
            if (split.length == 2 && String.valueOf(split[0]).length() < 3) {
                code = split[0];
                msg = split[1];
            }
            return altError(code, msg);
        }
        String[] split = e.getMessage().split("-");
        if (split.length == 2) {
            if (String.valueOf(split[0]).length() < 3) {
                return R.parse(e.getMessage());
            }
        }
        return R.error(e.getMessage());
    }
 
    private boolean useAlt(HttpServletRequest request) {
        if (null == request) {
            return false;
        }
        String header = request.getHeader("X-Response-Format");
        if ("alt".equalsIgnoreCase(header)) {
            return true;
        }
        String param = request.getParameter("responseFormat");
        return "alt".equalsIgnoreCase(param);
    }
 
    private Map<String, Object> altError(String code, String message) {
        Map<String, Object> map = new HashMap<>();
        map.put("success", false);
        if (null != code) {
            map.put("code", code);
        } else {
            map.put("code", "200");
        }
        map.put("message", "失败");
        map.put("returnMessage", message);
        map.put("timestamp", System.currentTimeMillis());
        return map;
    }
}