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.