Skip to content

Commit 20b8dab

Browse files
committed
Exceptions refactoring.
1 parent e40d2d5 commit 20b8dab

File tree

10 files changed

+119
-42
lines changed

10 files changed

+119
-42
lines changed

src/main/java/io/eigr/spawn/api/ActorRef.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import com.google.protobuf.GeneratedMessageV3;
88
import io.eigr.functions.protocol.Protocol;
99
import io.eigr.functions.protocol.actors.ActorOuterClass;
10-
import io.eigr.spawn.api.exceptions.ActorInvokeException;
10+
import io.eigr.spawn.api.exceptions.ActorCreationException;
11+
import io.eigr.spawn.api.exceptions.ActorInvocationException;
1112
import io.eigr.spawn.api.exceptions.ActorNotFoundException;
1213
import io.eigr.spawn.api.exceptions.SpawnException;
1314
import io.eigr.spawn.internal.transport.client.SpawnClient;
@@ -50,7 +51,7 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
5051
* @return the ActorRef instance
5152
* @since 0.0.1
5253
*/
53-
protected static ActorRef of(SpawnClient client, String system, String name) throws SpawnException {
54+
protected static ActorRef of(SpawnClient client, String system, String name) throws ActorCreationException {
5455
ActorOuterClass.ActorId actorId = buildActorId(system, name);
5556
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
5657
if (Objects.nonNull(ref)) {
@@ -73,7 +74,7 @@ protected static ActorRef of(SpawnClient client, String system, String name) thr
7374
* @return the ActorRef instance
7475
* @since 0.0.1
7576
*/
76-
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws SpawnException {
77+
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws ActorCreationException {
7778
ActorOuterClass.ActorId actorId = buildActorId(system, name, parent);
7879
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
7980
if (Objects.nonNull(ref)) {
@@ -102,7 +103,7 @@ private static ActorOuterClass.ActorId buildActorId(String system, String name,
102103
.build();
103104
}
104105

105-
private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws SpawnException {
106+
private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws ActorCreationException {
106107
Protocol.SpawnRequest req = Protocol.SpawnRequest.newBuilder()
107108
.addActors(actorId)
108109
.build();
@@ -311,15 +312,15 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
311312
case UNRECOGNIZED:
312313
String msg = String.format("Error when trying to invoke Actor %s. Details: %s",
313314
this.getActorName(), status.getMessage());
314-
throw new ActorInvokeException(msg);
315+
throw new ActorInvocationException(msg);
315316
case ACTOR_NOT_FOUND:
316-
throw new ActorNotFoundException();
317+
throw new ActorNotFoundException("Actor not found.");
317318
case OK:
318319
if (resp.hasValue() && Objects.nonNull(outputType)) {
319320
try {
320321
return Optional.of(resp.getValue().unpack(outputType));
321322
} catch (Exception e) {
322-
throw new SpawnException(e);
323+
throw new ActorInvocationException("Error handling response.", e);
323324
}
324325
}
325326
return Optional.empty();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.eigr.spawn.api.exceptions;
2+
3+
/**
4+
* Actor Creation Exception.
5+
*
6+
* @author Paulo H3nrique Alves
7+
*/
8+
public class ActorCreationException extends SpawnException {
9+
10+
public ActorCreationException() {
11+
super();
12+
}
13+
14+
public ActorCreationException(String s) {
15+
super(s);
16+
}
17+
18+
public ActorCreationException(String message, Throwable cause) {
19+
super(message, cause);
20+
}
21+
22+
public ActorCreationException(Throwable cause) {
23+
super(cause);
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.eigr.spawn.api.exceptions;
2+
3+
public final class ActorInvocationException extends SpawnException {
4+
5+
public ActorInvocationException() {
6+
super();
7+
}
8+
9+
public ActorInvocationException(String message) {
10+
super(message);
11+
}
12+
13+
public ActorInvocationException(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public ActorInvocationException(Throwable cause) {
18+
super(cause);
19+
}
20+
}

src/main/java/io/eigr/spawn/api/exceptions/ActorInvokeException.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
package io.eigr.spawn.api.exceptions;
22

33
public final class ActorNotFoundException extends SpawnException {
4+
5+
public ActorNotFoundException() {
6+
super();
7+
}
8+
9+
public ActorNotFoundException(String s) {
10+
super(s);
11+
}
12+
13+
public ActorNotFoundException(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public ActorNotFoundException(Throwable cause) {
18+
super(cause);
19+
}
420
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.eigr.spawn.api.exceptions;
2+
3+
/**
4+
* Actor Registration Exception.
5+
*
6+
* @author Paulo H3nrique Alves
7+
*/
8+
public class ActorRegistrationException extends SpawnException {
9+
10+
public ActorRegistrationException() {
11+
super();
12+
}
13+
public ActorRegistrationException(String s) {
14+
super(s);
15+
}
16+
17+
public ActorRegistrationException(String message, Throwable cause) {
18+
super(message, cause);
19+
}
20+
21+
public ActorRegistrationException(Throwable cause) {
22+
super(cause);
23+
}
24+
}

src/main/java/io/eigr/spawn/api/exceptions/SpawnException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*
66
* @author Paulo H3nrique Alves
77
*/
8-
public class SpawnException extends RuntimeException {
8+
public class SpawnException extends Exception {
99

10-
public SpawnException() {
10+
protected SpawnException() {
1111
super();
1212
}
1313

src/main/java/io/eigr/spawn/internal/transport/client/OkHttpSpawnClient.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.eigr.spawn.internal.transport.client;
22

33
import io.eigr.functions.protocol.Protocol;
4-
import io.eigr.spawn.api.exceptions.SpawnException;
4+
import io.eigr.spawn.api.exceptions.ActorCreationException;
5+
import io.eigr.spawn.api.exceptions.ActorInvocationException;
6+
import io.eigr.spawn.api.exceptions.ActorRegistrationException;
57
import okhttp3.*;
68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810

9-
import java.io.IOException;
1011
import java.util.Objects;
1112
import java.util.concurrent.TimeUnit;
1213

@@ -38,7 +39,7 @@ public OkHttpSpawnClient(String system, String proxyHost, int proxyPort) {
3839
}
3940

4041
@Override
41-
public Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws SpawnException {
42+
public Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws ActorRegistrationException {
4243
RequestBody body = RequestBody.create(registration.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
4344

4445
Request request = new Request.Builder().url(makeURLFrom(SPAWN_REGISTER_URI)).post(body).build();
@@ -50,13 +51,12 @@ public Protocol.RegistrationResponse register(Protocol.RegistrationRequest regis
5051
Objects.requireNonNull(response.body()
5152
).bytes());
5253
} catch (Exception e) {
53-
log.error("Error registering Actors", e);
54-
throw new SpawnException(e);
54+
throw new ActorRegistrationException("Error registering Actors", e);
5555
}
5656
}
5757

5858
@Override
59-
public Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws SpawnException {
59+
public Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws ActorCreationException {
6060
RequestBody body = RequestBody.create(registration.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
6161

6262
Request request = new Request.Builder()
@@ -70,13 +70,12 @@ public Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws S
7070
Objects.requireNonNull(response.body()
7171
).bytes());
7272
} catch (Exception e) {
73-
log.error("Error registering Actors", e);
74-
throw new SpawnException(e);
73+
throw new ActorCreationException("Error registering Actors", e);
7574
}
7675
}
7776

7877
@Override
79-
public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws SpawnException {
78+
public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws ActorInvocationException {
8079
RequestBody body = RequestBody.create(
8180
request.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
8281

@@ -86,12 +85,12 @@ public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) th
8685
.build();
8786

8887
Call invocationCall = client.newCall(invocationRequest);
89-
try {
90-
Response callInvocationResponse = invocationCall.execute();
88+
try (Response callInvocationResponse = invocationCall.execute()){
89+
assert callInvocationResponse.body() != null;
9190
return Protocol.InvocationResponse
9291
.parseFrom(Objects.requireNonNull(callInvocationResponse.body()).bytes());
9392
} catch (Exception e) {
94-
throw new SpawnException(e);
93+
throw new ActorInvocationException(e);
9594
}
9695
}
9796

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.eigr.spawn.internal.transport.client;
22

33
import io.eigr.functions.protocol.Protocol;
4+
import io.eigr.spawn.api.exceptions.ActorCreationException;
45
import io.eigr.spawn.api.exceptions.SpawnException;
56

67
public interface SpawnClient {
78

89
Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws SpawnException;
9-
Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws SpawnException;
10+
Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws ActorCreationException;
1011
Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws SpawnException;
1112
}
1213

src/main/java/io/eigr/spawn/internal/transport/server/ActorServiceHandler.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import io.eigr.spawn.api.actors.workflows.Forward;
1717
import io.eigr.spawn.api.actors.workflows.Pipe;
1818
import io.eigr.spawn.api.actors.workflows.SideEffect;
19-
import io.eigr.spawn.api.exceptions.ActorInvokeException;
19+
import io.eigr.spawn.api.exceptions.ActorInvocationException;
2020
import io.eigr.spawn.internal.Entity;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
@@ -62,8 +62,8 @@ public void handle(HttpExchange exchange) throws IOException {
6262
log.debug("Received Actor Action Request.");
6363

6464
if ("POST".equals(exchange.getRequestMethod())) {
65-
Protocol.ActorInvocationResponse response = handleRequest(exchange);
6665
try (OutputStream os = exchange.getResponseBody()) {
66+
Protocol.ActorInvocationResponse response = handleRequest(exchange);
6767
byte[] bytes = response.toByteArray();
6868
exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE);
6969
exchange.sendResponseHeaders(200, bytes.length);
@@ -116,13 +116,13 @@ private Protocol.ActorInvocationResponse handleRequest(HttpExchange exchange) th
116116
}
117117

118118
} catch (Exception e) {
119-
log.error("Error during handle request. Error: {}", e);
119+
log.error("Error during handle request.", e);
120120
}
121121

122-
throw new ActorInvokeException("Action result is null");
122+
throw new IOException("Action result is null");
123123
}
124124

125-
private Optional<Value> callAction(String system, String actor, String parent, String commandName, Any value, Protocol.Context context) {
125+
private Optional<Value> callAction(String system, String actor, String parent, String commandName, Any value, Protocol.Context context) throws ActorInvocationException {
126126
Optional<Entity> optionalEntity = getEntityByActor(actor, parent);
127127
if (optionalEntity.isPresent()) {
128128
Entity entity = optionalEntity.get();
@@ -142,7 +142,7 @@ private Optional<Value> callAction(String system, String actor, String parent, S
142142
} else if (entity.getTimerActions().containsKey(commandName)) {
143143
entityMethod = entity.getTimerActions().get(commandName);
144144
} else {
145-
throw new ActorInvokeException(
145+
throw new ActorInvocationException(
146146
String.format("The Actor does not have the desired action: %s", commandName));
147147
}
148148

@@ -164,12 +164,12 @@ private Optional<Value> callAction(String system, String actor, String parent, S
164164
final Object unpack = value.unpack(inputType);
165165
return Optional.of((Value) actorMethod.invoke(actorRef, unpack, actorContext));
166166
}
167-
} catch (IllegalAccessException e) {
168-
throw new RuntimeException(e);
167+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
168+
throw new ActorInvocationException(e);
169169
} catch (InvalidProtocolBufferException e) {
170-
throw new RuntimeException(e);
171-
} catch (InvocationTargetException | NoSuchMethodException | InstantiationException e) {
172-
throw new RuntimeException(e);
170+
throw new ActorInvocationException(e);
171+
} catch (NoSuchMethodException | InstantiationException e) {
172+
throw new ActorInvocationException(e);
173173
}
174174
}
175175

0 commit comments

Comments
 (0)