Exception Handling and Validation Handler in Vertx applications

Photo by Patrick Xu on Unsplash
  • onComplete will in invoked when the execution is completed, either it is succeeded or failed.
  • onSuccess handles the successful result.
  • onFailure catches the exception thrown in the execution.
public class PostNotFoundException extends RuntimeException {
public PostNotFoundException(UUID id) {
super("Post id: " + id + " was not found. ");
public Future<Post> findById(UUID id) {
Objects.requireNonNull(id, "id can not be null");
return client.preparedQuery("SELECT * FROM posts WHERE id=$1")
.map(iterator -> {
if (iterator.hasNext()) {
return MAPPER.apply(iterator.next());
throw new PostNotFoundException(id);
public void get(RoutingContext rc) {
var params = rc.pathParams();
var id = params.get("id");
post -> rc.response().end(Json.encode(post))
throwable -> rc.fail(throwable)
.failureHandler(frc -> {
Throwable failure = frc.failure();
if (failure instanceof PostNotFoundException) {
frc.response().setStatusCode(500).setStatusMessage("Server internal error:" + failure.getMessage()).end();
SchemaRouter schemaRouter = SchemaRouter.create(vertx, new SchemaRouterOptions());
SchemaParser schemaParser = SchemaParser.createDraft201909SchemaParser(schemaRouter);
ObjectSchemaBuilder bodySchemaBuilder = objectSchema()
.requiredProperty("title", stringSchema().with(minLength(5)).with(maxLength(100)))
.requiredProperty("content", stringSchema().with(minLength(10)).with(maxLength(2000)));
ValidationHandler validation = ValidationHandler.newInstance(
//.queryParameter(param("parameterName", intSchema()))
//.pathParameter(param("pathParam", numberSchema()))
Handler<RoutingContext> validationFailureHandler = (RoutingContext rc) -> {
if (rc.failure() instanceof BodyProcessorException exception) {
.end("validation failed.");




Self-employed technical consultant, solution architect and full-stack developer, open source contributor, freelancer and remote worker

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Saving Ubuntu Laptop Battery Life

AI-based Test Automation Tools. Part-3. Promises Delivered

Building a Complete Pipeline: The Essential Components of Continuous Testing [webinar]

Creating my open source dev blog

Hide/Unhide Android App Icon

Why product teams are better “on tap”

S.O.L.I.D Design Principles

All kind of responsibilities in a single interface. A bad design!

What are Bloom filters?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Self-employed technical consultant, solution architect and full-stack developer, open source contributor, freelancer and remote worker

More from Medium

Building Micronaut applications with Micronaut Mongo Reative

Photo by <a href=”https://unsplash.com/@fabianmardi?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Fabian Mardi</a> on <a href=”https://unsplash.com/s/photos/snow?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

How to build native CLI apps using Java, Maven, GraalVM, Picocli, JReleaser and GitHub actions

Spring Boot Tests with Kotlin and Embedded Kafka

Supporting Cross Node Interactive Queries In Kafka Streams