Help us shape the future of Go! Take the 2026 Go Developer Survey

errcode

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package errcode 是 fleetly 错误码注册表(代码内唯一真源)。

契约纪律(架构 §2.8「契约版本化纪律」):错误码为稳定字符串 (`E_<域>_<条件>` / `W_<域>_<条件>`),永不复用、只新增;文档域清单 (release-semantics §2.7、stateful-placement §2.8/§2.9、state-model §2.7/§2.9、architecture §2.3/§2.4)为定义性说明,注册表为运行时真源。 每码携带默认 HTTP 状态映射、默认 suggestion 文案与 docs 锚点。

fail-fast:重复注册、非法格式、E_/W_ 与 HTTP 映射不一致均在构造期 (Default 注册表构建 = 包初始化)panic,保证「只增、不复用」在进程 启动即被校验。

Index

Constants

View Source
const DocsURLPrefix = "https://docs.fleetly.dev/errors/"

DocsURLPrefix 是错误码文档锚点 URL 前缀(占位:文档站域名与路径结构 待 T0.5 契约冻结定稿;锚点 = 前缀 + 码 ID)。

Variables

This section is empty.

Functions

func HTTPStatus

func HTTPStatus(id string) int

HTTPStatus 返回码的默认 HTTP 状态;未注册码返回 0(调用方须自行兜底, 注册表不发明文档外码)。

func IDs

func IDs() []string

func Len

func Len() int

Types

type Code

type Code struct {
	// ID 稳定码字符串,如 "E_COMPOSE_UNSUPPORTED"。
	ID string
	// HTTP 是该码的默认 HTTP 状态映射;W_ 警告码恒为 0(警告是资源上的
	// 标注/事件,不作为 HTTP 错误出现,发布专项 §2.7「部署失败是资源终态
	// 而非 HTTP 错误」同理适用)。
	HTTP int
	// Summary 一句话语义(注册表内说明,不进信封)。
	Summary string
	// Suggestion 默认修复建议(ErrorResponse.suggestion 的默认占位文案,
	// 业务侧可覆盖;措辞冻结前可调整)。
	Suggestion string
}

Code 是一个已注册错误码。

func All

func All() []Code

func Get

func Get(id string) (Code, bool)

Get / Len / IDs / All 的包级快捷方式(作用于 Default)。

func (Code) Docs

func (c Code) Docs() string

Docs 返回该码的文档锚点 URL(DocsURLPrefix + ID)。

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry 是错误码注册表(只增:注册成功后不可删除或改写)。

func Default

func Default() *Registry

Default 返回包级默认注册表。

func NewRegistry

func NewRegistry() *Registry

NewRegistry 构造空注册表。

func (*Registry) All

func (r *Registry) All() []Code

All 返回全部码(按 ID 字典序)。

func (*Registry) Get

func (r *Registry) Get(id string) (Code, bool)

Get 按码 ID 查询;未注册返回 false。

func (*Registry) IDs

func (r *Registry) IDs() []string

IDs 返回全部码 ID(字典序)。

func (*Registry) Len

func (r *Registry) Len() int

Len 返回已注册码数。

func (*Registry) MustRegister

func (r *Registry) MustRegister(c Code)

MustRegister 注册一个码:格式非法、重复注册、HTTP 映射与码类不一致时 panic(构造期 fail-fast)。

func (*Registry) Snapshot

func (r *Registry) Snapshot() string

Snapshot 返回注册表的规范化文本快照(golden 测试用):每行 ID \t HTTP \t docs \t summary \t suggestion,按 ID 字典序。

Jump to

Keyboard shortcuts

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