99package dev .restate .sdk .core .statemachine ;
1010
1111import com .google .protobuf .ByteString ;
12+ import com .google .protobuf .MessageLite ;
1213import com .google .protobuf .UnsafeByteOperations ;
1314import dev .restate .common .Slice ;
1415import dev .restate .sdk .common .TerminalException ;
15- import dev .restate .sdk .core .ProtocolException ;
1616import dev .restate .sdk .core .generated .protocol .Protocol ;
17- import java .io .PrintWriter ;
18- import java .io .StringWriter ;
1917import java .nio .ByteBuffer ;
2018import java .time .Duration ;
2119import java .util .Objects ;
22- import org .jspecify .annotations .Nullable ;
2320
2421public class Util {
2522
@@ -38,46 +35,6 @@ static Protocol.Failure toProtocolFailure(Throwable throwable) {
3835 return toProtocolFailure (TerminalException .INTERNAL_SERVER_ERROR_CODE , throwable .toString ());
3936 }
4037
41- static Protocol .ErrorMessage toErrorMessage (
42- Throwable throwable ,
43- int currentCommandIndex ,
44- @ Nullable String currentCommandName ,
45- @ Nullable MessageType currentCommandType ) {
46- Protocol .ErrorMessage .Builder msg = Protocol .ErrorMessage .newBuilder ();
47-
48- if (throwable .getMessage () == null ) {
49- // This happens only with few common exceptions, but anyway
50- msg .setMessage (throwable .toString ());
51- } else {
52- msg .setMessage (throwable .getMessage ());
53- }
54-
55- if (throwable instanceof ProtocolException ) {
56- msg .setCode (((ProtocolException ) throwable ).getCode ());
57- } else {
58- msg .setCode (TerminalException .INTERNAL_SERVER_ERROR_CODE );
59- }
60-
61- // Convert stacktrace to string
62- StringWriter sw = new StringWriter ();
63- PrintWriter pw = new PrintWriter (sw );
64- throwable .printStackTrace (pw );
65- msg .setStacktrace (sw .toString ());
66-
67- // Add journal entry info
68- if (currentCommandIndex >= 0 ) {
69- msg .setRelatedCommandIndex (currentCommandIndex );
70- }
71- if (currentCommandName != null ) {
72- msg .setRelatedCommandName (currentCommandName );
73- }
74- if (currentCommandType != null ) {
75- msg .setRelatedCommandType (currentCommandType .encode ());
76- }
77-
78- return msg .build ();
79- }
80-
8138 static TerminalException toRestateException (Protocol .Failure failure ) {
8239 return new TerminalException (failure .getCode (), failure .getMessage ());
8340 }
@@ -100,6 +57,54 @@ static Duration durationMin(Duration a, Duration b) {
10057 return (a .compareTo (b ) <= 0 ) ? a : b ;
10158 }
10259
60+ /**
61+ * Returns a string representation of a command message.
62+ *
63+ * @param message The command message
64+ * @return A string representation of the command message
65+ */
66+ static String commandMessageToString (MessageLite message ) {
67+ if (message instanceof Protocol .InputCommandMessage ) {
68+ return "handler input" ;
69+ } else if (message instanceof Protocol .OutputCommandMessage ) {
70+ return "handler return" ;
71+ } else if (message instanceof Protocol .GetLazyStateCommandMessage ) {
72+ return "get state" ;
73+ } else if (message instanceof Protocol .GetLazyStateKeysCommandMessage ) {
74+ return "get state keys" ;
75+ } else if (message instanceof Protocol .SetStateCommandMessage ) {
76+ return "set state" ;
77+ } else if (message instanceof Protocol .ClearStateCommandMessage ) {
78+ return "clear state" ;
79+ } else if (message instanceof Protocol .ClearAllStateCommandMessage ) {
80+ return "clear all state" ;
81+ } else if (message instanceof Protocol .GetPromiseCommandMessage ) {
82+ return "get promise" ;
83+ } else if (message instanceof Protocol .PeekPromiseCommandMessage ) {
84+ return "peek promise" ;
85+ } else if (message instanceof Protocol .CompletePromiseCommandMessage ) {
86+ return "complete promise" ;
87+ } else if (message instanceof Protocol .SleepCommandMessage ) {
88+ return "sleep" ;
89+ } else if (message instanceof Protocol .CallCommandMessage ) {
90+ return "call" ;
91+ } else if (message instanceof Protocol .OneWayCallCommandMessage ) {
92+ return "one way call/send" ;
93+ } else if (message instanceof Protocol .SendSignalCommandMessage ) {
94+ return "send signal" ;
95+ } else if (message instanceof Protocol .RunCommandMessage ) {
96+ return "run" ;
97+ } else if (message instanceof Protocol .AttachInvocationCommandMessage ) {
98+ return "attach invocation" ;
99+ } else if (message instanceof Protocol .GetInvocationOutputCommandMessage ) {
100+ return "get invocation output" ;
101+ } else if (message instanceof Protocol .CompleteAwakeableCommandMessage ) {
102+ return "complete awakeable" ;
103+ }
104+
105+ return message .getClass ().getSimpleName ();
106+ }
107+
103108 private static final class ByteStringSlice implements Slice {
104109 private final ByteString byteString ;
105110
0 commit comments