apperr

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package apperr 定义全系统统一的错误形态。

错误身份是错误码(Code),不是 Go 的错误值:apperr.Is(err, code) 在 进程内(原始错误)与跨网络(RFC 9457 反序列化重建)两种情况下行为一致, 这是单体/微服务双模式语义对齐的基石。错误码常量由合约仓库生成。

Index

Constants

View Source
const (
	CodeInternal         = "INTERNAL"             // 未分类内部错误,对外不泄漏细节
	CodeInvalidArgument  = "INVALID_ARGUMENT"     // 请求不合法
	CodeNotFound         = "NOT_FOUND"            //
	CodeConflict         = "CONFLICT"             // 并发/状态冲突
	CodeUnauthenticated  = "UNAUTHENTICATED"      //
	CodePermissionDenied = "PERMISSION_DENIED"    //
	CodeUnavailable      = "UNAVAILABLE"          // 依赖不可用/超时,可重试
	CodeTxBoundary       = "TX_BOUNDARY"          // 事务内发起跨模块调用(运行时守卫)
	CodeIdempotency      = "IDEMPOTENCY_CONFLICT" // 同幂等键不同 payload
	CodeMigrationDrift   = "MIGRATION_DRIFT"      // 已应用的迁移内容被改动(启动期守卫)
)

框架保留错误码。业务错误码由合约仓库(psp-contracts)定义。

Variables

This section is empty.

Functions

func Is

func Is(err error, code string) bool

Is 报告 err(或其链上任一错误)的错误码是否为 code。

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 Conflict

func Conflict(format string, args ...any) *Error

func From

func From(err error) *Error

From 把任意 error 规范化为 *Error:已是 *Error 原样返回, 其余折叠为 CodeInternal(对外只暴露"internal error",细节留在错误链里供日志)。

func FromProblem

func FromProblem(status int, body []byte) *Error

FromProblem 在客户端把 problem+json 响应体重建为 *Error。 解析失败时折叠为对应 HTTP 状态的 CodeInternal/CodeUnavailable。

func Internal

func Internal(cause error) *Error

常用构造捷径。

func InvalidArgument

func InvalidArgument(format string, args ...any) *Error

func New

func New(code string, status int, message string) *Error

New 构造一个错误模板。message 面向调用方,必须不含内部细节。

func NotFound

func NotFound(format string, args ...any) *Error

func Unavailable

func Unavailable(cause error) *Error

func (*Error) Code

func (e *Error) Code() string

func (*Error) Details

func (e *Error) Details() map[string]any

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

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

Is 支持 errors.Is(err, template):同码即同错误,忽略 message/details 差异。

func (*Error) Message

func (e *Error) Message() string

func (*Error) Problem

func (e *Error) Problem() Problem

Problem 把错误转为 RFC 9457 形态。内部错误链(cause)不出现在结果里。

func (*Error) Status

func (e *Error) Status() int

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap 暴露内部链,供 errors.Is/As 检查底层错误;内部链不会被序列化外泄。

func (*Error) WithCause

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

WithCause 返回携带内部错误链的副本。cause 只用于日志与 errors.Is/As,不外泄。

func (*Error) WithDetail

func (e *Error) WithDetail(key string, value any) *Error

WithDetail 返回追加了一条结构化细节的副本。细节会随响应外泄,勿放内部信息。

func (*Error) WithMessage

func (e *Error) WithMessage(format string, args ...any) *Error

WithMessage 返回替换了 message 的副本。

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 行为不变。

Jump to

Keyboard shortcuts

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