Skip to content

Commit

Permalink
better example
Browse files Browse the repository at this point in the history
  • Loading branch information
tkountis committed Apr 11, 2024
1 parent 3d148b5 commit bd182c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
public class HelloWorldExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(final Throwable exception) {
exception.printStackTrace();
if (exception instanceof IllegalStateException) {
return Response.status(Response.Status.BAD_REQUEST).build();
}

return Response.serverError().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ public CompletionStage<String> slowHello(@DefaultValue("world") @QueryParam("who
return delayedResponse;
}

/**
* Resource that uses Java's CompletionStage to produce an error response.
* Note that the {@link ConnectionContext} could also be injected into a class-level {@code @Context} field.
* <p>
* Test with:
* <pre>
* curl -v http://localhost:8080/greetings/error-hello
* curl -v http://localhost:8080/greetings/error-hello?mapped=false
* </pre>
*
* @param mapped whether the exception is mapped or not.
* @param ctx the {@link ConnectionContext}.
* @return future greetings as a {@link CompletionStage} of {@link String}.
*/
@GET
@Path("error-hello")
@Produces(TEXT_PLAIN)
public CompletionStage<String> errorHello(@DefaultValue("true") @QueryParam("mapped") final boolean mapped,
@Context final ConnectionContext ctx) {
return CompletableFuture.failedFuture(mapped ? new IllegalStateException() : new Exception());
}

/**
* Resource that only relies on {@link Single}s for consuming and producing data, and operators for processing it.
* No OIO adaptation is involved when requests are dispatched to it,
Expand Down

0 comments on commit bd182c9

Please sign in to comment.