Skip to content

Commit

Permalink
[master] AF-1226: Improve error handling displaying additional info t…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagobento committed Jul 12, 2018
1 parent 6c29c3e commit 05d0e52
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@

package org.jboss.errai.bus.client.api.builder;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jboss.errai.bus.client.api.BusErrorCallback;
import org.jboss.errai.bus.client.api.base.DefaultErrorCallback;
import org.jboss.errai.bus.client.api.base.MessageBuilder;
import org.jboss.errai.bus.client.api.messaging.Message;
import org.jboss.errai.bus.client.api.messaging.MessageBus;
import org.jboss.errai.bus.client.api.messaging.MessageCallback;
import org.jboss.errai.bus.client.framework.AbstractRpcProxy;
import org.jboss.errai.common.client.api.Assert;
import org.jboss.errai.common.client.api.ErrorCallback;
import org.jboss.errai.common.client.api.RemoteCallback;
Expand All @@ -37,6 +31,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Facilitates the building of a remote call. Ensures that the remote call is constructed properly.
* <p/>
Expand Down Expand Up @@ -86,62 +85,49 @@ public <T, R> T call(final RemoteCallback<R> callback, final BusErrorCallback er
public RemoteCallEndpointDef call(final String serviceName) {
message.toSubject(serviceName + ":RPC");

final RemoteCallSendable sendable = new RemoteCallSendable() {
final RemoteCallSendable sendable = bus -> {
final Integer id;

@Override
public void sendNowWith(final MessageBus bus) {
final Integer id;

final String rpcMethod = message.getCommandType();
final String replyTo =
message.getSubject() + "." + rpcMethod + ":" + (id = uniqueNumber()) + ":RespondTo:RPC";

final String errorTo =
message.getSubject() + "." + rpcMethod + ":" + id + ":Errors:RPC";

if (remoteCallback != null) {
bus.subscribe(replyTo,
new MessageCallback() {
@Override
public void callback(final Message message) {
logger.debug("Received RPC response: [service={}, endpoint={}]", serviceName, rpcMethod);
bus.unsubscribeAll(replyTo);
if (DefaultRemoteCallBuilder.this.message.getErrorCallback() != null) {
bus.unsubscribeAll(errorTo);
}
remoteCallback.callback(message.get(responseType, "MethodReply"));
}
}
);
message.set(MessageParts.ReplyTo, replyTo);
}
final String rpcMethod = message.getCommandType();
final String replyTo = message.getSubject() + "." + rpcMethod + ":" + (id = uniqueNumber()) + ":RespondTo:RPC";
final String errorTo = message.getSubject() + "." + rpcMethod + ":" + id + ":Errors:RPC";

if (message.getErrorCallback() != null) {
bus.subscribe(errorTo,
new MessageCallback() {
@Override
public void callback(final Message m) {
logger.debug("Received RPC error: [service={}, endpoint={}]", serviceName, rpcMethod);
bus.unsubscribeAll(errorTo);
if (remoteCallback != null) {
bus.unsubscribeAll(replyTo);
}
message.set(MessageParts.AdditionalDetails, m.get(String.class, MessageParts.AdditionalDetails));
final Throwable throwable = m.get(Throwable.class, MessageParts.Throwable);
final boolean defaultErrorHandling = message.getErrorCallback().error(message,
throwable);
if (defaultErrorHandling) {
DefaultErrorCallback.INSTANCE.error(message, throwable);
}
}
}
);
message.set(MessageParts.ErrorTo, errorTo);
}
if (remoteCallback != null) {
bus.subscribe(replyTo, m -> {
logger.debug("Received RPC response: [service={}, endpoint={}]", serviceName, rpcMethod);
bus.unsubscribeAll(replyTo);
if (message.getErrorCallback() != null) {
bus.unsubscribeAll(errorTo);
}
try {
//Prevent ErraiBus to handle RPC success callback exceptions.
remoteCallback.callback(m.get(responseType, "MethodReply"));
} catch (final Exception e) {
AbstractRpcProxy.DEFAULT_RPC_ERROR_CALLBACK.error(m, new RuntimeException("Client-side exception occurred although RPC call succeeded.", e));
}
});
message.set(MessageParts.ReplyTo, replyTo);
}

logger.debug("Sending RPC request: [service={}, endpoint={}]", serviceName, rpcMethod);
message.sendNowWith(bus);
if (message.getErrorCallback() != null) {
bus.subscribe(errorTo, m -> {
logger.debug("Received RPC error: [service={}, endpoint={}]", serviceName, rpcMethod);
bus.unsubscribeAll(errorTo);
if (remoteCallback != null) {
bus.unsubscribeAll(replyTo);
}
message.set(MessageParts.AdditionalDetails, m.get(String.class, MessageParts.AdditionalDetails));
final Throwable throwable = m.get(Throwable.class, MessageParts.Throwable);
final boolean shouldPerformDefaultErrorHandling = message.getErrorCallback().error(message, throwable);
if (shouldPerformDefaultErrorHandling) {
DefaultErrorCallback.INSTANCE.error(message, throwable);
}
});
message.set(MessageParts.ErrorTo, errorTo);
}

logger.debug("Sending RPC request: [service={}, endpoint={}]", serviceName, rpcMethod);
message.sendNowWith(bus);
};

final RemoteCallErrorDef errorDef = new RemoteCallErrorDef() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jboss.errai.bus.client.api.base.DefaultErrorCallback;
import org.jboss.errai.bus.client.api.base.MessageBuilder;
import org.jboss.errai.bus.client.api.builder.RemoteCallSendable;
import org.jboss.errai.bus.client.api.messaging.Message;
import org.jboss.errai.bus.client.api.messaging.MessageBus;
import org.jboss.errai.common.client.api.ErrorCallback;
import org.jboss.errai.common.client.api.RemoteCallback;
Expand All @@ -39,16 +40,13 @@
@SuppressWarnings("rawtypes")
public abstract class AbstractRpcProxy implements RpcStub {

private static final ErrorCallback defaultErrorCallback = new ErrorCallback() {
@Override
public boolean error(Object message, Throwable throwable) {
invokeDefaultErrorHandlers(throwable);
return false;
}
public static ErrorCallback<Message> DEFAULT_RPC_ERROR_CALLBACK = (message, throwable) -> {
invokeDefaultErrorHandlers(throwable);
return false;
};

protected RemoteCallback remoteCallback;
protected ErrorCallback errorCallback = defaultErrorCallback;
protected ErrorCallback errorCallback = DEFAULT_RPC_ERROR_CALLBACK;
protected Annotation[] qualifiers;
protected RpcBatch batch;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,37 @@ public static void consumeEventFromMessage(final Message message) {
private static void _fireEvent(final String beanType, final Message message) {
if (eventObservers.containsKey(beanType)) {
for (final MessageCallback callback : new ArrayList<MessageCallback>(eventObservers.get(beanType))) {
fireIfNotFired(callback, message);
try {
fireIfNotFired(callback, message);
} catch (final Exception e) {
final String potentialTarget = callbackOwnerClass(callback);
String actualTarget = potentialTarget.equalsIgnoreCase("undefined.undefined") ? "[unavailable]" : potentialTarget;

throw new RuntimeException("CDI Event exception: " + message + " sent to " + actualTarget, e);
}
}
}
}

private static native String callbackOwnerClass(final Object o) /*-{
var pkg, clazzName;
for (var protoKey in o.__proto__) {
if (protoKey.startsWith("___clazz")) {
for (var clazzKey in o[protoKey]) {
if (clazzKey.startsWith("package")) {
pkg = o[protoKey][clazzKey];
}
if (clazzKey.startsWith("compound")) {
clazzName = o[protoKey][clazzKey];
}
}
}
}
return pkg + "." + clazzName;
}-*/;

@SuppressWarnings("unchecked")
private static void fireIfNotFired(final MessageCallback callback, final Message message) {
if (!message.hasResource(CLIENT_ALREADY_FIRED_RESOURCE)) {
Expand Down

0 comments on commit 05d0e52

Please sign in to comment.