0

I try to find out the way to report server errors via grpc. What I want to achieve is errors that are verbose and static defined enough to tell clients what actually happened.

Imagine I have the next request:

message UpdateEmailRequest {
  string email = 1;
}

message UpdateEmailResponse {
}

Then I was trying to make errors more verbose because status codes don't tell clients about error details. You could use FieldViolation, but it only specifies the field, not the error in the static way. You could provide Description to these FieldViolations but clients need to either deal with string errors and somehow map them to internal client errors OR clients display these errors directly, which doesn't work in some cases because business logic can vary based on the error type.

What I've invented is:

message UpdateEmailResponse {
  enum ErrorCode {
    UNKNOWN_ERROR = 0;
    INVALID_EMAIL_LENGTH = 1;
    INVALID_EMAIL_FORMAT = 2;
    INVALID_EMAIL_TEMPORARY_MAIL_NOT_ALLOWED = 3; //temporary mail services
    INVALID_EMAIL_ALIAS = 4; // email is alias or redirect, not the actual email
    INVALID_EMAIL_FORBIDDEN_WORDS = 5; // for email with bad language
    ...
  }

  message Body {}

  oneof response {
    ErrorCode error_code = 1;
    Body body = 2;
  }
}

This way you could let the clients know about specific errors combined with status codes, but it doesn't use the out-of-box FieldViolations and it looks like non-grpc way to handle errors.

Other solution is to embed json with error code (which I can still have defined inside the request entity) and error msg inside the FieldViolations's Description, but again, it looks like non-grpc way.

So, I'm stuck here with errors in grpc, could you tell me the proper way to do it?

Thanks.

  • [Details has the Any type](https://godoc.org/google.golang.org/genproto/googleapis/rpc/status#Status.Details), so what's stopping you from inventing your own Detail type? (Assuming you're using [google.golang.org/grpc/status](https://godoc.org/google.golang.org/grpc/status) to construct error values.) – Peter Dec 04 '19 at 08:44
  • @Peter , thanks, but it can't be defined for clients as a message in proto file, correct? Is it like a convenience ? – Nikita Lieskin Dec 04 '19 at 09:05
  • Oh, I see now, I can pass any proto message to WithDetails method. Thanks, it helps a lot. – Nikita Lieskin Dec 04 '19 at 09:09
  • 1
    My personal problem with that solution is that now the error messages are no longer strictly defined in the protobuf file. Clients who use your API now have to go and look for the possible ErrorCodes they can recieve and what they mean (if they need to know that information) instead of seeing directly which rpc call can produce which error – ptrckdev Dec 04 '19 at 12:45

0 Answers0