Custom Exception Handling in Quarkus REST API

Hello everyone, In this article, we will learn how to handle exceptions in the Quarkus REST application using ExceptionMapper interface implementations.
ExceptionMapper is a contract for a provider that will map a thrown application exception to a Response object.


We will show you custom exception handling with the help of a simple application,

Project Directory:



application.properties

knowledgefactory.custom.error.msg.usernotfound = User not found
knowledgefactory.custom.error.msg.badrequest.numeric = User Id must be numeric



Create Error Message

package org.knf.dev.demo.exception;

public class ErrorMessage {

private String message;
private Boolean status;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

public ErrorMessage(String message, Boolean status) {
super();
this.message = message;
this.status = status;
}

public ErrorMessage() {
super();
}
}


Create the Custom Exception Handler

package org.knf.dev.demo.exception;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class CustomExceptionHandler
implements ExceptionMapper<CustomException> {

@ConfigProperty(name = "knowledgefactory.custom.error.msg.usernotfound")
String userNotFound;

@Override
public Response toResponse(CustomException e) {

if (e.getMessage().equalsIgnoreCase(userNotFound)) {
return Response.status(Response.Status.NOT_FOUND).
entity(new ErrorMessage(e.getMessage(), false))
.build();
} else {

return Response.status(Response.Status.BAD_REQUEST).
entity(new ErrorMessage(e.getMessage(), false))
.build();
}
}
}


Create the Custom Exception

package org.knf.dev.demo.exception;

import java.io.Serializable;

public class CustomException extends
RuntimeException implements Serializable {

private static final long serialVersionUID = 1L;

public CustomException() {
}

public CustomException(String message) {
super(message);
}

public CustomException(String message, Throwable cause) {
super(message, cause);
}

public CustomException(Throwable cause) {
super(cause);
}

public CustomException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {

super(message, cause, enableSuppression, writableStackTrace);
}
}


Create User Pojo

package org.knf.dev.demo.data;

public class User {

private String name;
private String email;

public User(String name, String email) {
this.name = name;
this.email = email;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}


Create a dummy data generator

package org.knf.dev.demo.data;

import javax.inject.Singleton;
import java.util.HashMap;
import java.util.Map;

@Singleton
public
class UserData {

public User getUserById(Long id) {
User user = genearteDummyData().get(id);
return user;
}

//Generate Dummy Users
private Map<Long, User> genearteDummyData() {
Map<Long, User> dummyUsers = new HashMap<>();
User user1 = new User("user-1", "user-1@gmail.com");
User user2 = new User("user2", "user2@gmail.com");
User user3 = new User("user3", "user3@gmail.com");
dummyUsers.put(22l, user1);
dummyUsers.put(13l, user2);
dummyUsers.put(19l, user3);
return dummyUsers;
}
}



Create the User Controller 

package org.knf.dev.demo.controller;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.knf.dev.demo.data.User;
import org.knf.dev.demo.data.UserData;
import org.knf.dev.demo.exception.CustomException;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/api/v1")
public class UserController {

@ConfigProperty(name = "knowledgefactory.custom.error.msg.badrequest.numeric")
String idNumericErrorMsg;

@ConfigProperty(name = "knowledgefactory.custom.error.msg.usernotfound")
String userNotFound;

@Inject
UserData userData;

@GET
@Path("/users/{id}")
public Response findUserById(@PathParam("id") String id)
throws CustomException {
Long user_id = null;
try {
user_id = Long.parseLong(id);
} catch (NumberFormatException e) {
throw new CustomException(idNumericErrorMsg);
}
User user = userData.getUserById(user_id);
if (user == null) {
throw new CustomException(userNotFound);
}
return Response.ok().
entity(userData.getUserById(user_id)).build();
}
}


Quarkus Exception Handling demo

Build application  jar file: mvn clean package

Start the application: java -jar quarkus-run.jar


Invalid Request (User Id not found):


Invalid Request(User Id must be numeric):


Valid request:

 



More related topics,

Kotlin + Quarkus

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Spring boot video streaming example-HTML5

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + OpenCSV Export Data to CSV Example

Java - Blowfish Encryption and decryption Example