Skip to content

Commit

Permalink
[Java] Cleanup exceptions (#3110)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy authored Oct 10, 2018
1 parent 286e4be commit 9049bf7
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package com.microsoft.signalr;

import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -197,12 +196,7 @@ private CompletableFuture<NegotiateResponse> handleNegotiate(String url) {
if (response.getStatusCode() != 200) {
throw new RuntimeException(String.format("Unexpected status code returned from negotiate: %d %s.", response.getStatusCode(), response.getStatusText()));
}
NegotiateResponse negotiateResponse;
try {
negotiateResponse = new NegotiateResponse(response.getContent());
} catch (IOException e) {
throw new RuntimeException(e);
}
NegotiateResponse negotiateResponse = new NegotiateResponse(response.getContent());

if (negotiateResponse.getError() != null) {
throw new RuntimeException(negotiateResponse.getError());
Expand Down Expand Up @@ -409,18 +403,18 @@ private void stopConnection(String errorMessage) {
*
* @param method The name of the server method to invoke.
* @param args The arguments to be passed to the method.
* @throws Exception If there was an error while sending.
*/
public void send(String method, Object... args) throws Exception {
public void send(String method, Object... args) {
if (hubConnectionState != HubConnectionState.CONNECTED) {
throw new HubException("The 'send' method cannot be called if the connection is not active");
throw new RuntimeException("The 'send' method cannot be called if the connection is not active");
}

InvocationMessage invocationMessage = new InvocationMessage(null, method, args);
sendHubMessage(invocationMessage);
}

public <T> Single<T> invoke(Class<T> returnType, String method, Object... args) throws Exception {
@SuppressWarnings("unchecked")
public <T> Single<T> invoke(Class<T> returnType, String method, Object... args) {
String id = connectionState.getNextInvocationId();
InvocationMessage invocationMessage = new InvocationMessage(id, method, args);

Expand Down Expand Up @@ -451,7 +445,7 @@ public <T> Single<T> invoke(Class<T> returnType, String method, Object... args)
return Single.fromFuture(future);
}

private void sendHubMessage(HubMessage message) throws Exception {
private void sendHubMessage(HubMessage message) {
String serializedMessage = protocol.writeMessage(message);
if (message.getMessageType() == HubMessageType.INVOCATION) {
logger.log(LogLevel.Debug, "Sending %s message '%s'.", message.getMessageType().name(), ((InvocationMessage)message).getInvocationId());
Expand Down Expand Up @@ -777,15 +771,15 @@ public Class<?> getReturnType(String invocationId) {
}

@Override
public List<Class<?>> getParameterTypes(String methodName) throws Exception {
public List<Class<?>> getParameterTypes(String methodName) {
List<InvocationHandler> handlers = connection.handlers.get(methodName);
if (handlers == null) {
logger.log(LogLevel.Warning, "Failed to find handler for '%s' method.", methodName);
return emptyArray;
}

if (handlers.isEmpty()) {
throw new Exception(String.format("There are no callbacks registered for the method '%s'.", methodName));
throw new RuntimeException(String.format("There are no callbacks registered for the method '%s'.", methodName));
}

return handlers.get(0).getClasses();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.microsoft.signalr;

public class HubException extends Exception {
public class HubException extends RuntimeException {
private static final long serialVersionUID = -572019264269821519L;

public HubException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface HubProtocol {
* @param message A string representation of one or more {@link HubMessage}s.
* @return A list of {@link HubMessage}s.
*/
HubMessage[] parseMessages(String message, InvocationBinder binder) throws Exception;
HubMessage[] parseMessages(String message, InvocationBinder binder);

/**
* Writes the specified {@link HubMessage} to a String.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

interface InvocationBinder {
Class<?> getReturnType(String invocationId);
List<Class<?>> getParameterTypes(String methodName) throws Exception;
List<Class<?>> getParameterTypes(String methodName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,105 +36,109 @@ public TransferFormat getTransferFormat() {
}

@Override
public HubMessage[] parseMessages(String payload, InvocationBinder binder) throws Exception {
public HubMessage[] parseMessages(String payload, InvocationBinder binder) {
if (payload != null && !payload.substring(payload.length() - 1).equals(RECORD_SEPARATOR)) {
throw new RuntimeException("Message is incomplete.");
}

String[] messages = payload.split(RECORD_SEPARATOR);
List<HubMessage> hubMessages = new ArrayList<>();
for (String str : messages) {
HubMessageType messageType = null;
String invocationId = null;
String target = null;
String error = null;
ArrayList<Object> arguments = null;
JsonArray argumentsToken = null;
Object result = null;
JsonElement resultToken = null;
JsonReader reader = new JsonReader(new StringReader(str));
reader.beginObject();

do {
String name = reader.nextName();
switch (name) {
case "type":
messageType = HubMessageType.values()[reader.nextInt() - 1];
break;
case "invocationId":
invocationId = reader.nextString();
break;
case "target":
target = reader.nextString();
break;
case "error":
error = reader.nextString();
break;
case "result":
if (invocationId == null) {
resultToken = jsonParser.parse(reader);
try {
for (String str : messages) {
HubMessageType messageType = null;
String invocationId = null;
String target = null;
String error = null;
ArrayList<Object> arguments = null;
JsonArray argumentsToken = null;
Object result = null;
JsonElement resultToken = null;
JsonReader reader = new JsonReader(new StringReader(str));
reader.beginObject();

do {
String name = reader.nextName();
switch (name) {
case "type":
messageType = HubMessageType.values()[reader.nextInt() - 1];
break;
case "invocationId":
invocationId = reader.nextString();
break;
case "target":
target = reader.nextString();
break;
case "error":
error = reader.nextString();
break;
case "result":
if (invocationId == null) {
resultToken = jsonParser.parse(reader);
} else {
result = gson.fromJson(reader, binder.getReturnType(invocationId));
}
break;
case "item":
reader.skipValue();
break;
case "arguments":
if (target != null) {
List<Class<?>> types = binder.getParameterTypes(target);
arguments = bindArguments(reader, types);
} else {
argumentsToken = (JsonArray)jsonParser.parse(reader);
}
break;
case "headers":
throw new RuntimeException("Headers not implemented yet.");
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
}
} while (reader.hasNext());

reader.endObject();
reader.close();

switch (messageType) {
case INVOCATION:
if (argumentsToken != null) {
List<Class<?>> types = binder.getParameterTypes(target);
arguments = bindArguments(argumentsToken, types);
}
if (arguments == null) {
hubMessages.add(new InvocationMessage(invocationId, target, new Object[0]));
} else {
result = gson.fromJson(reader, binder.getReturnType(invocationId));
hubMessages.add(new InvocationMessage(invocationId, target, arguments.toArray()));
}
break;
case "item":
reader.skipValue();
case COMPLETION:
if (resultToken != null) {
result = gson.fromJson(resultToken, binder.getReturnType(invocationId));
}
hubMessages.add(new CompletionMessage(invocationId, result, error));
break;
case "arguments":
if (target != null) {
List<Class<?>> types = binder.getParameterTypes(target);
arguments = bindArguments(reader, types);
case STREAM_INVOCATION:
case STREAM_ITEM:
case CANCEL_INVOCATION:
throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType));
case PING:
hubMessages.add(PingMessage.getInstance());
break;
case CLOSE:
if (error != null) {
hubMessages.add(new CloseMessage(error));
} else {
argumentsToken = (JsonArray)jsonParser.parse(reader);
hubMessages.add(new CloseMessage());
}
break;
case "headers":
throw new HubException("Headers not implemented yet.");
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
}
} while (reader.hasNext());

reader.endObject();
reader.close();

switch (messageType) {
case INVOCATION:
if (argumentsToken != null) {
List<Class<?>> types = binder.getParameterTypes(target);
arguments = bindArguments(argumentsToken, types);
}
if (arguments == null) {
hubMessages.add(new InvocationMessage(invocationId, target, new Object[0]));
} else {
hubMessages.add(new InvocationMessage(invocationId, target, arguments.toArray()));
}
break;
case COMPLETION:
if (resultToken != null) {
result = gson.fromJson(resultToken, binder.getReturnType(invocationId));
}
hubMessages.add(new CompletionMessage(invocationId, result, error));
break;
case STREAM_INVOCATION:
case STREAM_ITEM:
case CANCEL_INVOCATION:
throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType));
case PING:
hubMessages.add(PingMessage.getInstance());
break;
case CLOSE:
if (error != null) {
hubMessages.add(new CloseMessage(error));
} else {
hubMessages.add(new CloseMessage());
}
break;
default:
break;
}
} catch (IOException ex) {
throw new RuntimeException("Error reading JSON.", ex);
}

return hubMessages.toArray(new HubMessage[hubMessages.size()]);
Expand Down
Loading

0 comments on commit 9049bf7

Please sign in to comment.