Using Quarkus to throw status code with message

2.7k views Asked by At

Hello i'm trying to learn Quarkus but am unable to find out how to return a error message to the client

    @POST
    public Uni<Response> create(Fruit fruit) {
        if (Fruit.find(fruit.name) != null) {
            throw new WebApplicationException(fruit.name + " already exists", 422);
        }

        return Panache.<Fruit>withTransaction(fruit::persist)
                .onItem()
                .transform(inserted -> Response
                        .created(URI.create("/fruits" + inserted.id))
                        .build());
    }

I have the following code block what i want is for the

fruit.name + " already exists

to be printed to the client when they try to POST. But I only get the 422 error code. How can I also send the message back to the client?

2

There are 2 answers

0
geoand On

You can use throw new WebApplicationException(Response.status(422).entity(fruit.name + " already exists").build())

0
Devenol On
if (Fruit.find(fruit.name) != null) {
    return Uni.createFrom().item(Response.status(422).entity(fruit.name + " already exists").build());
}