Repository navigation
Expand file tree
/
Copy pathErr.java
More file actions
279 lines (246 loc) · 7.67 KB
/
Copy pathErr.java
File metadata and controls
279 lines (246 loc) · 7.67 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jooby;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Throwables;
/**
* An exception that carry a {@link Status}. The status field will be set in the HTTP
* response.
*
* See {@link Err.Handler} for more details on how to deal with exceptions.
*
* @author edgar
* @since 0.1.0
*/
@SuppressWarnings("serial")
public class Err extends RuntimeException {
/**
* Missing parameter/header or request attribute.
*
* @author edgar
*/
public static class Missing extends Err {
/**
* Creates a new {@link Missing} error.
*
* @param name Name of the missing parameter/header or request attribute.
*/
public Missing(final String name) {
super(Status.BAD_REQUEST, name);
}
}
/**
* Default err handler it does content negotation.
*
* On <code>text/html</code> requests the err handler creates an <code>err</code> view and use the
* {@link Err#toMap()} result as model.
*
* @author edgar
* @since 0.1.0
*/
public static class DefHandler implements Err.Handler {
/** Default err view. */
public static final String VIEW = "err";
/** logger, logs!. */
private final Logger log = LoggerFactory.getLogger(Err.class);
@Override
public void handle(final Request req, final Response rsp, final Err ex) throws Throwable {
log.error("execution of: {}{} resulted in exception\nRoute:\n{}\n\nStacktrace:",
req.method(), req.path(), req.route().print(6), ex);
rsp.send(
Results
.when(MediaType.html, () -> Results.html(VIEW).put("err", ex.toMap()))
.when(MediaType.all, () -> ex.toMap()));
}
}
/**
* Handle and render exceptions. Error handlers are executed in the order they were provided, the
* first err handler that send an output wins!
*
* The default err handler does content negotation on error, see {@link DefHandler}.
*
* @author edgar
* @since 0.1.0
*/
public interface Handler {
/**
* Handle a route exception by probably logging the error and sending a err response to the
* client.
*
* Please note you always get an {@link Err} whenever you throw it or not. For example if your
* application throws an {@link IllegalArgumentException} exception you will get an {@link Err}
* and you can retrieve the original exception by calling {@link Err#getCause()}.
*
* Jooby always give you an {@link Err} with an optional root cause and an associated status
* code.
*
* @param req HTTP request.
* @param rsp HTTP response.
* @param ex Error found and status code.
* @throws Throwable If something goes wrong.
*/
void handle(Request req, Response rsp, Err ex) throws Throwable;
}
/**
* The status code. Required.
*/
private int status;
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
* @param message A error message. Required.
* @param cause The cause of the problem.
*/
public Err(final Status status, final String message, final Throwable cause) {
super(message(status, message), cause);
this.status = status.value();
}
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
* @param message A error message. Required.
* @param cause The cause of the problem.
*/
public Err(final int status, final String message, final Throwable cause) {
super(message("", status, message), cause);
this.status = status;
}
/**
* Creates a new {@link Err}.
*
* @param status A web socket close status. Required.
* @param message Close message.
*/
public Err(final WebSocket.CloseStatus status, final String message) {
super(message(status.reason(), status.code(), message));
this.status = status.code();
}
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
* @param message A error message. Required.
*/
public Err(final Status status, final String message) {
super(message(status, message));
this.status = status.value();
}
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
* @param message A error message. Required.
*/
public Err(final int status, final String message) {
this(Status.valueOf(status), message);
}
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
* @param cause The cause of the problem.
*/
public Err(final Status status, final Throwable cause) {
super(message(status, null), cause);
this.status = status.value();
}
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
* @param cause The cause of the problem.
*/
public Err(final int status, final Throwable cause) {
this(Status.valueOf(status), cause);
}
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
*/
public Err(final Status status) {
super(message(status, null));
this.status = status.value();
}
/**
* Creates a new {@link Err}.
*
* @param status A HTTP status. Required.
*/
public Err(final int status) {
this(Status.valueOf(status));
}
/**
* @return The status code to send as response.
*/
public int statusCode() {
return status;
}
/**
* Produces a friendly view of the err, resulting map has these attributes:
*
* <pre>
* message: exception message (if present)
* stacktrace: array with the stacktrace
* status: status code
* reason: a status code reason
* </pre>
*
* @return A lightweight view of the err.
*/
public Map<String, Object> toMap() {
Status status = Status.valueOf(this.status);
Throwable cause = Optional.ofNullable(getCause()).orElse(this);
String message = Optional.ofNullable(cause.getMessage()).orElse(status.reason());
String[] stacktrace = Throwables.getStackTraceAsString(cause).replace("\r", "").split("\\n");
Map<String, Object> err = new LinkedHashMap<>();
err.put("message", message);
err.put("stacktrace", stacktrace);
err.put("status", status.value());
err.put("reason", status.reason());
return err;
}
/**
* Build an error message using the HTTP status.
*
* @param status The HTTP Status.
* @param tail A message to append.
* @return An error message.
*/
private static String message(final Status status, final String tail) {
return message(status.reason(), status.value(), tail);
}
/**
* Build an error message using the HTTP status.
*
* @param reason Reason.
* @param status The Status.
* @param tail A message to append.
* @return An error message.
*/
private static String message(final String reason, final int status, final String tail) {
return reason + "(" + status + ")" + (tail == null ? "" : ": " + tail);
}
}