forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActServerError.java
More file actions
187 lines (167 loc) · 5.89 KB
/
ActServerError.java
File metadata and controls
187 lines (167 loc) · 5.89 KB
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
package act.view;
import act.Act;
import act.app.*;
import act.exception.BindException;
import act.util.ActError;
import org.osgl.$;
import org.osgl.exception.InvalidRangeException;
import org.osgl.exception.UnsupportedException;
import org.osgl.http.H;
import org.osgl.mvc.annotation.ResponseStatus;
import org.osgl.mvc.result.ErrorResult;
import org.osgl.mvc.result.Result;
import org.osgl.mvc.result.ServerError;
import org.osgl.util.C;
import org.osgl.util.E;
import org.rythmengine.exception.RythmException;
import javax.validation.ValidationException;
import java.util.List;
import java.util.Map;
public class ActServerError extends ServerError implements ActError {
protected SourceInfo sourceInfo;
protected ActServerError(Throwable t) {
super($.notNull(t));
if (Act.isDev()) {
populateSourceInfo(t);
}
}
@Override
public Throwable getCauseOrThis() {
Throwable cause = super.getCause();
return null == cause ? this : cause;
}
public SourceInfo sourceInfo() {
return sourceInfo;
}
@Override
public int statusCode() {
Throwable cause = getCause();
int statusCode = userDefinedStatusCode(cause.getClass());
return -1 == statusCode ? super.statusCode() : statusCode;
}
public List<String> stackTrace() {
List<String> l = C.newList();
Throwable t = getCause();
while (null != t) {
StackTraceElement[] a = t.getStackTrace();
for (StackTraceElement e : a) {
l.add("at " + e.toString());
}
t = t.getCause();
if (null != t) {
l.add("Caused by " + t.toString());
}
}
return l;
}
protected void populateSourceInfo(Throwable t) {
if (t instanceof SourceInfo) {
this.sourceInfo = (SourceInfo)t;
} else {
DevModeClassLoader cl = (DevModeClassLoader) App.instance().classLoader();
for (StackTraceElement stackTraceElement : t.getStackTrace()) {
int line = stackTraceElement.getLineNumber();
if (line <= 0) {
continue;
}
Source source = cl.source(stackTraceElement.getClassName());
if (null == source) {
continue;
}
sourceInfo = new SourceInfoImpl(source, line);
}
}
}
private static Map<Class<? extends Throwable>, $.Function<Throwable, Result>> x = C.newMap();
static {
$.Function<Throwable, Result> unsupported = new $.Transformer<Throwable, Result>() {
@Override
public Result transform(Throwable throwable) {
return ActNotImplemented.create(throwable);
}
};
x.put(UnsupportedException.class, unsupported);
x.put(UnsupportedOperationException.class, unsupported);
x.put(IllegalStateException.class, new $.Transformer<Throwable, Result>() {
@Override
public Result transform(Throwable throwable) {
return ActConflict.create(throwable);
}
});
$.Transformer<Throwable, Result> badRequest = new $.Transformer<Throwable, Result>() {
@Override
public Result transform(Throwable throwable) {
return ActBadRequest.create(throwable);
}
};
x.put(IllegalArgumentException.class, badRequest);
x.put(InvalidRangeException.class, badRequest);
x.put(IndexOutOfBoundsException.class, badRequest);
x.put(ValidationException.class, badRequest);
x.put(BindException.class, badRequest);
}
private static Map<Class, Integer> userDefinedStatus = C.newMap();
private static int userDefinedStatusCode(Class<? extends Throwable> exCls) {
Integer I = userDefinedStatus.get(exCls);
if (null == I) {
ResponseStatus rs = exCls.getAnnotation(ResponseStatus.class);
if (null == rs) {
I = -1;
userDefinedStatus.put(exCls, -1);
} else {
I = rs.value();
}
}
return I;
}
public static Result of(Throwable t) {
if (t instanceof RythmException) {
return new RythmError((RythmException) t);
} else {
$.Function<Throwable, Result> transformer = transformerOf(t);
return null == transformer ? new ActServerError(t) : transformer.apply(t);
}
}
private static $.Function<Throwable, Result> transformerOf(Throwable t) {
Class tc = t.getClass();
$.Function<Throwable, Result> transformer = x.get(tc);
if (null != transformer) {
return transformer;
}
for (Class c : x.keySet()) {
if (c.isAssignableFrom(tc)) {
return x.get(c);
}
}
return null;
}
public static ActServerError of(NullPointerException e) {
return new ActServerError(e);
}
public static ActServerError of(RythmException e) {
return new RythmError(e);
}
public static Result of(int statusCode) {
E.illegalArgumentIf(statusCode < 400);
switch (statusCode) {
case 400:
return ActBadRequest.create();
case 401:
return ActUnauthorized.create();
case 403:
return ActForbidden.create();
case 404:
return ActNotFound.create();
case 405:
return ActMethodNotAllowed.create();
case 409:
return ActConflict.create();
case 500:
return new ActServerError(new RuntimeException());
case 501:
return ActNotImplemented.create();
default:
return new ErrorResult(H.Status.of(statusCode));
}
}
}