-
Notifications
You must be signed in to change notification settings - Fork 15.7k
extending existing enum #6104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There is no way to define an enum in one file and then extend it with additional values in another file. You can extend messages, though--would that work for your use case? |
But we cannot extend the |
What you could do is define a message ErrorResponse {
oneof code {
ErrorCode error_code = 1;
int32 custom_code = 2;
}
} Could this work? |
Both have custom error code and predefined error code? Not a good choice i think, the custom error code may override the predefined one. What i want to do is keep some error code reserved(<1000 perhaps), and others are also defined in proto. Here is my workaround now : message ErrorResponse {
int32 code = 1;
string message = 2;
}
enum ErrorCode {
OK = 0;
UNKNOWN = 1;
} Users could define other error code in another enum, like: enum LogicErrorCode {
USER_NOT_FOUND = 1001;
DB_ERROR = 1002;
} Then users should call func buildErrorResponse(code LogicErrorCode,msg string) *ErrorResponse{
if msg == "" {
msg = code.String()
}
return &ErrorResponse{Code:int32(code),Message:msg}
} When do care about the error code on the peer, could handle it like this, fortunately, not very bad. func onError(err *ErrorResponse) {
if err.Code == int32(ErrorCode_OK){
}
if err.Code == int32(LogicErrorCode_USER_NOT_FOUND){
}
} |
I think you can do this:
In the user's code, they can do:
|
NOT A GOOG IDEA I THINK. |
I think the error codes are a valid use case for something like this -- I have generic errors for things the programmer did wrong, and component specific error codes for actual problems, e.g. reading from an SD Card I have Reusing the common error codes without a form of inheritance is difficult. |
Is there any way to extend enum?
E.g.
I would have enum
ErrorCode
in the first file as for the underlying messageErrorResponse
.And also i want exted the enum in the second file like this:
Is it possible at all?
Any workarounds?
The text was updated successfully, but these errors were encountered: