errorx-basic

command
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: MIT Imports: 3 Imported by: 0

README

errorx basic example

最小可运行示例,展示 errorx 的构造、包装、检查三件套,不涉及 HTTP。

运行

go run ./examples/errorx-basic

预期输出

code: AIHUB_SKILL_CREATE_FAILED
status: 500
retryable: true
request_id: req_123

这个示例展示了什么

cause := errors.New("pq: connection refused")          // 底层错误
err := errorx.Wrap(cause, ErrSkillCreateFailed,         // 包装成业务错误
    errorx.WithMessage("创建技能失败"),
    errorx.WithHTTPStatus(500),
    errorx.WithRetryable(true),                         // 标记可重试
    errorx.WithMetadata("skill_id", "skill_001"),       // 内部 metadata
    errorx.WithRequestID("req_123"),                    // 请求 ID
    errorx.WithTraceID("trace_abc"),                    // 链路追踪 ID
)

// inspect helpers 从 err 中提取语义,无需类型断言
errorx.CodeOf(err)       // AIHUB_SKILL_CREATE_FAILED
errorx.HTTPStatusOf(err) // 500
errorx.RetryableOf(err)  // true
errorx.RequestIDOf(err)  // req_123

下一步

如果要看 HTTP handler 完整示例,跑 examples/errorx-http

go run ./examples/errorx-http

它展示了 errorx 在真实 HTTP 接口中的端到端用法(包括 JSON 响应、日志记录、metadata 分层)。

相关文档

  • errorx/README.md — 完整用户指南
  • docs/ai/errorx.md — AI 编码食谱

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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