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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.java110.utils.exception;
 
 
import com.alibaba.fastjson.JSONObject;
 
import java.io.PrintStream;
import java.io.PrintWriter;
 
/**
 * 无权限异常
 * Created by wuxw on 2018/4/14.
 */
public class FilterException extends RuntimeException {
 
 
    private Result result;
    private Throwable cause = this;
 
    public FilterException(){}
 
    /**
     * 构造方法
     * @param result 返回值
     * @param cause  异常堆栈
     */
    public FilterException(Result result, Throwable cause) {
        super(result.getMsg(), cause);
        this.result = result;
    }
 
    /**
     * 构造方法
     * @param code 返回码
     * @param msg  错误消息
     */
    public FilterException(int code, String msg) {
        super(msg);
        this.result = new Result(code, msg);
    }
 
    public FilterException(String code, String msg) {
        super(msg);
        this.result = new Result(code, msg);
    }
 
    /**
     * 构造方法
     * @param result 返回值
     * @param detail 具体的返回消息
     */
    public FilterException(Result result, String detail) {
        super(result.getMsg() + "," + detail);
        this.result = new Result(result.getCode(), result.getMsg() + "," + detail);
    }
 
    /**
     * 构造方法
     * @param result 返回值
     * @param detail 具体的返回消息
     * @param cause  异常堆栈
     */
    public FilterException(Result result, String detail, Throwable cause) {
        super(result.getMsg() + "," + detail, cause);
        this.result = new Result(result.getCode(), result.getMsg() + "," + detail);
    }
 
    /**
     * 构造方法
     * @param code    返回码
     * @param msg    返回消息
     * @param cause 异常堆栈
     */
    public FilterException(int code, String msg, Throwable cause) {
        super(msg, cause);
 
        if(cause != null) {
            if(cause.getCause() != null) {
                msg += " cause:" + ExceptionUtils.populateExecption(cause.getCause(), 500);
            }
            msg += " StackTrace:"+ExceptionUtils.populateExecption(cause, 500);
        }
        this.result = new Result(code, msg);
    }
 
    /**
     * 构造方法
     * @param code    返回码
     * @param cause    异常堆栈
     */
    public FilterException(int code, Throwable cause) {
        super(cause);
        String msg = "";
 
        if(cause != null) {
            if(cause.getCause() != null) {
                msg += " cause:" + ExceptionUtils.populateExecption(cause.getCause(), 500);
            }
            msg += " StackTrace:"+ExceptionUtils.populateExecption(cause, 500);
        }
        this.result = new Result(code, msg);
    }
 
    /**
     *
     * TODO 简单描述该方法的实现功能(可选).
     * @see Throwable#getCause()
     */
    public synchronized Throwable getCause() {
        return (cause==this ? super.getCause() : cause);
    }
 
 
    /**
     * 返回异常消息
     * @return 异常消息
     */
    @Override
    public String getMessage() {
        return ExceptionUtils.buildMessage(super.getMessage(), getCause());
    }
 
    /**
     * 异常
     * @return
     */
    public String toJsonString() {
        JSONObject exceptionJson = JSONObject.parseObject("{\"exception\":{}");
        JSONObject exceptionJsonObj = exceptionJson.getJSONObject("exception");
 
        if (getResult() != null)
            exceptionJsonObj.putAll(JSONObject.parseObject(result.toString()));
 
        exceptionJsonObj.put("exceptionTrace",getMessage());
 
        return exceptionJsonObj.toString();
    }
    @Override
    public void printStackTrace(PrintStream ps) {
        ps.print("<exception>");
        if (getResult() != null) {
            ps.print(result.toString());
        }
        ps.append("<exceptionTrace>");
 
        Throwable cause = getCause();
        if (cause == null) {
            super.printStackTrace(ps);
        } else {
            ps.println(this);
            ps.print("Caused by: ");
            cause.printStackTrace(ps);
        }
        ps.append("</exceptionTrace>");
        ps.println("</exception>");
    }
 
    @Override
    public void printStackTrace(PrintWriter pw) {
        pw.print("<exception>");
        if (getResult() != null) {
            pw.print(result.toString());
        }
        pw.append("<exceptionTrace>");
 
        Throwable cause = getCause();
        if (cause == null) {
            super.printStackTrace(pw);
        } else {
            pw.println(this);
            pw.print("Caused by: ");
            cause.printStackTrace(pw);
        }
        pw.append("</exceptionTrace>");
        pw.println("</exception>");
    }
 
    /**
     * 返回异常值
     * @return    异常值对象
     */
    public Result getResult() {
        return result;
    }
 
    public void setResult(Result result) {
        this.result = result;
    }
 
}