-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathGenericFailureHandler.java
More file actions
46 lines (41 loc) · 2.08 KB
/
GenericFailureHandler.java
File metadata and controls
46 lines (41 loc) · 2.08 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
package com.uid2.operator.vertx;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpClosedException;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import org.apache.http.impl.EnglishReasonPhraseCatalog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GenericFailureHandler implements Handler<RoutingContext> {
private static final Logger LOGGER = LoggerFactory.getLogger(GenericFailureHandler.class);
@Override
public void handle(RoutingContext ctx) {
// Status code will be 500 for the RuntimeException
int statusCode = ctx.statusCode();
HttpServerResponse response = ctx.response();
String url = ctx.normalizedPath();
Throwable t = ctx.failure();
if (t != null) {
// Because Vert.x swallows stack traces so cannot log stack trace
// And we want to ignore HttpClosedException errors as it is (usually) caused by users and no impact
if (t instanceof HttpClosedException) {
LOGGER.warn("Ignoring exception - URL: [{}] - Error:", url, t);
response.end();
} else if (statusCode >= 500 && statusCode < 600) { // 5xx is server error, so error
LOGGER.error("URL: [{}] - Error response code: [{}] - Error:", url, statusCode, t);
} else if (statusCode >= 400 && statusCode < 500) { // 4xx is user error, so just warn
LOGGER.warn("URL: [{}] - Error response code: [{}] - Error:", url, statusCode, t);
}
}
if (!response.ended() && !response.closed()) {
String responseBody;
// Include error message for debugging (especially in e2e tests)
if (t != null && t.getMessage() != null) {
responseBody = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, null) + ": " + t.getMessage();
} else {
responseBody = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, null);
}
response.setStatusCode(statusCode).end(responseBody);
}
}
}