Skip to content

Commit 9c7b77e

Browse files
authored
Merge pull request #3 from h3nrique/main
Add general exception
2 parents eacdf72 + 492c4b0 commit 9c7b77e

File tree

12 files changed

+189
-93
lines changed

12 files changed

+189
-93
lines changed

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

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
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;
13+
import io.eigr.spawn.api.exceptions.SpawnException;
1214
import io.eigr.spawn.internal.transport.client.SpawnClient;
1315

1416
import java.time.Duration;
@@ -49,7 +51,7 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
4951
* @return the ActorRef instance
5052
* @since 0.0.1
5153
*/
52-
protected static ActorRef of(SpawnClient client, String system, String name) throws Exception {
54+
protected static ActorRef of(SpawnClient client, String system, String name) throws ActorCreationException {
5355
ActorOuterClass.ActorId actorId = buildActorId(system, name);
5456
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
5557
if (Objects.nonNull(ref)) {
@@ -72,7 +74,7 @@ protected static ActorRef of(SpawnClient client, String system, String name) thr
7274
* @return the ActorRef instance
7375
* @since 0.0.1
7476
*/
75-
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws Exception {
77+
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws ActorCreationException {
7678
ActorOuterClass.ActorId actorId = buildActorId(system, name, parent);
7779
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
7880
if (Objects.nonNull(ref)) {
@@ -101,7 +103,7 @@ private static ActorOuterClass.ActorId buildActorId(String system, String name,
101103
.build();
102104
}
103105

104-
private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws Exception {
106+
private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws ActorCreationException {
105107
Protocol.SpawnRequest req = Protocol.SpawnRequest.newBuilder()
106108
.addActors(actorId)
107109
.build();
@@ -118,13 +120,10 @@ private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient clie
118120
* @return an Optional containing, or not, the response object to the Action call
119121
* @since 0.0.1
120122
*/
121-
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType) throws Exception {
123+
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType) throws ActorInvocationException {
122124
Optional<T> res = invokeActor(action, Empty.getDefaultInstance(), outputType, Optional.empty());
123-
if (res.isPresent()) {
124-
return Optional.of(outputType.cast(res.get()));
125-
}
125+
return res.map(outputType::cast);
126126

127-
return res;
128127
}
129128

130129
/**
@@ -139,13 +138,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
139138
* @return an Optional containing, or not, the response object to the Action call
140139
* @since 0.0.1
141140
*/
142-
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType, InvocationOpts opts) throws Exception {
141+
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType, InvocationOpts opts) throws ActorInvocationException {
143142
Optional<T> res = invokeActor(action, Empty.getDefaultInstance(), outputType, Optional.ofNullable(opts));
144-
if (res.isPresent()) {
145-
return Optional.of(outputType.cast(res.get()));
146-
}
143+
return res.map(outputType::cast);
147144

148-
return res;
149145
}
150146

151147
/**
@@ -159,13 +155,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
159155
* @return an Optional containing, or not, the response object to the Action call
160156
* @since 0.0.1
161157
*/
162-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType) throws Exception {
158+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType) throws ActorInvocationException {
163159
Optional<T> res = invokeActor(action, value, outputType, Optional.empty());
164-
if (res.isPresent()) {
165-
return Optional.of(outputType.cast(res.get()));
166-
}
160+
return res.map(outputType::cast);
167161

168-
return res;
169162
}
170163

171164
/**
@@ -181,13 +174,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
181174
* @return an Optional containing, or not, the response object to the Action call
182175
* @since 0.0.1
183176
*/
184-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType, InvocationOpts opts) throws Exception {
177+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType, InvocationOpts opts) throws ActorInvocationException {
185178
Optional<T> res = invokeActor(action, value, outputType, Optional.ofNullable(opts));
186-
if (res.isPresent()) {
187-
return Optional.of(outputType.cast(res.get()));
188-
}
179+
return res.map(outputType::cast);
189180

190-
return res;
191181
}
192182

193183
/**
@@ -198,7 +188,7 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
198188
* @param action name of the action to be called.
199189
* @since 0.0.1
200190
*/
201-
public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exception {
191+
public <T extends GeneratedMessageV3> void invokeAsync(String action) throws ActorInvocationException {
202192
InvocationOpts opts = InvocationOpts.builder().async(true).build();
203193
invokeActor(action, Empty.getDefaultInstance(), null, Optional.of(opts));
204194
}
@@ -213,10 +203,10 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exc
213203
* Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
214204
* @since 0.0.1
215205
*/
216-
public <T extends GeneratedMessageV3> void invokeAsync(String action, InvocationOpts opts) throws Exception {
206+
public <T extends GeneratedMessageV3> void invokeAsync(String action, InvocationOpts opts) throws ActorInvocationException {
217207
InvocationOpts mergedOpts = InvocationOpts.builder()
218208
.async(true)
219-
.delay(opts.getDelay())
209+
.delaySeconds(opts.getDelaySeconds())
220210
.scheduledTo(opts.getScheduledTo())
221211
.timeoutSeconds(opts.getTimeoutSeconds())
222212
.build();
@@ -233,7 +223,7 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action, Invocation
233223
* @param value the action argument object.
234224
* @since 0.0.1
235225
*/
236-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value) throws Exception {
226+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value) throws ActorInvocationException {
237227
InvocationOpts opts = InvocationOpts.builder().async(true).build();
238228
invokeActor(action, value, null, Optional.of(opts));
239229
}
@@ -249,10 +239,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
249239
* Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
250240
* @since 0.0.1
251241
*/
252-
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value, InvocationOpts opts) throws Exception {
242+
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value, InvocationOpts opts) throws ActorInvocationException {
253243
InvocationOpts mergedOpts = InvocationOpts.builder()
254244
.async(true)
255-
.delay(opts.getDelay())
245+
.delaySeconds(opts.getDelaySeconds())
256246
.scheduledTo(opts.getScheduledTo())
257247
.timeoutSeconds(opts.getTimeoutSeconds())
258248
.build();
@@ -285,33 +275,27 @@ public boolean isUnNamedActor() {
285275
}
286276

287277
private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invokeActor(
288-
String cmd, S argument, Class<T> outputType, Optional<InvocationOpts> options) throws Exception {
278+
String cmd, S argument, Class<T> outputType, Optional<InvocationOpts> options) throws ActorInvocationException {
289279
Objects.requireNonNull(this.actorId, "ActorId cannot be null");
290280

291281
Protocol.InvocationRequest.Builder invocationRequestBuilder = Protocol.InvocationRequest.newBuilder();
292282

293283
Map<String, String> metadata = new HashMap<>();
294-
if (options.isPresent()) {
295-
InvocationOpts opts = options.get();
284+
options.ifPresent(opts -> {
296285
invocationRequestBuilder.setAsync(opts.isAsync());
297-
298-
if (opts.getDelay().isPresent() && !opts.getScheduledTo().isPresent()) {
299-
invocationRequestBuilder.setScheduledTo(opts.getDelay().get());
300-
} else if (opts.getScheduledTo().isPresent()) {
301-
invocationRequestBuilder.setScheduledTo(opts.getScheduleTimeInLong());
302-
}
303-
304286
metadata.put("request-timeout", String.valueOf(opts.getTimeout()));
305-
}
287+
opts.getDelaySeconds().ifPresent(invocationRequestBuilder::setScheduledTo);
288+
// 'scheduledTo' override 'delay' if both is set.
289+
opts.getScheduledTo()
290+
.ifPresent(scheduleTo -> invocationRequestBuilder.setScheduledTo(opts.getScheduleTimeInLong()));
291+
});
306292

307293
final ActorOuterClass.Actor actorRef = ActorOuterClass.Actor.newBuilder()
308294
.setId(this.actorId)
309295
.build();
310296

311297
Any commandArg = Any.pack(argument);
312298

313-
314-
315299
invocationRequestBuilder
316300
.setSystem(ActorOuterClass.ActorSystem.newBuilder().setName(this.actorId.getSystem()).build())
317301
.setActor(actorRef)
@@ -328,13 +312,16 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
328312
case UNRECOGNIZED:
329313
String msg = String.format("Error when trying to invoke Actor %s. Details: %s",
330314
this.getActorName(), status.getMessage());
331-
332-
throw new ActorInvokeException(msg);
315+
throw new ActorInvocationException(msg);
333316
case ACTOR_NOT_FOUND:
334-
throw new ActorNotFoundException();
317+
throw new ActorNotFoundException("Actor not found.");
335318
case OK:
336319
if (resp.hasValue() && Objects.nonNull(outputType)) {
337-
return Optional.of(resp.getValue().unpack(outputType));
320+
try {
321+
return Optional.of(resp.getValue().unpack(outputType));
322+
} catch (Exception e) {
323+
throw new ActorInvocationException("Error handling response.", e);
324+
}
338325
}
339326
return Optional.empty();
340327
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class InvocationOpts {
2323
private Duration timeoutSeconds = Duration.ofSeconds(10);
2424

2525
@Builder.Default
26-
private Optional<Long> delay = Optional.empty();
26+
private Optional<Long> delaySeconds = Optional.empty();
2727

2828
@Builder.Default
2929
private Optional<LocalDateTime> scheduledTo = Optional.empty();

src/main/java/io/eigr/spawn/api/actors/workflows/SideEffect.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ public static <T extends GeneratedMessageV3> SideEffect to(ActorRef actor, Strin
4343
public Protocol.SideEffect build() {
4444
Protocol.InvocationRequest.Builder requestBuilder = Protocol.InvocationRequest.newBuilder();
4545

46-
if (this.opts.isPresent()) {
47-
InvocationOpts options = this.opts.get();
48-
if (options.getDelay().isPresent() && !options.getScheduledTo().isPresent()) {
49-
requestBuilder.setScheduledTo(options.getDelay().get());
50-
} else if (options.getScheduledTo().isPresent()) {
51-
requestBuilder.setScheduledTo(options.getScheduleTimeInLong());
52-
}
53-
}
46+
opts.ifPresent(invocationOpts -> {
47+
invocationOpts.getDelaySeconds().ifPresent(requestBuilder::setScheduledTo);
48+
// 'scheduledTo' override 'delay' if both is set.
49+
invocationOpts.getScheduledTo()
50+
.ifPresent(scheduleTo -> requestBuilder.setScheduledTo(invocationOpts.getScheduleTimeInLong()));
51+
});
5452

5553
requestBuilder.setSystem(ActorOuterClass.ActorSystem.newBuilder()
5654
.setName(this.actor.getActorSystem())
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 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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
package io.eigr.spawn.api.exceptions;
22

3-
public final class ActorNotFoundException extends IllegalArgumentException{
3+
/**
4+
* Actor Registration Exception.
5+
*
6+
* @author Paulo H3nrique Alves
7+
*/
8+
public class ActorNotFoundException extends ActorInvocationException {
9+
10+
public ActorNotFoundException() {
11+
super();
12+
}
13+
public ActorNotFoundException(String s) {
14+
super(s);
15+
}
16+
17+
public ActorNotFoundException(String message, Throwable cause) {
18+
super(message, cause);
19+
}
20+
21+
public ActorNotFoundException(Throwable cause) {
22+
super(cause);
23+
}
424
}
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+
}
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 Exception {
9+
10+
protected 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+
}

0 commit comments

Comments
 (0)