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

apperr

package
v0.1.0-rc1 Latest Latest
Warning

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

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

Documentation

Overview

Package apperr 是携带 fleetly 错误信封的应用错误类型。

错误信封契约(发布专项 §2.7、proto/fleetly/shared/v1/error.proto): {code, message, phase, deployment_id, suggestion, context, docs} 七字段; code 必须来自 errcode 注册表(唯一真源,只增不复用)。gRPC 侧以 status detail(*sharedv1.ErrorResponse)携带信封;REST 侧由 gateway HTTPErrorHandler 解出信封渲染 snake_case JSON。

兜底取舍(待 T0.5 冻结确认):无信封 detail 的错误(传输层/框架层错误) 退化为「code 留空串 + message 保底」的通用信封,HTTP 状态由 grpc code 机械映射——不发明文档外码、不挪用既有码语义。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvelopeFromGRPCStatus

func EnvelopeFromGRPCStatus(st *status.Status) (*sharedv1.ErrorResponse, int)

EnvelopeFromGRPCStatus 是 gateway 错误链的统一出口:有信封 detail 时 返回信封与注册表 HTTP 状态;无 detail 走退化信封——code 留空串(不发明 文档外码、不挪用既有码语义),message 取 status 原文保底,HTTP 状态由 grpc code 机械映射。兜底形态待 T0.5 契约冻结确认。

func GRPCCodeToHTTP

func GRPCCodeToHTTP(c codes.Code) int

GRPCCodeToHTTP 是 HTTPToGRPCCode 的传输侧逆向映射(gateway 对无信封 错误的兜底;与 torchwood grpcCodeToHTTP 同形)。

func HTTPToGRPCCode

func HTTPToGRPCCode(httpStatus int) codes.Code

HTTPToGRPCCode 把注册表 HTTP 状态机械映射为 gRPC code(信封承载契约, 传输 code 仅求稳定可逆;409/410 统一 FailedPrecondition——冲突/前置失效 语义,细分由信封 code 表达)。

Types

type Error

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

Error 是携带完整错误信封的应用错误。零值不可用;经 New/With* 构造。

func FromError

func FromError(err error) (*Error, bool)

FromError 是 FromGRPCStatus 的 error 入口(非 status 错误返回 false)。

func FromGRPCStatus

func FromGRPCStatus(st *status.Status) (*Error, bool)

FromGRPCStatus 从 gRPC status 还原 *Error:找到 *sharedv1.ErrorResponse detail 时返回 (err, true)——注册码恢复注册表默认 suggestion/docs(detail 内值优先);无 detail 返回 (nil, false),由调用方走退化信封。

func New

func New(code, messageFormat string, args ...any) *Error

New 构造注册表内错误码的应用错误:suggestion/docs 取注册表默认 (可经 WithSuggestion 覆盖文案)。code 未注册时 panic(构造期 fail-fast, 保证信封 code 恒为注册表内码);W_ 警告码同样 panic——警告是资源/计划 上的标注(HTTP=0),永不作为错误返回。

func (*Error) Code

func (e *Error) Code() string

Code 返回稳定错误码(退化信封为空串)。

func (*Error) Context

func (e *Error) Context() map[string]string

Context 返回结构化上下文(只读用途)。

func (*Error) Envelope

func (e *Error) Envelope() *sharedv1.ErrorResponse

Envelope 构造错误信封 proto(七字段;空值字段由 marshaler 语义决定 是否输出)。

func (*Error) Error

func (e *Error) Error() string

Error 实现 error 接口:"CODE: message";无码(信封还原路径)时仅 message。

func (*Error) GRPCStatus

func (e *Error) GRPCStatus() *status.Status

GRPCStatus 使 *Error 直接满足 gRPC 传输的错误接口(grpc status 包对 实现 GRPCStatus() *status.Status 的错误原样采用,detail 随之上线), handler 可直接 return *Error。

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() int

HTTPStatus 返回该错误的默认 HTTP 状态:注册码按注册表映射,无码兜底 500(退化信封在 gateway 侧另经 GRPCCodeToHTTP 机械映射)。

func (*Error) Is

func (e *Error) Is(target error) bool

Is 支持 errors.Is:同为 *Error 且 code 相等(非空)即判定同一错误类别; 否则沿 Unwrap 链由 errors 包判定。

func (*Error) Message

func (e *Error) Message() string

Message 返回人读信息。

func (*Error) ToGRPCStatus

func (e *Error) ToGRPCStatus() *status.Status

ToGRPCStatus 转为携带信封 detail 的 gRPC status:grpc code 由注册表 HTTP 映射机械推导,detail 为 *sharedv1.ErrorResponse。

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap 暴露底层原因(errors.Is/As 沿链判定)。

func (*Error) WithCause

func (e *Error) WithCause(cause error) *Error

WithCause 记录底层原因错误(errors.Unwrap 链保留)。

func (*Error) WithContext

func (e *Error) WithContext(key, value string) *Error

WithContext 附加结构化上下文键值(machine-readable,同名覆盖)。

func (*Error) WithDeploymentID

func (e *Error) WithDeploymentID(id string) *Error

WithDeploymentID 关联部署 ID。

func (*Error) WithPhase

func (e *Error) WithPhase(phase string) *Error

WithPhase 附加发布阶段(如 resolve/build/deploy/serve)。

func (*Error) WithSuggestion

func (e *Error) WithSuggestion(s string) *Error

WithSuggestion 覆盖注册表默认建议文案。

Jump to

Keyboard shortcuts

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