protoc-gen-errno

command
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Command protoc-gen-errno is a protoc plugin that turns a proto enum into a semantic error catalog, one constructor per value.

The catalog is expressed with the two extensions errno.proto declares:

enum UserStatus {
  option (onexmesh.errno.v1.default_code) = 400;   // for values without one
  Active = 0;
  Banned = 2 [(onexmesh.errno.v1.code) = 403];
}

and for each value it emits a Reason constant plus an errorsx constructor:

var ReasonBanned = "Banned"
func ErrorBanned(msg string) *errorsx.ErrorX

The file is named *_errno.pb.go after the annotated proto. A proto that declares no enum produces no file at all -- the plugin is additive, and a file with nothing to say would only be an empty package-level artifact.

Two things are worth knowing before changing this plugin:

  • (onexmesh.errno.v1.code) is optional, and so is the enum-level default_code. A value that sets neither falls back to 500. That is not a placeholder: a proto enum doubles as an error catalog precisely when a value can be listed before its status code is decided, and an unclassified value must still produce a constructor rather than be skipped.
  • Consequently this plugin skips nothing. The sibling generator protoc-gen-go-errors (Kratos) drops every value whose code is 0, which is why its output and this one differ on the same input even though both walk an enum the same way.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL