Spec

Enum Type Specification

Enum and Error Extension User Manual — add business codes, multi-language messages, and error metadata to enum values.

Add business codes, multi-language messages, and error metadata to enum values.

  • Extension Package: apihug/protobuf/extend/constant.proto
  • Optional companion: apihug/protobuf/swagger/annotations.proto when you also want enum display metadata via (hope.swagger.enm)
  • Scope: EnumValue
  • Scenarios: Business enums, error codes

Forbidden Rules

NEVER add any file-level option declarations in proto files:

Proto
// Forbidden to use the following options (rely on default code generation configuration)
option java_package = "...";
option java_multiple_files = true;
option go_package = "...";

All code generation configurations are automatically managed by the build system. Strictly forbidden to manually add any file-level options.


1. Import and Syntax

1.1 Import

Proto
import "apihug/protobuf/extend/constant.proto";

1.2 Basic Syntax

Proto
ENUM_NAME = sequence [(hope.constant.field) = {
  code: business_code;  // int32
  message: "English";  // string
  message2: "Chinese";  // string
}];

1.3 Quick Example

Proto
syntax = "proto3";
package com.example.order;

import "apihug/protobuf/extend/constant.proto";

enum OrderStatusEnum {
  PLACED = 0 [(hope.constant.field) = {
    code: 1,
    message: "Placed",
    message2: "已经下单"
  }];

  APPROVED = 1 [(hope.constant.field) = {
    code: 2,
    message: "Approved",
    message2: "已经审批"
  }];

  DELIVERED = 2 [(hope.constant.field) = {
    code: 4,
    message: "Delivered",
    message2: "已经发货"
  }];
}

2. Field Description

FieldTypeRequiredDescription
codeint32YesBusiness code, unique within the same enum
messagestringYesPrimary language message (English), defaults to enum name
message2stringNoSecondary language message (Chinese)
errorError messageNoError extension info (only for error codes)

Note: cn_message (field=3) was deprecated on 2024-05-08, please use message2

Proto Source Alignment

From apihug/protobuf/extend/constant.proto:

Proto
message Meta {
  int32 code = 1;
  string message = 2;
  string message2 = 4;
  Error error = 5;
}

The extension is registered on google.protobuf.EnumValueOptions at field number 37020.


3. Error Code Extension

3.1 Error Syntax

Proto
error: {
  title: "Error title";  // string
  tips: "Handling tips";  // string
  http_status: HTTP status enum;  // Enum name (NOT_FOUND)
  phase: Layer enum;  // CONTROLLER/SERVICE/DOMAIN
  severity: Severity enum;  // LOW/WARN/ERROR/FATAL
}

3.2 Example

Proto
import "apihug/protobuf/extend/constant.proto";

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
    }
  }];
}

3.3 Error Fields

FieldTypeValuesDescription
titlestring-Error title
tipsstring-Handling suggestion
http_statusHttpStatus enumNOT_FOUND/BAD_REQUEST/...HTTP status code (write enum name)
phasePhase enumCONTROLLER/SERVICE/DOMAINError layer
severitySeverity enumLOW/WARN/ERROR/FATALSeverity level

3.4 HttpStatus Enum (from source)

The HttpStatus enum in constant.proto maps directly to HTTP status codes. The table below is still only a selected reference, but it now includes the common extended 4xx and 5xx values present in source:

4xx Client Errors:

Enum ValueCodeDescription
BAD_REQUEST400Bad Request
UNAUTHORIZED401Unauthorized
PAYMENT_REQUIRED402Payment Required
FORBIDDEN403Forbidden
NOT_FOUND404Not Found
METHOD_NOT_ALLOWED405Method Not Allowed
NOT_ACCEPTABLE406Not Acceptable
PROXY_AUTHENTICATION_REQUIRED407Proxy Authentication Required
REQUEST_TIMEOUT408Request Timeout
CONFLICT409Conflict
GONE410Gone
PAYLOAD_TOO_LARGE413Payload Too Large
UNPROCESSABLE_ENTITY422Unprocessable Entity
LOCKED423Locked
FAILED_DEPENDENCY424Failed Dependency
TOO_MANY_REQUESTS429Too Many Requests

5xx Server Errors:

Enum ValueCodeDescription
INTERNAL_SERVER_ERROR500Internal Server Error
NOT_IMPLEMENTED501Not Implemented
BAD_GATEWAY502Bad Gateway
SERVICE_UNAVAILABLE503Service Unavailable
GATEWAY_TIMEOUT504Gateway Timeout
NETWORK_AUTHENTICATION_REQUIRED511Network Authentication Required

3.5 Phase and Severity Enums

Phase (Error.Phase):

Enum ValueDescription
CONTROLLERForm/controller layer
SERVICEService layer
DOMAINDomain layer

Severity (Error.Severity):

Enum ValueDescription
LOWNo impact
WARNRetryable business error
ERRORBusiness cannot proceed
FATALData corruption / catastrophic

4. Key Types

SyntaxTypeWrong ExampleCorrect Example
codeint32-code: 10001
messagestring-message: "user_not_found"
http_statusenumhttp_status: 404http_status: NOT_FOUND
phaseenumphase: "SERVICE"phase: SERVICE
severityenumseverity: 2severity: ERROR

5. Complete Example

Proto
syntax = "proto3";
package com.example.user;

import "apihug/protobuf/extend/constant.proto";
import "apihug/protobuf/swagger/annotations.proto";

// Authority enum
enum Authority {
  option (hope.swagger.enm) = {
    title: "Authority enum";
    description: "System permissions";
  };

  USER_CREATE = 0 [(hope.constant.field) = {
    code: 1001,
    message: "user:create",
    message2: "创建用户"
  }];

  USER_DELETE = 1 [(hope.constant.field) = {
    code: 1002,
    message: "user:delete",
    message2: "删除用户"
  }];
}

// Error code enum
enum UserError {
  USER_NOT_FOUND = 0 [(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
    }
  }];

  PASSWORD_INVALID = 1 [(hope.constant.field) = {
    code: 10002,
    message: "password_invalid",
    message2: "密码错误"
    error: {
      title: "Invalid Password",
      tips: "密码错误次数过多将锁定账户",
      http_status: UNAUTHORIZED,
      phase: CONTROLLER,
      severity: WARN
    }
  }];
}

6. Common Errors

Error 1: Type Confusion

Wrong:

Proto
error: {
  http_status: 404,      // Don't write numbers
  phase: "SERVICE",      // Don't add quotes
  severity: 2            // Don't write sequence numbers
}

Correct:

Proto
error: {
  http_status: NOT_FOUND,  // Enum name
  phase: SERVICE,
  severity: ERROR
}

Error 2: Duplicate code

Wrong:

Proto
enum UserError {
  ERROR_1 = 0 [(hope.constant.field) = {code: 10001}];
  ERROR_2 = 1 [(hope.constant.field) = {code: 10001}];  // Duplicate code
}

Correct:

Proto
enum UserError {
  ERROR_1 = 0 [(hope.constant.field) = {code: 10001}];
  ERROR_2 = 1 [(hope.constant.field) = {code: 10002}];  // Unique code
}

7. Quick Reference

Basic Template

Proto
syntax = "proto3";
package your_package_name;

import "apihug/protobuf/extend/constant.proto";

enum EnumName {
  VALUE_NAME = sequence [(hope.constant.field) = {
    code: business_code,
    message: "English",
    message2: "Chinese"
  }];
}

Error Code Template

Proto
enum ErrorEnumName {
  ERROR_NAME = sequence [(hope.constant.field) = {
    code: error_code,
    message: "identifier",
    message2: "Chinese description"
    error: {
      title: "Title",
      tips: "Tips",
      http_status: HTTP_STATUS,
      phase: PHASE,
      severity: SEVERITY
    }
  }];
}
Copyright © 2026 ApiHug·AI-native Enterprise Architecture Factory