Skip to content

Commit

Permalink
Rest Client Reactive: unwrap exceptions set by mappers in async
Browse files Browse the repository at this point in the history
  • Loading branch information
michalszynkiewicz committed Aug 17, 2021
1 parent 5372f5a commit bed695a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.CONSUMES;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.OBJECT;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.UNI;
import static org.jboss.resteasy.reactive.common.processor.ResteasyReactiveDotNames.WEB_APPLICATION_EXCEPTION;

import java.io.Closeable;
import java.io.File;
Expand All @@ -17,7 +16,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -1292,23 +1291,25 @@ private void handleReturn(ClassInfo restClientInterface, String defaultMediaType
String mediaTypeValue = defaultMediaType;

// if a JAXRS method throws an exception, unwrap the ProcessingException and throw the exception instead
// Similarly with WebApplicationException
// Similarly with RuntimeException's
TryBlock tryBlock = methodCreator.tryBlock();

List<Type> exceptionTypes = jandexMethod.exceptions();
Set<DotName> exceptions = new HashSet<>();
exceptions.add(WEB_APPLICATION_EXCEPTION);
Set<String> exceptions = new LinkedHashSet<>();
for (Type exceptionType : exceptionTypes) {
exceptions.add(exceptionType.name());
exceptions.add(exceptionType.name().toString());
}
if (!exceptions.contains(Exception.class.getName()) && !exceptions.contains(Throwable.class.getName())) {
exceptions.add(RuntimeException.class.getName());
}

CatchBlockCreator catchBlock = tryBlock.addCatch(ProcessingException.class);
ResultHandle caughtException = catchBlock.getCaughtException();
ResultHandle cause = catchBlock.invokeVirtualMethod(
MethodDescriptor.ofMethod(Throwable.class, "getCause", Throwable.class),
caughtException);
for (DotName exception : exceptions) {
catchBlock.ifTrue(catchBlock.instanceOf(cause, exception.toString()))
for (String exception : exceptions) {
catchBlock.ifTrue(catchBlock.instanceOf(cause, exception))
.trueBranch().throwException(cause);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.util.List;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
Expand All @@ -12,6 +11,7 @@
import org.jboss.resteasy.reactive.client.handlers.ClientResponseCompleteRestHandler;
import org.jboss.resteasy.reactive.client.impl.ClientRequestContextImpl;
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
import org.jboss.resteasy.reactive.common.core.UnwrappableException;
import org.jboss.resteasy.reactive.common.jaxrs.ResponseImpl;

public class MicroProfileRestClientResponseFilter implements ClientResponseFilter {
Expand All @@ -35,7 +35,7 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
ResponseImpl response = ClientResponseCompleteRestHandler.mapToResponse(restClientContext, false);
Throwable throwable = exceptionMapper.toThrowable(response);
if (throwable != null) {
throw new ProcessingException(throwable);
throw new UnwrappableException(throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,30 @@ public H[] getHandlers() {
*/
public void handleException(Throwable t) {
if (handlers == abortHandlerChain) {
handleUnrecoverableError(t);
handleUnrecoverableError(unwrapException(t));
} else {
this.throwable = t;
this.throwable = unwrapException(t);
restart(abortHandlerChain);
}
}

public void handleException(Throwable t, boolean keepSameTarget) {
if (handlers == abortHandlerChain) {
handleUnrecoverableError(t);
handleUnrecoverableError(unwrapException(t));
} else {
this.throwable = t;
this.throwable = unwrapException(t);
restart(abortHandlerChain, keepSameTarget);
}
}

private Throwable unwrapException(Throwable t) {
if (t instanceof UnwrappableException) {
return t.getCause();
}

return t;
}

protected abstract void handleUnrecoverableError(Throwable throwable);

public Object getProperty(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jboss.resteasy.reactive.common.core;

import javax.ws.rs.ProcessingException;

public class UnwrappableException extends ProcessingException {
public UnwrappableException(Throwable cause) {
super(cause);
}
}

0 comments on commit bed695a

Please sign in to comment.