Skip to content

Commit

Permalink
Issue-75: GetState returns gRPC NOT_FOUND (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zen Yui authored Aug 26, 2020
1 parent b608e9d commit 05d0e3e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ class AggregateCommandHandler(
log.error(s"[ChiefOfState] could not find state for entity ${command.entityId}")
CommandHandlerResponse()
.withFailedResponse(
FailedCommandHandlerResponse()
.withReason("entity not found")
.withCause(FailureCause.INTERNAL_ERROR)
AggregateCommandHandler.GET_STATE_NOT_FOUND_FAILURE
)
}
}
Expand Down Expand Up @@ -269,4 +267,10 @@ object AggregateCommandHandler {
Status.Code.OUT_OF_RANGE,
Status.Code.PERMISSION_DENIED
)

// constant failure for entity not found
val GET_STATE_NOT_FOUND_FAILURE: FailedCommandHandlerResponse =
FailedCommandHandlerResponse()
.withReason("entity not found")
.withCause(FailureCause.INTERNAL_ERROR)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import scalapb.{GeneratedMessage, GeneratedMessageCompanion}
import com.namely.chiefofstate.config.SendCommandSettings

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Failure
import scala.util.{Try, Success, Failure}
import io.superflat.lagompb.GlobalException

class GrpcServiceImpl(sys: ActorSystem,
clusterSharding: ClusterSharding,
Expand Down Expand Up @@ -108,12 +109,25 @@ class GrpcServiceImpl(sys: ActorSystem,
)
} else {
sendCommand[GetStateRequest, State](clusterSharding, in.entityId, in, Map.empty[String, String])
.map((namelyState: StateAndMeta[State]) => {
GetStateResponse(
state = namelyState.state.currentState,
meta = Some(Util.toCosMetaData(namelyState.metaData))
.transform({
// transform success to a GetStateResponse
case Success(namelyState) =>
Success(
GetStateResponse(
state = namelyState.state.currentState,
meta = Some(Util.toCosMetaData(namelyState.metaData))
)
)
})

// handle not-found errors specifically
case Failure(e) if e.getMessage == AggregateCommandHandler.GET_STATE_NOT_FOUND_FAILURE.reason =>
Failure(new GrpcServiceException(status = Status.NOT_FOUND.withDescription("COS could not find entity")))

// pass through other failures
case Failure(e) =>
log.error(s"unhandled error in getState", e)
Failure(e)
})
}
}
}

0 comments on commit 05d0e3e

Please sign in to comment.