Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example with jax-rs error mapper #2742

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright © 2023 Apple Inc. and the ServiceTalk project authors
tkountis marked this conversation as resolved.
Show resolved Hide resolved
*
* 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();
tkountis marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
Loading