com.gitee.rabbitnoteeth.bedrock.http.server.handler.FailureHandler Maven / Gradle / Ivy
The newest version!
package com.gitee.rabbitnoteeth.bedrock.http.server.handler;
import com.gitee.rabbitnoteeth.bedrock.http.server.IHttpServer;
import com.gitee.rabbitnoteeth.bedrock.http.server.constant.HttpConstants;
import com.gitee.rabbitnoteeth.bedrock.http.json.JsonResult;
import io.vertx.core.Handler;
import io.vertx.core.VertxException;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;
import java.io.IOException;
public class FailureHandler implements Handler {
private final IHttpServer server;
public FailureHandler(IHttpServer server) {
this.server = server;
}
@Override
public void handle(RoutingContext context) {
HttpServerResponse response = context.response();
response.putHeader("Content-Type", "application/json;charset=UTF-8");
try {
// 处理404
int statusCode = context.statusCode();
if (statusCode == HttpConstants.HTTP_RESPONSE_STATUS_404) {
response.setStatusCode(HttpConstants.HTTP_RESPONSE_STATUS_404).end();
return;
}
// 处理异常
Throwable err = context.failure();
if (!response.closed()) {
server.onError(context, err);
}
} catch (Exception e) {
if (!response.closed()) {
server.onError(context, e);
}
}
}
}