Skip to content

Commit 3766d8a

Browse files
Few improvements to errors from sdk-shared-core: (#513)
* Improve the non-deterministic error, adding some nice to read diff * Correctly report command index/command name only when known.
1 parent 09c364b commit 3766d8a

File tree

22 files changed

+1011
-197
lines changed

22 files changed

+1011
-197
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ build
3636
.factorypath
3737
kls_database.db
3838
.kotlin
39+
40+
.restate
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package my.restate.sdk.examples;
10+
11+
import dev.restate.sdk.Context;
12+
import dev.restate.sdk.annotation.Handler;
13+
import dev.restate.sdk.annotation.Service;
14+
import dev.restate.sdk.endpoint.Endpoint;
15+
import dev.restate.sdk.http.vertx.RestateHttpServer;
16+
import java.time.Duration;
17+
18+
@Service
19+
public class Greeter {
20+
21+
public record Greeting(String name) {}
22+
23+
public record GreetingResponse(String message) {}
24+
25+
@Handler
26+
public GreetingResponse greet(Context ctx, Greeting req) {
27+
// Respond to caller
28+
return new GreetingResponse("You said hi to " + req.name + "!");
29+
}
30+
31+
public static void main(String[] args) {
32+
RestateHttpServer.listen(
33+
Endpoint.bind(
34+
new Greeter(), configurator -> configurator.inactivityTimeout(Duration.ofSeconds(1))));
35+
}
36+
}

sdk-core/src/main/java/dev/restate/sdk/core/ProtocolException.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public class ProtocolException extends RuntimeException {
1818
static final int NOT_FOUND_CODE = 404;
1919
public static final int UNSUPPORTED_MEDIA_TYPE_CODE = 415;
2020
public static final int INTERNAL_CODE = 500;
21-
static final int JOURNAL_MISMATCH_CODE = 570;
21+
public static final int JOURNAL_MISMATCH_CODE = 570;
2222
static final int PROTOCOL_VIOLATION_CODE = 571;
23+
static final int CLOSED_CODE = 598;
2324

2425
@SuppressWarnings("StaticAssignmentOfThrowable")
25-
static final ProtocolException CLOSED = new ProtocolException("Invocation closed");
26+
static final ProtocolException CLOSED = new ProtocolException("Invocation closed", CLOSED_CODE);
2627

2728
private final int code;
2829

@@ -54,10 +55,10 @@ public static ProtocolException unexpectedMessage(
5455
PROTOCOL_VIOLATION_CODE);
5556
}
5657

57-
public static ProtocolException unexpectedMessage(String type, MessageLite actual) {
58+
public static ProtocolException unexpectedMessage(String expected, MessageLite actual) {
5859
return new ProtocolException(
5960
"Unexpected message type received from the runtime. Expected: '"
60-
+ type
61+
+ expected
6162
+ "', Actual: '"
6263
+ actual.getClass().getCanonicalName()
6364
+ "'",
@@ -69,25 +70,6 @@ static ProtocolException unexpectedNotificationVariant(Class<?> clazz) {
6970
"Unexpected notification variant " + clazz.getName(), PROTOCOL_VIOLATION_CODE);
7071
}
7172

72-
public static ProtocolException commandDoesNotMatch(MessageLite expected, MessageLite actual) {
73-
return new ProtocolException(
74-
"Replayed journal doesn't match the handler code.\nThe handler code generated: "
75-
+ expected
76-
+ "\nwhile the replayed entry is: "
77-
+ actual,
78-
JOURNAL_MISMATCH_CODE);
79-
}
80-
81-
public static ProtocolException commandClassDoesNotMatch(
82-
Class<? extends MessageLite> expectedClazz, MessageLite actual) {
83-
return new ProtocolException(
84-
"Replayed journal doesn't match the handler code.\nThe handler code generated: "
85-
+ expectedClazz.getName()
86-
+ "\nwhile the replayed entry is: "
87-
+ actual,
88-
JOURNAL_MISMATCH_CODE);
89-
}
90-
9173
public static ProtocolException commandsToProcessIsEmpty() {
9274
return new ProtocolException("Expecting command queue to be non empty", JOURNAL_MISMATCH_CODE);
9375
}

sdk-core/src/main/java/dev/restate/sdk/core/statemachine/ClosedState.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ final class ClosedState implements State {
1515

1616
@Override
1717
public void hitError(
18-
Throwable throwable, @Nullable Duration nextRetryDelay, StateContext stateContext) {
18+
Throwable throwable,
19+
@Nullable CommandRelationship relatedCommand,
20+
@Nullable Duration nextRetryDelay,
21+
StateContext stateContext) {
1922
// Ignore, as we closed already
2023
}
2124

0 commit comments

Comments
 (0)