OpenAPI RouteBuilder error handling

10 views
Skip to first unread message

Ronald van Raaphorst

unread,
Mar 19, 2026, 6:28:02 AM (3 days ago) Mar 19
to vert.x
Hi all,

I'm using the Web OpenAPI RouteBuilder to add routes based on an OpenAPI specification (v5.0.0 btw).
That seems to work fine for valid requests.

If I have a request with a missing but required query parameter, a 404 is returned and the failureHandler is called (see below). 
This failureHandler however has no information about the error, nor is it possible to validate the request again just to get the error.

I'd have expected that the failure handler would accept the routingContext and the exception/failure, so that it can be used for something meaningful, like error logging.

Am I missing something?

Best regards,
Ronald


private Router fromOpenApiContract(OpenAPIContract openApiContract) {
  RouterBuilder routerBuilder = RouterBuilder.create(vertx, openApiContract);
  RequestValidator validator = RequestValidator.create(vertx, openApiContract);

  for (OpenAPIRoute route : routerBuilder.getRoutes()) {
    // Access the operation object from the contract
    Operation operation = route.getOperation();

    route.addHandler(routingContext -> {
      // Delegate the call elsewhere
    });

    route.addFailureHandler(routingContext -> {

    // How do I know what the error message was
    // For example, in the case of a missing required query parameter
    // The related request / response does not contain the required query parameter p
    //
    // The request has already been validated, but I don't have the error here
    // (Id' like to return/log the proper error message!)

    // And if I validate the request again, I get a "Request has already been read"
    HttpServerRequest request = routingContext.request();
validator.validate(request, operation.getOperationId())
        .onSuccess(validatedRequest -> {
          // ..
        })
        .onFailure( h -> {
          // Exception: "java.lang.IllegalStateException: Request has already been read"
          routingContext.response().end("404");
        });
    });
  }
  return routerBuilder.createRouter();
}
Reply all
Reply to author
Forward
0 new messages