@@ -11,7 +11,6 @@ import com.namely.protobuf.chiefofstate.v1.writeside.{
1111 HandleCommandResponse ,
1212 WriteSideHandlerServiceClient
1313}
14- import com .namely .protobuf .chiefofstate .v1 .writeside .HandleCommandResponse .ResponseType .{PersistAndReply , Reply }
1514import com .namely .chiefofstate .config .HandlerSetting
1615import io .grpc .{Status , StatusRuntimeException }
1716import io .superflat .lagompb .{CommandHandler , ProtosRegistry }
@@ -98,16 +97,10 @@ class AggregateCommandHandler(
9897 if (priorEventMeta.revisionNumber > 0 ) {
9998 log.debug(s " [ChiefOfState] found state for entity ${command.entityId}" )
10099 CommandHandlerResponse ()
101- .withSuccessResponse(
102- SuccessCommandHandlerResponse ()
103- .withNoEvent(com.google.protobuf.empty.Empty .defaultInstance)
104- )
105100 } else {
106101 log.error(s " [ChiefOfState] could not find state for entity ${command.entityId}" )
107102 CommandHandlerResponse ()
108- .withFailedResponse(
109- AggregateCommandHandler .GET_STATE_NOT_FOUND_FAILURE
110- )
103+ .withFailure(FailureResponse ().withNotFound(" entity not found" ))
111104 }
112105 }
113106
@@ -128,9 +121,10 @@ class AggregateCommandHandler(
128121 // make blocking gRPC call to handler service
129122 val responseAttempt : Try [HandleCommandResponse ] = Try {
130123 // construct the request message
131- val handleCommandRequest = HandleCommandRequest (command= remoteCommand.command)
132- .withCurrentState(priorState)
133- .withMeta(Util .toCosMetaData(priorEventMeta))
124+ val handleCommandRequest =
125+ HandleCommandRequest (command= remoteCommand.command)
126+ .withCurrentState(priorState)
127+ .withMeta(Util .toCosMetaData(priorEventMeta))
134128
135129 // create an akka gRPC request builder
136130 val futureResponse : Future [HandleCommandResponse ] =
@@ -144,7 +138,7 @@ class AggregateCommandHandler(
144138 request.addHeader(header.key, value)
145139 case RemoteCommand .Header .Value .BytesValue (value) =>
146140 request.addHeader(header.key, akka.util.ByteString (value.toByteArray))
147- case unhandled => throw new Exception (s " unhandled gRPC header type, ${unhandled.getClass.getName} " )
141+ case _ => throw new Exception (s " header value must be string or bytes " )
148142 }
149143 }
150144 )
@@ -168,55 +162,29 @@ class AggregateCommandHandler(
168162 * @return an instance of CommandHandlerResponse
169163 */
170164 def handleRemoteResponseSuccess (response : HandleCommandResponse ): CommandHandlerResponse = {
171- response.responseType match {
172- case PersistAndReply (persistAndReply) =>
165+ response.event match {
166+ case Some (event) =>
167+
173168 log.debug(" [ChiefOfState] command handler return successfully. An event will be persisted..." )
174- val eventFQN : String = Util .getProtoFullyQualifiedName(persistAndReply.getEvent)
175169
176- log.debug(s " [ChiefOfState] command handler event to persist $eventFQN" )
177- if (handlerSetting.enableProtoValidations) {
178- if (handlerSetting.eventFQNs.contains(eventFQN)) {
179- log.debug(s " [ChiefOfState] command handler event to persist $eventFQN is valid. " )
180- CommandHandlerResponse ()
181- .withSuccessResponse(
182- SuccessCommandHandlerResponse ()
183- .withEvent(persistAndReply.getEvent)
184- )
185- } else {
186- log.error(
187- s " [ChiefOfState] command handler event to persist $eventFQN is not configured. Failing request "
188- )
189- CommandHandlerResponse ()
190- .withFailedResponse(
191- FailedCommandHandlerResponse ()
192- .withReason(s " received unknown event type $eventFQN" )
193- .withCause(FailureCause .VALIDATION_ERROR )
194- )
195- }
196- } else {
197- log.debug(s " [ChiefOfState] command handler event to persist $eventFQN. FQN validation skipped. " )
170+ val eventFQN : String = Util .getProtoFullyQualifiedName(event)
171+
172+ if (handlerSetting.enableProtoValidations && ! handlerSetting.eventFQNs.contains(eventFQN)) {
173+ log.error(s " [ChiefOfState] command handler returned unknown event type, $eventFQN" )
198174 CommandHandlerResponse ()
199- .withSuccessResponse (
200- SuccessCommandHandlerResponse ()
201- .withEvent(persistAndReply.getEvent )
175+ .withFailure (
176+ FailureResponse ()
177+ .withValidation( s " received unknown event type $eventFQN " )
202178 )
179+ } else {
180+ log.debug(s " [ChiefOfState] command handler event to persist $eventFQN is valid. " )
181+ CommandHandlerResponse ()
182+ .withEvent(event)
203183 }
204184
205- case Reply (_) =>
185+ case None =>
206186 log.debug(" [ChiefOfState] command handler return successfully. No event will be persisted..." )
207187 CommandHandlerResponse ()
208- .withSuccessResponse(
209- SuccessCommandHandlerResponse ()
210- .withNoEvent(com.google.protobuf.empty.Empty .defaultInstance)
211- )
212-
213- case unhandled =>
214- CommandHandlerResponse ()
215- .withFailedResponse(
216- FailedCommandHandlerResponse ()
217- .withReason(s " command handler returned malformed event, ${unhandled.getClass.getName}" )
218- .withCause(FailureCause .INTERNAL_ERROR )
219- )
220188 }
221189 }
222190
@@ -235,38 +203,28 @@ class AggregateCommandHandler(
235203 log.error(s " [ChiefOfState] $reason" )
236204
237205 // handle specific gRPC error statuses
238- val cause =
206+ val failureResponse =
239207 if (GRPC_FAILED_VALIDATION_STATUSES .contains(status.getCode)) {
240- FailureCause . VALIDATION_ERROR
208+ FailureResponse ().withValidation(status.getDescription)
241209 } else {
242- FailureCause . INTERNAL_ERROR
210+ FailureResponse ().withCritical(status.getDescription)
243211 }
244212
245- CommandHandlerResponse ()
246- .withFailedResponse(
247- FailedCommandHandlerResponse ()
248- .withReason(reason)
249- .withCause(cause)
250- )
213+ CommandHandlerResponse ().withFailure(failureResponse)
251214
252215 case e : GrpcServiceException =>
253216 log.error(s " [ChiefOfState] handler gRPC failed with ${e.status.toString}" , e)
254217 CommandHandlerResponse ()
255- .withFailedResponse(
256- FailedCommandHandlerResponse ()
257- .withReason(e.getStatus.toString)
258- .withCause(FailureCause .INTERNAL_ERROR )
218+ .withFailure(
219+ FailureResponse ().withCritical(e.getStatus.getDescription)
259220 )
260221
261222 case e : Throwable =>
262223 log.error(s " [ChiefOfState] gRPC handler critical failure " , e)
263224 CommandHandlerResponse ()
264- .withFailedResponse(
265- FailedCommandHandlerResponse ()
266- .withReason(
267- s " Critical error occurred handling command, ${e.getMessage}"
268- )
269- .withCause(FailureCause .INTERNAL_ERROR )
225+ .withFailure(
226+ FailureResponse ()
227+ .withCritical(s " Critical error occurred handling command, ${e.getMessage}" )
270228 )
271229 }
272230 }
@@ -286,10 +244,4 @@ object AggregateCommandHandler {
286244 Status .Code .OUT_OF_RANGE ,
287245 Status .Code .PERMISSION_DENIED
288246 )
289-
290- // constant failure for entity not found
291- val GET_STATE_NOT_FOUND_FAILURE : FailedCommandHandlerResponse =
292- FailedCommandHandlerResponse ()
293- .withReason(" entity not found" )
294- .withCause(FailureCause .INTERNAL_ERROR )
295247}
0 commit comments