io.vertx.ext.web.Router.errorHandler()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(112)

本文整理了Java中io.vertx.ext.web.Router.errorHandler方法的一些代码示例,展示了Router.errorHandler的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Router.errorHandler方法的具体详情如下:
包路径:io.vertx.ext.web.Router
类名称:Router
方法名:errorHandler

Router.errorHandler介绍

[英]Specify an handler to handle an error for a particular status code. You can use to manage general errors too using status code 500. The handler will be called when the context fails and other failure handlers didn't write the reply or when an exception is thrown inside an handler. You must not use RoutingContext#next() inside the error handler This does not affect the normal failure routing logic.
[中]

代码示例

代码示例来源:origin: vert-x3/vertx-web

@Test
 public void testErrorInCustomErrorHandler() throws Exception {

  router.route("/path").handler(rc -> rc.fail(410));
  router.errorHandler(410, rc -> {
   throw new RuntimeException();
  });

  testRequest(HttpMethod.GET, "/path", 410, "Gone");
 }
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testCustom404ErrorHandler() throws Exception {
 // Default 404 handler
 testRequest(HttpMethod.GET, "/blah", 404, "Not Found", "<html><body><h1>Resource not found</h1></body></html>");
 router.errorHandler(404, routingContext -> routingContext
  .response()
  .setStatusMessage("Not Found")
  .setStatusCode(404)
  .end("Not Found custom error")
 );
 testRequest(HttpMethod.GET, "/blah", 404, "Not Found", "Not Found custom error");
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testCustomErrorHandler() throws Exception {
 router.route("/path").handler(rc -> rc.fail(410));
 router.errorHandler(410, context -> context.response().setStatusCode(500).setStatusMessage("Dumb").end());
 testRequest(HttpMethod.GET, "/path", 500, "Dumb");
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testDecodingErrorCustomHandler() throws Exception {
 String BAD_PARAM = "~!@\\||$%^&*()_=-%22;;%27%22:%3C%3E/?]}{";
 router.errorHandler(400, context -> context.response().setStatusCode(500).setStatusMessage("Dumb").end());
 router.route().handler(rc -> rc.next());
 router.route("/path").handler(rc -> rc.response().setStatusCode(500).end());
 testRequest(HttpMethod.GET, "/path?q=" + BAD_PARAM, 500,"Dumb");
}

代码示例来源:origin: io.vertx/vertx-rx-java

/**
 * Specify an handler to handle an error for a particular status code. You can use to manage general errors too using status code 500.
 * The handler will be called when the context fails and other failure handlers didn't write the reply or when an exception is thrown inside an handler.
 * You <b>must not</b> use {@link io.vertx.rxjava.ext.web.RoutingContext#next} inside the error handler
 * This does not affect the normal failure routing logic.
 * @param statusCode status code the errorHandler is capable of handle
 * @param errorHandler error handler. Note: You <b>must not</b> use {@link io.vertx.rxjava.ext.web.RoutingContext#next} inside the provided handler
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.Router errorHandler(int statusCode, Handler<io.vertx.rxjava.ext.web.RoutingContext> errorHandler) { 
 io.vertx.rxjava.ext.web.Router ret = io.vertx.rxjava.ext.web.Router.newInstance(delegate.errorHandler(statusCode, new Handler<io.vertx.ext.web.RoutingContext>() {
  public void handle(io.vertx.ext.web.RoutingContext event) {
   errorHandler.handle(io.vertx.rxjava.ext.web.RoutingContext.newInstance(event));
  }
 }));
 return ret;
}

代码示例来源:origin: vert-x3/vertx-rx

/**
 * Specify an handler to handle an error for a particular status code. You can use to manage general errors too using status code 500.
 * The handler will be called when the context fails and other failure handlers didn't write the reply or when an exception is thrown inside an handler.
 * You <b>must not</b> use {@link io.vertx.rxjava.ext.web.RoutingContext#next} inside the error handler
 * This does not affect the normal failure routing logic.
 * @param statusCode status code the errorHandler is capable of handle
 * @param errorHandler error handler. Note: You <b>must not</b> use {@link io.vertx.rxjava.ext.web.RoutingContext#next} inside the provided handler
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.Router errorHandler(int statusCode, Handler<io.vertx.rxjava.ext.web.RoutingContext> errorHandler) { 
 io.vertx.rxjava.ext.web.Router ret = io.vertx.rxjava.ext.web.Router.newInstance(delegate.errorHandler(statusCode, new Handler<io.vertx.ext.web.RoutingContext>() {
  public void handle(io.vertx.ext.web.RoutingContext event) {
   errorHandler.handle(io.vertx.rxjava.ext.web.RoutingContext.newInstance(event));
  }
 }));
 return ret;
}

代码示例来源:origin: io.vertx/vertx-web

@Test
 public void testErrorInCustomErrorHandler() throws Exception {

  router.route("/path").handler(rc -> rc.fail(410));
  router.errorHandler(410, rc -> {
   throw new RuntimeException();
  });

  testRequest(HttpMethod.GET, "/path", 410, "Gone");
 }
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testCustom404ErrorHandler() throws Exception {
 // Default 404 handler
 testRequest(HttpMethod.GET, "/blah", 404, "Not Found", "<html><body><h1>Resource not found</h1></body></html>");
 router.errorHandler(404, routingContext -> routingContext
  .response()
  .setStatusMessage("Not Found")
  .setStatusCode(404)
  .end("Not Found custom error")
 );
 testRequest(HttpMethod.GET, "/blah", 404, "Not Found", "Not Found custom error");
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testCustomErrorHandler() throws Exception {
 router.route("/path").handler(rc -> rc.fail(410));
 router.errorHandler(410, context -> context.response().setStatusCode(500).setStatusMessage("Dumb").end());
 testRequest(HttpMethod.GET, "/path", 500, "Dumb");
}

代码示例来源:origin: io.vertx/vertx-web

@Test
public void testDecodingErrorCustomHandler() throws Exception {
 String BAD_PARAM = "~!@\\||$%^&*()_=-%22;;%27%22:%3C%3E/?]}{";
 router.errorHandler(400, context -> context.response().setStatusCode(500).setStatusMessage("Dumb").end());
 router.route().handler(rc -> rc.next());
 router.route("/path").handler(rc -> rc.response().setStatusCode(500).end());
 testRequest(HttpMethod.GET, "/path?q=" + BAD_PARAM, 500,"Dumb");
}

相关文章