Skip to content

Commit 2da09d0

Browse files
committed
Add general exception.
1 parent 9cfbc8f commit 2da09d0

File tree

6 files changed

+74
-55
lines changed

6 files changed

+74
-55
lines changed

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

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.eigr.functions.protocol.actors.ActorOuterClass;
1010
import io.eigr.spawn.api.exceptions.ActorInvokeException;
1111
import io.eigr.spawn.api.exceptions.ActorNotFoundException;
12+
import io.eigr.spawn.api.exceptions.SpawnException;
1213
import io.eigr.spawn.internal.transport.client.SpawnClient;
1314

1415
import java.time.Duration;
@@ -47,7 +48,7 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
4748
* @return the ActorRef instance
4849
* @since 0.0.1
4950
*/
50-
protected static ActorRef of(SpawnClient client, String system, String name) throws Exception {
51+
protected static ActorRef of(SpawnClient client, String system, String name) throws SpawnException {
5152
ActorOuterClass.ActorId actorId = buildActorId(system, name);
5253
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
5354
if (Objects.nonNull(ref)) {
@@ -70,7 +71,7 @@ protected static ActorRef of(SpawnClient client, String system, String name) thr
7071
* @return the ActorRef instance
7172
* @since 0.0.1
7273
*/
73-
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws Exception {
74+
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws SpawnException {
7475
ActorOuterClass.ActorId actorId = buildActorId(system, name, parent);
7576
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
7677
if (Objects.nonNull(ref)) {
@@ -99,7 +100,7 @@ private static ActorOuterClass.ActorId buildActorId(String system, String name,
99100
.build();
100101
}
101102

102-
private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws Exception {
103+
private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws SpawnException {
103104
Protocol.SpawnRequest req = Protocol.SpawnRequest.newBuilder()
104105
.addActors(actorId)
105106
.build();
@@ -116,13 +117,10 @@ private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient clie
116117
* @return an Optional containing, or not, the response object to the Action call
117118
* @since 0.0.1
118119
*/
119-
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType) throws Exception {
120+
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType) throws SpawnException {
120121
Optional<T> res = invokeActor(action, Empty.getDefaultInstance(), outputType, Optional.empty());
121-
if (res.isPresent()) {
122-
return Optional.of(outputType.cast(res.get()));
123-
}
122+
return res.map(outputType::cast);
124123

125-
return res;
126124
}
127125

128126
/**
@@ -137,13 +135,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
137135
* @return an Optional containing, or not, the response object to the Action call
138136
* @since 0.0.1
139137
*/
140-
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType, InvocationOpts opts) throws Exception {
138+
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType, InvocationOpts opts) throws SpawnException {
141139
Optional<T> res = invokeActor(action, Empty.getDefaultInstance(), outputType, Optional.ofNullable(opts));
142-
if (res.isPresent()) {
143-
return Optional.of(outputType.cast(res.get()));
144-
}
140+
return res.map(outputType::cast);
145141

146-
return res;
147142
}
148143

149144
/**
@@ -157,13 +152,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
157152
* @return an Optional containing, or not, the response object to the Action call
158153
* @since 0.0.1
159154
*/
160-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType) throws Exception {
155+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType) throws SpawnException {
161156
Optional<T> res = invokeActor(action, value, outputType, Optional.empty());
162-
if (res.isPresent()) {
163-
return Optional.of(outputType.cast(res.get()));
164-
}
157+
return res.map(outputType::cast);
165158

166-
return res;
167159
}
168160

169161
/**
@@ -179,13 +171,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
179171
* @return an Optional containing, or not, the response object to the Action call
180172
* @since 0.0.1
181173
*/
182-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType, InvocationOpts opts) throws Exception {
174+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType, InvocationOpts opts) throws SpawnException {
183175
Optional<T> res = invokeActor(action, value, outputType, Optional.ofNullable(opts));
184-
if (res.isPresent()) {
185-
return Optional.of(outputType.cast(res.get()));
186-
}
176+
return res.map(outputType::cast);
187177

188-
return res;
189178
}
190179

191180
/**
@@ -196,7 +185,7 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
196185
* @param action name of the action to be called.
197186
* @since 0.0.1
198187
*/
199-
public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exception {
188+
public <T extends GeneratedMessageV3> void invokeAsync(String action) throws SpawnException {
200189
InvocationOpts opts = InvocationOpts.builder().async(true).build();
201190
invokeActor(action, Empty.getDefaultInstance(), null, Optional.of(opts));
202191
}
@@ -211,7 +200,7 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exc
211200
* Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
212201
* @since 0.0.1
213202
*/
214-
public <T extends GeneratedMessageV3> void invokeAsync(String action, InvocationOpts opts) throws Exception {
203+
public <T extends GeneratedMessageV3> void invokeAsync(String action, InvocationOpts opts) throws SpawnException {
215204
InvocationOpts mergedOpts = InvocationOpts.builder()
216205
.async(true)
217206
.delay(opts.getDelay())
@@ -230,7 +219,7 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action, Invocation
230219
* @param value the action argument object.
231220
* @since 0.0.1
232221
*/
233-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value) throws Exception {
222+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value) throws SpawnException {
234223
InvocationOpts opts = InvocationOpts.builder().async(true).build();
235224
invokeActor(action, value, null, Optional.of(opts));
236225
}
@@ -246,7 +235,7 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
246235
* Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
247236
* @since 0.0.1
248237
*/
249-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value, InvocationOpts opts) throws Exception {
238+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value, InvocationOpts opts) throws SpawnException {
250239
InvocationOpts mergedOpts = InvocationOpts.builder()
251240
.async(true)
252241
.delay(opts.getDelay())
@@ -281,21 +270,18 @@ public boolean isUnNamedActor() {
281270
}
282271

283272
private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invokeActor(
284-
String cmd, S argument, Class<T> outputType, Optional<InvocationOpts> options) throws Exception {
273+
String cmd, S argument, Class<T> outputType, Optional<InvocationOpts> options) throws SpawnException {
285274
Objects.requireNonNull(this.actorId, "ActorId cannot be null");
286275

287276
Protocol.InvocationRequest.Builder invocationRequestBuilder = Protocol.InvocationRequest.newBuilder();
288277

289-
if (options.isPresent()) {
290-
InvocationOpts opts = options.get();
278+
options.ifPresent(opts -> {
291279
invocationRequestBuilder.setAsync(opts.isAsync());
292-
293-
if (opts.getDelay().isPresent() && !opts.getScheduledTo().isPresent()) {
294-
invocationRequestBuilder.setScheduledTo(opts.getDelay().get());
295-
} else if (opts.getScheduledTo().isPresent()) {
296-
invocationRequestBuilder.setScheduledTo(opts.getScheduleTimeInLong());
297-
}
298-
}
280+
opts.getDelay().ifPresent(invocationRequestBuilder::setScheduledTo);
281+
// 'scheduledTo' override 'delay' if both is set.
282+
opts.getScheduledTo()
283+
.ifPresent(scheduleTo -> invocationRequestBuilder.setScheduledTo(opts.getScheduleTimeInLong()));
284+
});
299285

300286
final ActorOuterClass.Actor actorRef = ActorOuterClass.Actor.newBuilder()
301287
.setId(this.actorId)
@@ -318,13 +304,16 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
318304
case UNRECOGNIZED:
319305
String msg = String.format("Error when trying to invoke Actor %s. Details: %s",
320306
this.getActorName(), status.getMessage());
321-
322307
throw new ActorInvokeException(msg);
323308
case ACTOR_NOT_FOUND:
324309
throw new ActorNotFoundException();
325310
case OK:
326311
if (resp.hasValue() && Objects.nonNull(outputType)) {
327-
return Optional.of(resp.getValue().unpack(outputType));
312+
try {
313+
return Optional.of(resp.getValue().unpack(outputType));
314+
} catch (Exception e) {
315+
throw new SpawnException(e);
316+
}
328317
}
329318
return Optional.empty();
330319
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.eigr.spawn.api.exceptions;
22

3-
public final class ActorInvokeException extends IllegalStateException {
3+
public final class ActorInvokeException extends SpawnException {
44

55
public ActorInvokeException() {}
66
public ActorInvokeException(String message) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package io.eigr.spawn.api.exceptions;
22

3-
public final class ActorNotFoundException extends IllegalArgumentException{
3+
public final class ActorNotFoundException extends SpawnException {
44
}
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+
* Generic Spawn exception.
5+
*
6+
* @author Paulo H3nrique Alves
7+
*/
8+
public class SpawnException extends RuntimeException {
9+
10+
public SpawnException() {
11+
super();
12+
}
13+
14+
public SpawnException(String s) {
15+
super(s);
16+
}
17+
18+
public SpawnException(String message, Throwable cause) {
19+
super(message, cause);
20+
}
21+
22+
public SpawnException(Throwable cause) {
23+
super(cause);
24+
}
25+
}

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

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

33
import io.eigr.functions.protocol.Protocol;
4+
import io.eigr.spawn.api.exceptions.SpawnException;
45
import okhttp3.*;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

9+
import java.io.IOException;
810
import java.util.Objects;
911
import java.util.concurrent.TimeUnit;
1012

@@ -25,7 +27,7 @@ public OkHttpSpawnClient(String system, String proxyHost, int proxyPort) {
2527
this.system = system;
2628
this.proxyHost = proxyHost;
2729
this.proxyPort = proxyPort;
28-
this.client = new OkHttpClient.Builder()
30+
this.client = new OkHttpClient.Builder()
2931
.connectTimeout(30, TimeUnit.SECONDS)
3032
.readTimeout(30, TimeUnit.SECONDS)
3133
.writeTimeout(30, TimeUnit.SECONDS)
@@ -36,7 +38,7 @@ public OkHttpSpawnClient(String system, String proxyHost, int proxyPort) {
3638
}
3739

3840
@Override
39-
public Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws Exception {
41+
public Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws SpawnException {
4042
RequestBody body = RequestBody.create(registration.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
4143

4244
Request request = new Request.Builder().url(makeURLFrom(SPAWN_REGISTER_URI)).post(body).build();
@@ -49,12 +51,12 @@ public Protocol.RegistrationResponse register(Protocol.RegistrationRequest regis
4951
).bytes());
5052
} catch (Exception e) {
5153
log.error("Error registering Actors", e);
52-
throw new Exception(e);
54+
throw new SpawnException(e);
5355
}
5456
}
5557

5658
@Override
57-
public Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws Exception {
59+
public Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws SpawnException {
5860
RequestBody body = RequestBody.create(registration.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
5961

6062
Request request = new Request.Builder()
@@ -69,12 +71,12 @@ public Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws E
6971
).bytes());
7072
} catch (Exception e) {
7173
log.error("Error registering Actors", e);
72-
throw new Exception(e);
74+
throw new SpawnException(e);
7375
}
7476
}
7577

7678
@Override
77-
public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws Exception {
79+
public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws SpawnException {
7880
RequestBody body = RequestBody.create(
7981
request.toByteArray(), MediaType.parse(SPAWN_MEDIA_TYPE));
8082

@@ -84,10 +86,13 @@ public Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) th
8486
.build();
8587

8688
Call invocationCall = client.newCall(invocationRequest);
87-
Response callInvocationResponse = invocationCall.execute();
88-
89-
return Protocol.InvocationResponse
90-
.parseFrom(Objects.requireNonNull(callInvocationResponse.body()).bytes());
89+
try {
90+
Response callInvocationResponse = invocationCall.execute();
91+
return Protocol.InvocationResponse
92+
.parseFrom(Objects.requireNonNull(callInvocationResponse.body()).bytes());
93+
} catch (Exception e) {
94+
throw new SpawnException(e);
95+
}
9196
}
9297

9398
private String makeURLForSystemAndActor(String systemName, String actorName) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.eigr.spawn.internal.transport.client;
22

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

56
public interface SpawnClient {
67

7-
Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws Exception;
8-
9-
Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws Exception;
10-
Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws Exception;
8+
Protocol.RegistrationResponse register(Protocol.RegistrationRequest registration) throws SpawnException;
9+
Protocol.SpawnResponse spawn(Protocol.SpawnRequest registration) throws SpawnException;
10+
Protocol.InvocationResponse invoke(Protocol.InvocationRequest request) throws SpawnException;
1111
}
1212

0 commit comments

Comments
 (0)