Search code examples
grpcerror-codegrpc-go

gRPC InvalidArgument or FailedPrecondition if system cannot be updated


Suppose we have a gRPC DB-backed application with some resource X like this, for example in Go:

type X struct {
    id      string
    name    string
    version string
}

And for some reason the application does not permit updates unless version = "2".

If a client tries to make a request to update the name of an existing resource whose version = "1", like this:

// This exists in the DB.
x := X{
    id:      "xyz",
    version: "1",
    name:    "some name",
}

UpdateX(
    X{
        id:      "xyz",
        name:    "some other name",
    },
) 

what gRPC error code should be returned?

Referring to the codes here: https://grpc.github.io/grpc/core/md_doc_statuscodes.html, I can see arguments for both INVALID_ARGUMENT and FAILED_PRECONDITION.

INVALID_ARGUMENT:

  • The resource cannot be updated so the request will always fail, and is hence invalid

FAILED_PRECONDITION:

  • The state of the system needs to be checked to see what the version of the resource is, you can't determine a priori whether the request is possible
  • Even though the system currently does not allow such updates, in theory it could be updated so that the request becomes acceptable

Solution

  • Based on your description, you should probably use FAILED_PRECONDITION. Like mentioned in https://grpc.github.io/grpc/core/md_doc_statuscodes.html: INVALID_ARGUMENT indicates arguments that are problematic. But in your case the arguments is correct, but the system does not allow update, thus I think FAILED_PRECONDITION is a better fit.