Documentation
¶
Overview ¶
Package apperr 定义全系统统一的错误形态。
错误身份是错误码(Code),不是 Go 的错误值:apperr.Is(err, code) 在 进程内(原始错误)与跨网络(RFC 9457 反序列化重建)两种情况下行为一致, 这是单体/微服务双模式语义对齐的基石。错误码常量由合约仓库生成。
Index ¶
- Constants
- func Is(err error, code string) bool
- func WriteProblem(w http.ResponseWriter, err error)
- type Error
- func Conflict(format string, args ...any) *Error
- func From(err error) *Error
- func FromProblem(status int, body []byte) *Error
- func Internal(cause error) *Error
- func InvalidArgument(format string, args ...any) *Error
- func New(code string, status int, message string) *Error
- func NotFound(format string, args ...any) *Error
- func Unavailable(cause error) *Error
- func (e *Error) Code() string
- func (e *Error) Details() map[string]any
- func (e *Error) Error() string
- func (e *Error) Is(target error) bool
- func (e *Error) Message() string
- func (e *Error) Problem() Problem
- func (e *Error) Status() int
- func (e *Error) Unwrap() error
- func (e *Error) WithCause(cause error) *Error
- func (e *Error) WithDetail(key string, value any) *Error
- func (e *Error) WithMessage(format string, args ...any) *Error
- type Problem
Constants ¶
const ( CodeInternal = "INTERNAL" // 未分类内部错误,对外不泄漏细节 CodeInvalidArgument = "INVALID_ARGUMENT" // 请求不合法 CodeNotFound = "NOT_FOUND" // CodeConflict = "CONFLICT" // 并发/状态冲突 CodeUnauthenticated = "UNAUTHENTICATED" // CodePermissionDenied = "PERMISSION_DENIED" // CodeTxBoundary = "TX_BOUNDARY" // 事务内发起跨模块调用(运行时守卫) CodeIdempotency = "IDEMPOTENCY_CONFLICT" // 同幂等键不同 payload CodeMigrationDrift = "MIGRATION_DRIFT" // 已应用的迁移内容被改动(启动期守卫) )
框架保留错误码。业务错误码由合约仓库(psp-contracts)定义。
Variables ¶
This section is empty.
Functions ¶
func WriteProblem ¶
func WriteProblem(w http.ResponseWriter, err error)
WriteProblem 把 err 规范化后写为 problem+json 响应。 服务端出口统一走这里,保证响应形态唯一。
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error 是唯一跨层传播的错误类型。值不可变:With* 方法返回副本, 因此包级 sentinel(var ErrX = apperr.New(...))可以安全共享。
func From ¶
From 把任意 error 规范化为 *Error:已是 *Error 原样返回, 其余折叠为 CodeInternal(对外只暴露"internal error",细节留在错误链里供日志)。
func FromProblem ¶
FromProblem 在客户端把 problem+json 响应体重建为 *Error。 解析失败时折叠为对应 HTTP 状态的 CodeInternal/CodeUnavailable。
func InvalidArgument ¶
func Unavailable ¶
func (*Error) WithDetail ¶
WithDetail 返回追加了一条结构化细节的副本。细节会随响应外泄,勿放内部信息。
type Problem ¶
type Problem struct {
Type string `json:"type,omitempty"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail,omitempty"`
Code string `json:"code"`
Details map[string]any `json:"details,omitempty"`
}
Problem 是 RFC 9457 (application/problem+json) 响应体。 code 是扩展成员,承载错误身份;跨网络重建后 apperr.Is 行为不变。