Skip to content

Commit

Permalink
Example with jax-rs error mapper (#2742)
Browse files Browse the repository at this point in the history
Motivation:

Add an example of how error mapping can be done with jax-rs to help users debug development and/or not issues.

Results:

Better example base
  • Loading branch information
tkountis committed Apr 11, 2024
1 parent 82a5ce2 commit 55501d3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicetalk.examples.http.jaxrs;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class HelloWorldExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(final Throwable exception) {
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 @@ -31,7 +31,8 @@ public final class HelloWorldJaxRsApplication extends Application {
public Set<Class<?>> getClasses() {
return new HashSet<>(asList(
MultiPartFeature.class,
HelloWorldJaxRsResource.class
HelloWorldJaxRsResource.class,
HelloWorldExceptionMapper.class
)
);
}
Expand Down
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 Single.<String>failed(mapped ? new IllegalStateException() : new Exception()).toCompletionStage();
}

/**
* 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 55501d3

Please sign in to comment.