If I make a writeRelationships call with
# spicedb
w
If I make a writeRelationships call with more than one precondition, is there a reliable way to determine which precondition failed? The exception message contains the text of the failing precondition - but I'm hoping to find the index of the failing precondition, and can't see how to access this.
v
it should be possible, yes.. The gRPC error returns rich metadata including the precondition that failed. It does not give you the index, but it gives you the whole precondition definition: https://github.com/authzed/spicedb/blob/e8aeca080d7804e0fb1782c675065c7d5f020937/internal/services/v1/errors.go#L165-L226
w
Oh, cool - I thought I'd have to programmatically parse the text of the error message to correlate it to the preconditions I passed into the call.
I'm using the java client - when a precondition fails a
io.grpc.StatusRuntimeException
is thrown. I can't see where to get the precondition metadata from this. Could you give me a pointer?
v
I don't know how to do this with Java, but first entry in google seemed promising: https://stackoverflow.com/questions/60936054/how-to-use-grpc-status-details-in-package-io-grpc
w
Thanks for the hint! I'm not super familiar with grpc - so didn't know where to start. For future searchers of truth, roughly what you need to do is (kotlin)
Copy code
} catch (e: StatusRuntimeException) {
    if (e.status.code == Code.FAILED_PRECONDITION) {
        val statusProto = io.grpc.protobuf.StatusProto.fromStatusAndTrailers(e.status, e.trailers)
        val errorInfo = statusProto.getDetails(0).unpack(com.google.rpc.ErrorInfo::class.java)
        val preconditionFailureDetails = errorInfo.metadataMap
v
I think there should also be types generated for the errors in the SDK, since it's defined as part of the proto API
w
Yep - there's an enum
ErrorReasonOuterClass.ErrorReason
- but can't see a custom type (or key constants) for the data in the metadataMap. which is ok - it's fine to work with.
y
yeah so far this feels like the most cumbersome and frustrating part of the java gRPC API
we went on the same journey a few months back
35 Views