HTTP status Code

Can you give an example or snippet on how to modify the http status code on kalix? Another question is what happens if kalix cannot find an entity instance. As far as I understood, it just gives an unknown error. Is there any way to change this, so the client know what’s wrong when the entity instance is not found.

Hey David,

When you log an error effect, you can specify a GRPC status code that gets automatically transcoded to a HTTP status code. See the below example from our timers API docs in which Status.Code.NOT_FOUND would be converted to a 404 HTTP status code:

  override def confirm(currentState: OrderState, confirmRequest: example.ConfirmRequest): ValueEntity.Effect[Empty] =
    if (currentState.placed) 
      effects
        .updateState(currentState.copy(confirmed = true))
        .thenReply(Empty.defaultInstance)
    else
      effects
        .error(s"No order found for '${confirmRequest.orderNumber}'", Status.Code.NOT_FOUND)

The example above might also be useful to answer the second part of your question. When an entity does not yet exist, kalix will use the provided emptyState implementation to initialize it and pass that back as the currentState for the method that is being called. Inside it we can check if the entity is in a desired state and return an error otherwise (similar to what we do above).

Hope this helps!

thanks for the answer. My case is something like this: I have 2 services deployed. The first service, lets call it service 1, calls the service 2 through grpc call like shown in : Calling other services :: Kalix Documentation . I have successfully set the status code from the service 2 through metadata but how do I access this meta data from service 1 upon receiving the response ? thanks

Hey David,

When handling the response, if the call to service 2 has failed you can access the status code wrapped in a StatusRuntimeException, similarly to the example you can find here:

(...)
    grpcClient
        .cancel(CancelRequest(orderNumber.number))
        .map { _ => Empty.defaultInstance } 
        .recover { 
          case ex: StatusRuntimeException if ex.getStatus.getCode == Status.Code.INVALID_ARGUMENT =>
            Empty.defaultInstance
        }
(...)

Hope this can help!