
Spec
Enum and Error Metadata Design Specification
This document describes how to use extension fields from extend/constant.proto on enums to attach business codes, multilingual messages, and error-related information to each enum value.
Main dependencies:
apihug/protobuf/extend/constant.protoextend google.protobuf.EnumValueOptions { Meta field = 37020; }Meta information to each enum value:
syntax = "proto3";
package your.package;
import "apihug/protobuf/extend/constant.proto";
enum OrderStatus {
UNKNOWN = 0 [(hope.constant.field) = {
code: 0;
message: "unknown";
message2: "未知";
}];
CREATED = 1 [(hope.constant.field) = {
code: 1;
message: "created";
message2: "已创建";
}];
}
Meta is used to describe general information about enum values:
code (int32)
message (string)
message2 (string)
error (Error)
If Meta is not configured, the framework can infer default rules, but for readability and stability, it is recommended to explicitly set it for new code.
When an enum is used for error codes (e.g., business errors, system errors), error information can be described in Meta.error:
enum UserError {
USER_NOT_FOUND = 1 [(hope.constant.field) = {
code: 10001;
message: "user_not_found";
message2: "用户不存在";
error: {
title: "User not found";
tips: "检查用户ID是否正确,或联系管理员";
http_status: NOT_FOUND;
phase: SERVICE;
severity: ERROR;
};
}];
}
title (string)
tips (string)
http_status (HttpStatus enum)
BAD_REQUEST, UNAUTHORIZED, NOT_FOUND, INTERNAL_SERVER_ERROR, etc.phase (Phase enum)
CONTROLLER: Interface/presentation layer.SERVICE: Service/application layer.DOMAIN: Domain model layer.severity (Severity enum)
LOW: Low impact.WARN: Warning, retryable.ERROR: Normal business flow cannot continue.FATAL: Critical error, may corrupt data or require manual intervention.BAD_REQUEST (400).UNAUTHORIZED (401) or FORBIDDEN (403).NOT_FOUND (404).INTERNAL_SERVER_ERROR (500).code values to each enum value, avoiding arbitrary reuse or modification of existing codes.message / message2 text.error field to facilitate unified handling and troubleshooting.HttpStatus, base it on standard HTTP semantics and avoid overusing 200/500 as generic statuses.