Search code examples
javaapihttp-status-code-404responsequarkus

How to throw NotFoundException with message body in Java Quarkus


I currently have the problem, that I am looking for an artifact in Jenkins. If this artifact can not be found, then a 404 should come back. So far this works quite well. Unfortunately, I get no message and only the ErrorCode 404 back. I would like to return a message with more information.

Here's my code.

Endpoint:

@GET
@Path(API_RESOURCE_IMAGE_REPORT)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
@Operation(summary = "", description = "")
@APIResponses(
        value = {
                @APIResponse(
                        responseCode = "200",
                        description =
                                "Request successful",
                        content = @Content(mediaType = MediaType.TEXT_HTML)),
                @APIResponse(
                        responseCode = "404",
                        description = "Resource not found ",
                        content =
                        @Content(
                                mediaType = MediaType.APPLICATION_JSON,
                                schema = @Schema(implementation = NotFoundException.class))),
        })
public Response getReport(@Parameter(
        description = "",
        required = true)
                          @PathParam("imageName") final String imageName,
                          @Parameter(description = "", required = true)
                          @PathParam("tag") final String tag,
                          @Parameter(description = "")
                          @PathParam("type") String type
) {

    InputStream report = jenkinsClient.getReport(imageName, tag, type);
    
    return Response.status(HttpURLConnection.HTTP_ACCEPTED).entity(report).build();
}

Jenkinsclient:

public InputStream getReport(final String imageName, final String tag, final String type) throws NotFoundException {

        try {
            final int lastSuccessfulBuildnumber = jenkinsClient.api().jobsApi().jobInfo(imageName, tag).lastSuccessfulBuild().number();
            LOG.info("Last successful buildnumber: " + lastSuccessfulBuildnumber);

            final InputStream report = jenkinsClient.api().jobsApi().artifact(imageName, tag, lastSuccessfulBuildnumber, Objects.equals(type, "image") ? "trivy_image_report.html" : "trivy_Dockerfile_report.html");

            if (report == null) {
                throw new NotFoundException("No dockerfile or image report found");
            }

            return report;

        } catch (Exception e) {
            throw new NotFoundException("No dockerfile or image scan report found");

        }
    }

I expected a 404 with a the message "No dockerfile or image scan report found". But i got only 404 without a message when i dont find an artefact. I used the "javax.ws.rs.NotFoundException" for NotFoundException.

Thanks for help


Solution

  • Thanks to Nikos Paraskevopoulos in the comments. Ive added the response status in the Exception like this:

    throw new NotFoundException(Response.status(HttpURLConnection.HTTP_NOT_FOUND).entity("your message").build());
    

    this worked fine and i have the message in the response body

    response 404