Compare commits

...

6 Commits

72 changed files with 4188 additions and 135 deletions

44
API.md
View File

@@ -111,6 +111,44 @@ service user-api {
## 业务错误:声明、代码和文档共用一份定义 ## 业务错误:声明、代码和文档共用一份定义
**错误目录也可以只在 Go 中维护。** 从 `v0.11.1-preview` 起,生成路由没有声明错误列表时,继承 `omni.WithErrors(catalog)` 注册的 App 目录;`.api` 继续定义接口和返回格式,无需重复登记错误。全局中间件提前返回同样适用。显式引用 `.api` 错误时才使用路由独立目录,规则见 [目录继承](errors/README.md)。CLI 不执行运行时 Go 配置,因此静态文档不会自动枚举只在 Go 中定义的具体错误。
从 `v0.10.0-preview` 起,推荐用 `errorCatalog` 同时定义逻辑标识、对外业务码、提示和 HTTP 状态:
```text
errorCatalog CommonErrors (
defaultStatus: 200
errors: {
"UsernameInvalid": {"key": "USERNAME_INVALID", "code": 400002, "message": "用户名格式不正确"},
"PasswordInvalid": {"key": "PASSWORD_INVALID", "code": 400002, "message": "密码格式不正确"},
"Forbidden": {"key": "FORBIDDEN", "status": 403, "code": 403001, "message": "没有权限"}
}
)
```
对象键是生成函数名;`key` 是唯一逻辑标识,允许项目自己的命名;`code` 是对外业务码,可以重复。数字码需搭配 `codeType: int32` 或 `int64` 的公共响应;字符串码搭配 `string`。目录不规定返回体字段。
省略 `defaultStatus` 时为 400;设为 200 时,只作用于目录里未单独写 `status` 的条目。生成路由自动绑定它实际声明的错误目录,在 App 中间件之前生效。业务只调用 `apierrors.UsernameInvalid()`;无需再维护 `codes` / `messages`,也无需修改 main。手写路由可复用生成的 `apierrors.Catalog()`。
同一 HTTP 200 下,OpenAPI 用 `anyOf` 保留成功 DTO 和错误体,日志与指标仍识别业务失败。完整语法、路由作用域与手写项目配置见 [统一错误目录](errors/README.md),可运行契约见 [errors.api](examples/contracts/errors.api)。
从 `v0.11.0-preview` 起,目录条目支持 `params` 有序列表与 `{name}` 提示模板:
```json
"TextTooLong": {
"key": "TEXT_TOO_LONG", "code": 400002,
"message": "{label}不能超过{max}个字符",
"params": [
{"name": "label", "type": "string", "example": "企业名称"},
{"name": "max", "type": "int", "example": 100}
]
}
```
生成 `apierrors.TextTooLong(label string, max int)`,返回时 Message 已填好;固定提示保持无参数函数。声明顺序决定参数顺序。模板校验、运行时缺参/类型错误、转义及非 HTTP 使用规则见 [提示模板参数](errors/README.md)。OpenAPI 的错误目录保留参数定义,响应示例使用具体文案。
原有声明继续支持:
```go ```go
error ( error (
Unauthenticated 401 "UNAUTHENTICATED" "authentication required" Unauthenticated 401 "UNAUTHENTICATED" "authentication required"
@@ -121,10 +159,10 @@ error (
``` ```
也可写单条 `error EmailTaken 409 "EMAIL_TAKEN" "email already in use"`,或把错误目录放进相对导入的 `errors.api`。 也可写单条 `error EmailTaken 409 "EMAIL_TAKEN" "email already in use"`,或把错误目录放进相对导入的 `errors.api`。
名称用 UpperCamelCase,HTTP 状态范围 400–599,错误码用大写字母/数字/下划线且全局唯一,message 是可公开返回的稳定提示。 名称用 UpperCamelCase,HTTP 状态允许 200–599 中可携带响应体的最终状态(排除 204/205/304),旧声明的逻辑错误码用大写字母/数字/下划线且全局唯一,message 是可公开返回的稳定提示。`Catalog` 为生成函数保留名称。
`@server (errors: Unauthenticated)` 声明组级错误;路由 `@errors UserNotFound, EmailTaken` 追加到组级列表并去重。 `@server (errors: Unauthenticated)` 声明组级错误;路由 `@errors UserNotFound, EmailTaken` 追加到组级列表并去重。
`@errors none` 清空继承的业务错误。未声明、重复声明或生成符号冲突会报告源码位置。 `@errors none` 清空 `.api` 分组继承的错误列表,随后按未设置路由目录处理,继承 App 错误目录。未声明、重复声明或生成符号冲突会报告源码位置。
生成 `internal/apierrors/errorsGen.go`: 生成 `internal/apierrors/errorsGen.go`:
@@ -138,7 +176,7 @@ return nil, apierrors.EmailTaken()
OpenAPI 对每个状态按选定的响应模板生成结构和命名 examples,同一 409 下多个错误不会相互覆盖;平铺模式的纯业务状态还列出 code enum。 OpenAPI 对每个状态按选定的响应模板生成结构和命名 examples,同一 409 下多个错误不会相互覆盖;平铺模式的纯业务状态还列出 code enum。
默认框架错误状态与自动添加的鉴权 401 保留开放的 code 字段,容纳校验器/中间件实际错误。 默认框架错误状态与自动添加的鉴权 401 保留开放的 code 字段,容纳校验器/中间件实际错误。
`x-omni-errors` 提供错误目录与路由关联。文档声明不拦截、改写或穷举运行时的所有错误。 `x-omni-errors` 提供错误目录与路由关联。目录解析已定义的错误,不自动推断数据库或其他未声明错误的业务含义,也不穷举所有运行时错误。
## 鉴权:声明与路由注册一起变化 ## 鉴权:声明与路由注册一起变化

View File

@@ -1,5 +1,30 @@
# Changelog # Changelog
## v0.11.1-preview
- 修复生成路由用空错误目录遮蔽 App 目录的问题;空 WithErrors 完全透传,避免 Context 与 Encoder 二次解析时丢失状态、业务码和中文提示。
- 未声明错误列表的路由继承 App,支持错误只在 Go 中维护;显式路由目录保持独立,响应格式、RawJSON、body limit 和全局中间件提前返回保持正确策略。
- 兼容已有 v0.11.0 生成代码;CLI 不再为空列表绑定目录。补充纯 Go 错误目录的实际生成消费者与回归验收。
## v0.11.0-preview
- 错误目录增加有序 params 和命名提示模板,生成带类型的错误函数;固定提示保持兼容,业务不再自行拼接文案。
- 新增 WithParams、参数校验与共用解析器;复制请求参数、保留错误链,重复解析和缓冲链路保留填充结果。非法调用转系统兜底,访问日志提供不含参数值的诊断。
- OpenAPI 错误目录同步参数类型/示例,响应示例由运行时解析器生成;JSON/YAML 配置同样支持模板。
- 本地/共享幂等重放保存已解析公开错误快照,不保存原始参数或 cause;Redis Lua 重写记录不损失 int64 业务码精度。
- 模板编译拒绝未声明/未使用参数、非法类型及示例;模板错误不能再维护同标识的旧 messages 映射。
- CLI 新生成代码最低要求 v0.11.0-preview;已生成的固定错误函数和 v0.10.0 调用继续支持。
## v0.10.0-preview
- 新增 App/路由独立错误目录:`omni.WithErrors`、`errors.Catalog`、`ByCode` 与不可变 Registry,业务只选逻辑标识;对外码和提示由项目配置。
- 默认 HTTP 状态与逐项覆盖均可配置,包括 HTTP 200 的业务失败;错误模板、RawJSON、日志、业务失败指标、链路、标准中间件桥接和幂等重放保留失败语义。
- `.api errorCatalog` 同源生成 Catalog、独立错误函数、路由目录和 OpenAPI;允许重复对外码、不同路由不同码类型、公共契约导入。旧 error 声明也允许 HTTP 200。
- 同一 200 的成功/错误响应使用 anyOf 保留两种 schema,错误示例保持各自提示;自定义 Encoder 和直接输出继续支持。
- 目录校验类型、范围、状态和冲突;JSON 配置拒绝重复键,整数保留 int64 精度;未知标识使用系统兜底,默认公开响应不暴露 cause。
- 新生成代码最低要求 v0.10.0-preview;原有构造函数、Codes/Messages 与旧生成代码继续可用。Catalog 为生成错误包保留函数名。
## v0.9.1-preview ## v0.9.1-preview
- 新增标准 `database.OpenConnector` 和独立 `PoolConfig`;与 DSN 入口共享池参数、限时启动 Ping、失败清理、Health、事务,不注册全局驱动配置。 - 新增标准 `database.OpenConnector` 和独立 `PoolConfig`;与 DSN 入口共享池参数、限时启动 Ping、失败清理、Health、事务,不注册全局驱动配置。

View File

@@ -2,7 +2,7 @@
GoOmni 是面向多个业务项目复用的 Go 后端框架:用 `.api` 定义接口、请求和返回类型,生成项目目录、HTTP 接入代码及 OpenAPI,业务代码负责 Service 和 Repository。也可以在已有项目中直接使用 `omni.New()`,或单独引入日志、存储等模块。 GoOmni 是面向多个业务项目复用的 Go 后端框架:用 `.api` 定义接口、请求和返回类型,生成项目目录、HTTP 接入代码及 OpenAPI,业务代码负责 Service 和 Repository。也可以在已有项目中直接使用 `omni.New()`,或单独引入日志、存储等模块。
**从这份 README 开始即可接入。** 当前版本为 **`v0.9.1-preview`**,Go **1.22+**;CLI 和项目依赖使用同一固定版本。框架不依赖 Gin,默认使用标准库网络层,提供可切换的 native HTTP/1.1 和实验性 HTTP/2 适配器。 **从这份 README 开始即可接入。** 当前版本为 **`v0.11.1-preview`**,Go **1.22+**;CLI 和项目依赖使用同一固定版本。框架不依赖 Gin,默认使用标准库网络层,提供可切换的 native HTTP/1.1 和实验性 HTTP/2 适配器。
响应字段、业务码、登录方式、权限规则和数据库表结构由接入项目决定。文中的 `code/msg/data`、`200/9999` 都是示例配置,可以替换。 响应字段、业务码、登录方式、权限规则和数据库表结构由接入项目决定。文中的 `code/msg/data`、`200/9999` 都是示例配置,可以替换。
@@ -26,7 +26,7 @@ module 地址是 `git.yuntuo.hk/k2safe/go-omni`。先确保本机 Go 和 Git 可
```sh ```sh
export GOPRIVATE=git.yuntuo.hk export GOPRIVATE=git.yuntuo.hk
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.9.1-preview go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.11.1-preview
export PATH="$(go env GOPATH)/bin:$PATH" export PATH="$(go env GOPATH)/bin:$PATH"
go-omni --help go-omni --help
``` ```
@@ -38,7 +38,7 @@ go-omni --help
在准备存放业务项目的目录执行,`./hello` 必须是新目录或空目录;把 `example.com/hello` 换成自己的 Go module 名称。 在准备存放业务项目的目录执行,`./hello` 必须是新目录或空目录;把 `example.com/hello` 换成自己的 Go module 名称。
```sh ```sh
go-omni api new --framework-version v0.9.1-preview \ go-omni api new --framework-version v0.11.1-preview \
--module example.com/hello ./hello --module example.com/hello ./hello
cd hello cd hello
go mod tidy go mod tidy
@@ -126,7 +126,7 @@ service catalog-api {
```sh ```sh
go-omni api validate --api catalog.api go-omni api validate --api catalog.api
go-omni new --api catalog.api --framework-version v0.9.1-preview \ go-omni new --api catalog.api --framework-version v0.11.1-preview \
--module example.com/catalog ./catalog --module example.com/catalog ./catalog
cd catalog cd catalog
go mod tidy go mod tidy
@@ -167,21 +167,21 @@ curl -i http://127.0.0.1:8080/api/v1/items/missing
需要 MySQL、文件存储或文件日志时,**首次生成**可以使用: 需要 MySQL、文件存储或文件日志时,**首次生成**可以使用:
```sh ```sh
go-omni new --api catalog.api --framework-version v0.9.1-preview \ go-omni new --api catalog.api --framework-version v0.11.1-preview \
--module example.com/catalogFull --database mysql --storage local \ --module example.com/catalogFull --database mysql --storage local \
--log-output file --log-format json ./catalogFull --log-output file --log-format json ./catalogFull
``` ```
该命令在 `catalog.api` 所在目录执行,目标仍需是新目录。数据库迁移、表结构和 Repository 由项目维护,API 模板不自动建表;MySQL DSN 通过 `GO_OMNI_DB_DSN` 提供。存储还可选 `s3`、`r2`、`oss`,按生成项目的配置说明提供桶、端点和凭据。完整装配例子见 [userService](examples/userService/README.md)。 该命令在 `catalog.api` 所在目录执行,目标仍需是新目录。数据库迁移、表结构和 Repository 由项目维护,API 模板不自动建表;MySQL DSN 通过 `GO_OMNI_DB_DSN` 提供。存储还可选 `s3`、`r2`、`oss`,按生成项目的配置说明提供桶、端点和凭据。完整装配例子见 [userService](examples/userService/README.md)。
不采用 `.api` 时,也可以用 `go-omni new --framework-version v0.9.1-preview --module example.com/billing ./billing` 创建手写接口的 CRUD 示例项目;两种生成方式选择一种即可。 不采用 `.api` 时,也可以用 `go-omni new --framework-version v0.11.1-preview --module example.com/billing ./billing` 创建手写接口的 CRUD 示例项目;两种生成方式选择一种即可。
## 已有项目:按模块接入 ## 已有项目:按模块接入
在业务项目的 **go.mod 所在目录**执行: 在业务项目的 **go.mod 所在目录**执行:
```sh ```sh
go get git.yuntuo.hk/k2safe/go-omni@v0.9.1-preview go get git.yuntuo.hk/k2safe/go-omni@v0.11.1-preview
``` ```
已有项目可直接复用现有目录、Service、Repository 和数据库连接,先选择一个接口接入。也可以只用 `logx`、`storage` 等包,暂时保留原来的 HTTP 框架。 已有项目可直接复用现有目录、Service、Repository 和数据库连接,先选择一个接口接入。也可以只用 `logx`、`storage` 等包,暂时保留原来的 HTTP 框架。
@@ -307,7 +307,73 @@ auth UserAuth (
生成器会创建 `internal/middleware/auth.go`,由项目实现凭据校验和身份注入。声明会生成路由绑定与 OpenAPI 的认证描述,**不会自动实现登录或授权规则**;未实现的 hook 返回 501。手写路由可使用 `auth.RequireAuth(authenticator)` 或自己的中间件。JWT、Session 和 Policy 按需选择。 生成器会创建 `internal/middleware/auth.go`,由项目实现凭据校验和身份注入。声明会生成路由绑定与 OpenAPI 的认证描述,**不会自动实现登录或授权规则**;未实现的 hook 返回 501。手写路由可使用 `auth.RequireAuth(authenticator)` 或自己的中间件。JWT、Session 和 Policy 按需选择。
业务错误在 `.api` 中用 `error` 声明,通过 `@errors` 关联路由后,生成可调用的错误函数和 OpenAPI 中对应的 401、403、409 等响应。手写路由可以直接返回 `omni.NotFound`、`omni.Forbidden` 等错误。已有项目在自己的适配层转换原错误,内部原因和公开提示分开处理。 业务错误可以集中定义,业务只选择发生了哪个错误。**HTTP 状态由项目配置,业务失败可以返回 HTTP 200**;框架不根据 HTTP 状态单独判断成功与否。
错误目录支持两种维护方式,按项目选择一种:**Go 中定义并用 `omni.WithErrors` 注册一次**,或者在公共 `.api` 中声明并生成调用函数。前者不要求 `.api` 重复声明目录或逐路由列出错误;生成路由默认继承 App 目录。
采用 `.api` 管理时,其他契约可通过 `import` 引用:
```text
errorCatalog CommonErrors (
defaultStatus: 200
errors: {
"UsernameInvalid": {"key": "USERNAME_INVALID", "code": 400002, "message": "用户名格式不正确"},
"PasswordInvalid": {"key": "PASSWORD_INVALID", "code": 400002, "message": "密码格式不正确"},
"PermissionDenied": {"key": "PERMISSION_DENIED", "status": 403, "code": 403001, "message": "没有权限"}
}
)
```
`UsernameInvalid` 是生成函数名;`key` 是唯一逻辑标识;`code` 是项目对外的字符串或整数业务码,可以重复。条目没有填写 `status` 时使用目录的 `defaultStatus`,未配置目录默认值时为 400。这个默认值只作用于该目录的条目,不会把所有框架异常都改成 200。
在已有公共响应中保留 `codeType: int32` / `int64` / `string`、成功码、未定义错误的兜底码和返回体模板;不再重复填写这几条错误的 `codes` / `messages`。通过 `@server (errors: UsernameInvalid, PasswordInvalid)` 或路由 `@errors UsernameInvalid` 引用它们。
生成后业务调用:
```go
return TokenResponse{}, apierrors.UsernameInvalid()
// 需要保留内部原因时:
return TokenResponse{}, apierrors.UsernameInvalid().WithCause(err)
```
生成器同时提供 `internal/apierrors.Catalog()`、错误函数、路由错误目录及 OpenAPI。目录会在 App 中间件执行前绑定到生成路由;无需手动改已经生成的 main。HTTP 200 的错误使用失败响应模板;OpenAPI 用 `anyOf` 同时保留成功 DTO 和错误体,多条错误共用数字码时仍保留各自示例。完整契约见 [errors.api](examples/contracts/errors.api)。
手写项目可在启动时加载自己的目录,保留原来的响应配置:
```go
// omnierrors 是 git.yuntuo.hk/k2safe/go-omni/errors。
catalog := omnierrors.Catalog{
DefaultStatus: 200,
Definitions: map[string]omnierrors.Definition{
"USERNAME_INVALID": {Code: 400002, Message: "用户名格式不正确"},
"PASSWORD_INVALID": {Code: 400002, Message: "密码格式不正确"},
},
}
app := omni.New(
omni.WithErrors(catalog),
omni.WithResponse(response.Config{
CodeType: "int32", SuccessCode: 0, ErrorCode: 9999,
MessageField: "msg", DataMode: "always",
}),
)
// Service 只返回标准 error,不依赖 App 或 HTTP Context:
// return omnierrors.ByCode("USERNAME_INVALID").WithCause(err)
```
目录也可放入 `config.Config.Errors`,由 JSON/YAML 加载;生成项目的路径为 `server.errors`,生成路由只有显式引用错误定义时才使用 `.api` 独立目录,未声明则继承 App。不同 App 独立持有配置,单路由也可通过 `response.WithErrors` 绑定专用目录与自定义 Encoder。直接输出文本、XML、下载或已提交的响应继续保留原协议。
访问日志保留实际 HTTP 状态,并增加失败标记、逻辑错误标识和目录业务码;指标、链路及框架标准中间件/幂等重放也保留失败语义。未知标识使用系统错误兜底,内部 cause 不写入默认公开响应。旧 `error (...)`、`New` / `BadRequest` / `Codes` / `Messages` 继续支持;新目录与旧映射冲突会报错,避免静默覆盖。
动态提示也可以集中配置,在目录中声明 `message: "{label}不能超过{max}个字符"` 和有序 `params`(label/string、max/int),生成带类型的调用:
```go
return apierrors.TextTooLong("企业名称", 100)
// 最终提示:企业名称不能超过100个字符
```
运行时支持 `omnierrors.ByCode("TEXT_TOO_LONG").WithParams(...)`,与生成函数共用解析器。固定错误继续无参数调用;缺参或类型错误使用系统兜底。模板、参数类型及具体响应示例同步写入 OpenAPI。完整示例见 [errorParams.api](examples/contracts/errorParams.api)。
更详细的配置、作用域和升级步骤见 [统一错误目录](errors/README.md)。
### 数据库、存储及其他模块 ### 数据库、存储及其他模块
@@ -383,7 +449,7 @@ go test ./...
`Gen.go`、`docs/openapi.json` 和 manifest 由生成器管理,手动修改会报告冲突。Service、Repository、中间件实现、main、配置和依赖装配属于项目,重复生成保留手写内容;契约改变后仍需自行调整业务签名并通过编译。将 `.api`、生成文件和 `.goOmni/apiManifest.json` 一起提交,CI 可运行 `generate --check` 检查是否同步。 `Gen.go`、`docs/openapi.json` 和 manifest 由生成器管理,手动修改会报告冲突。Service、Repository、中间件实现、main、配置和依赖装配属于项目,重复生成保留手写内容;契约改变后仍需自行调整业务签名并通过编译。将 `.api`、生成文件和 `.goOmni/apiManifest.json` 一起提交,CI 可运行 `generate --check` 检查是否同步。
升级时先安装目标版本的 CLI,再在业务 module 中 `go get` **同一版本**;API 项目随后执行上述生成与测试步骤,手写项目只需依赖升级及业务回归。`v0.9.1-preview` 的变化、迁移边界和验收记录见 [发布说明](RELEASE.md)。 升级时先安装目标版本的 CLI,再在业务 module 中 `go get` **同一版本**;API 项目随后执行上述生成与测试步骤,手写项目只需依赖升级及业务回归。`v0.11.1-preview` 的变化、迁移边界和验收记录见 [发布说明](RELEASE.md)。
## 按需深入 ## 按需深入

View File

@@ -1,45 +1,40 @@
# v0.9.1-preview # v0.11.1-preview
本版补齐已有项目接入的两个框架缺口:标准数据库 Connector 注入,以及应用级 HTTP 读写超时配置。TLS、数据库驱动日志、字符集/时区、迁移与业务规则继续由项目决定。 修复生成路由使用空错误目录遮蔽 `omni.WithErrors(catalog)` 的问题。错误现在可以只在 Go 中注册一次,`.api` 无需重复定义错误目录或逐条列出错误。
## 安装与升级 ## 行为
- 未声明路由错误列表:继承 App 目录,包括带参数提示及项目自定义 INTERNAL_ERROR 兜底。
- 显式引用路由错误:继续使用路由独立目录,不与 App 合并。
- `@errors none` 清空 `.api` 分组错误列表,随后继承 App。
- 错误继承不覆盖 `.api` 的返回格式、RawJSON、手写绑定或请求体限额;全局中间件提前返回、标准桥接与克隆保持一致。
- 空 `response.WithErrors` / `MustWithErrors` 返回原 Encoder,既不覆盖 Context 的目录,也不在编码阶段按空目录重新解析错误。
## 升级
```sh ```sh
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.9.1-preview go get git.yuntuo.hk/k2safe/go-omni@v0.11.1-preview
# 在各业务 module 中: go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.11.1-preview
go get git.yuntuo.hk/k2safe/go-omni@v0.9.1-preview go-omni api generate --dry-run
go-omni api generate
go test ./...
``` ```
私有仓库使用本机 Git 凭据管理器,并将 `git.yuntuo.hk` 追加到已有 `GOPRIVATE`。CLI 和项目依赖使用同一固定版本。v0.9.0-preview 生成代码继续兼容,本次无需为了这两个入口重新生成接口。 运行时修复也适用于旧 v0.11.0 生成代码,因此升级运行时后即可解除空目录遮蔽。同步升级 CLI 重新生成时不再创建空目录包装;已生成业务文件继续受保护。代码生成最低运行时仍为 v0.11.0-preview。
## 接入变化 接入见 [README](README.md)、[错误目录继承](errors/README.md) 和 [只在 Go 注册错误的契约示例](examples/contracts/errorInheritance.api)。静态 OpenAPI 不执行 Go 配置,保留契约响应结构,不自动列举运行时目录内容。
- 将项目已有 `sql.OpenDB(connector)` 的池创建替换为 `database.OpenConnector(ctx, connector, poolConfig)`;不要同时保留两个池。返回对象可用于 Health/InTx;需要标准库指针时取 `.DB`。 ## 验收
- 项目原有 `mysql.NewConnector`、独立 TLS、driver Logger、驱动读写时限、MySQL 版本/时区/排序规则校验继续保留,配置不进入核心框架。
- 将原来逐项修改 `http.Server` 的读写/空闲/头大小配置放入 `omni.HTTPConfig`,通过 `WithHTTPConfig` 或 `WithConfig` 传入。Go-only `ErrorLog` 支持 nethttp。
- 原有 `database.Open(ctx, Config)`、手动组装 `http.Server`、自定义 Runner 均可继续使用。零 HTTP 配置保留所选网络层已有默认行为;正值覆盖,负值报错。
完整可复制示例:[数据库 Connector](database/README.md#connector-接入v091-preview)、[HTTP 配置及语义](transport/README.md#http-配置v091-preview)。其他迁移能力见 [迁移扩展指南](MIGRATION.md),从项目接入开始见 [README](README.md)。 复现用例在修复前:普通路由通过,生成/RawJSON 路由和提前返回中间件共 12 项失败,均变成 500/英文系统提示。修复后同一批用例通过。
## 边界
独立实验性 `TransportHTTP2` 本次支持 IdleTimeout/MaxHeaderBytes 的配置透传,但不支持读头、读请求和写响应超时,设置这些正值会在监听前明确报错。native 与标准库服务器的超时含义不完全相同,详见上表;网络超时不等于 Handler 执行时间限制。`RunWith` 自定义 Runner 的配置由项目管理。
Connector 必须遵守驱动 context 协议,资源所有权按标准 `database/sql` 管理。有资源的 Connector 不跨池共享;启动 Ping 失败关闭池,成功后应用负责关闭。没有把 eTax/ePay 的数据库策略写进框架,也未修改这些业务项目或宣称它们已完成迁移。
## 验证入口
```sh ```sh
GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./... GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./...
go vet ./... go vet ./...
go build ./... go build ./...
python3 scripts/verifyConnector.py --local --report performance/connectorLocal.json python3 scripts/verifyErrorInheritanceRelease.py --version v0.11.1-preview
python3 scripts/verifyConnector.py --version v0.9.1-preview --report performance/connectorRemote.json python3 scripts/verifyErrorParamsRelease.py --version v0.11.1-preview --report performance/errorInheritanceParamsRemote.json
python3 scripts/verifyCompatibilityRelease.py --version v0.9.1-preview --report performance/connectorCompatibilityRemote.json python3 scripts/verifyCompatibilityRelease.py --version v0.11.1-preview --report performance/errorInheritanceCompatibilityRemote.json
``` ```
Connector 脚本只使用本地独立 MySQL 8.4 容器,限制 1 CPU、768 MiB、128 PID,生成一次性 CA/证书及凭据,结束清理自己的容器/卷/临时文件。测试验证正确 TLS、错误 CA/主机名拒绝、每池时区/日志配置隔离、事务、关闭与失败回收。HTTP 测试使用回环地址验证慢头、慢 body、空闲连接、流式写背压及请求头上限。常规单测不依赖 Docker,显式脚本要求真实 TLS 测试通过,不能跳过。 结果见 [目录继承验收](performance/errorInheritanceAcceptance.md)。本轮未修改 eTax/ePay 业务代码,没有使用业务服务器。
历史发布及验收记录保留在 [CHANGELOG](CHANGELOG.md)、[v0.9.0 接入验收](performance/compatibilityAcceptance.md)。
全量 race(40 个测试包)、静态检查、构建及本地隔离验收已通过,详见 [验收记录](performance/connectorAcceptance.md)。

View File

@@ -138,3 +138,12 @@ go run ./cmd/go-omni api generate --api examples/contracts/responses.api \
``` ```
示例业务方法起初为 501,完成 Service 后就会套用声明的模板。参考 [完整 .api](examples/contracts/responses.api)。验收测试会在临时目录生成真实项目,写入业务方法,执行已生成路由,检查数字错误码、原始 JSON、鉴权错误、路由覆盖与重复生成保护。 示例业务方法起初为 501,完成 Service 后就会套用声明的模板。参考 [完整 .api](examples/contracts/responses.api)。验收测试会在临时目录生成真实项目,写入业务方法,执行已生成路由,检查数字错误码、原始 JSON、鉴权错误、路由覆盖与重复生成保护。
## 错误目录与 HTTP 200 失败(v0.10.0-preview)
错误含义由项目的 `errors.Catalog` 或 `.api errorCatalog` 集中定义,返回字段/结构仍由本页的 response 模板控制。目录提供独立的逻辑标识、对外字符串/整数码、消息、HTTP 状态,使用 `WithErrors` 或生成路由绑定。详见 [统一错误目录](errors/README.md)。
`response.Result.IsError()` 综合明确失败标记、Err 和 HTTP 状态判断;HTTP 200 的业务错误不会使用成功码/成功提示,仍可选择 `errorBody`。新目录的公开码与提示优先于通用兜底;与同标识 Codes/Messages 冲突会拒绝,不会静默覆盖。没有目录的旧映射保持原有行为。
自定义 Encoder 可读取 `Code`(逻辑标识)、`BusinessCode`(目录对外码)、Message 与 Err,并返回项目要求的最终状态和媒体类型。不要仅通过 Status 判断成功;内部原因默认只供诊断,不应由 Encoder 直接序列化 Err。RawJSON 的成功输出保持原样,失败继续交给所选错误编码器。

View File

@@ -67,6 +67,7 @@ func configure(options ...config.Option) config.Config {
func newApp(router httpx.Router, settings config.Config) *App { func newApp(router httpx.Router, settings config.Config) *App {
settings.Response = settings.Response.Clone() settings.Response = settings.Response.Clone()
settings.Errors = settings.Errors.Clone()
encoder, responseErr := response.New(settings.Response) encoder, responseErr := response.New(settings.Response)
if responseErr != nil { if responseErr != nil {
panic(responseErr) panic(responseErr)
@@ -77,6 +78,10 @@ func newApp(router httpx.Router, settings config.Config) *App {
} }
settings.TrustedProxies = append([]string(nil), settings.TrustedProxies...) settings.TrustedProxies = append([]string(nil), settings.TrustedProxies...)
settings.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...) settings.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...)
responseEncoder, responseErr = response.WithErrors(settings.Errors, responseEncoder)
if responseErr != nil {
panic(responseErr)
}
resolver, err := httpx.NewClientIPResolver(settings.TrustedProxies...) resolver, err := httpx.NewClientIPResolver(settings.TrustedProxies...)
if err != nil { if err != nil {
panic("go-omni: " + err.Error()) panic("go-omni: " + err.Error())
@@ -114,6 +119,7 @@ func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
func (a *App) Config() config.Config { func (a *App) Config() config.Config {
settings := a.config settings := a.config
settings.Response = settings.Response.Clone() settings.Response = settings.Response.Clone()
settings.Errors = settings.Errors.Clone()
settings.TrustedProxies = append([]string(nil), settings.TrustedProxies...) settings.TrustedProxies = append([]string(nil), settings.TrustedProxies...)
settings.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...) settings.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...)
return settings return settings

178
app/errorCatalog_test.go Normal file
View File

@@ -0,0 +1,178 @@
package app_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
omni "git.yuntuo.hk/k2safe/go-omni"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx"
"git.yuntuo.hk/k2safe/go-omni/metrics"
"git.yuntuo.hk/k2safe/go-omni/middleware"
"git.yuntuo.hk/k2safe/go-omni/response"
)
func TestAppErrorCatalogHTTP200LogsAndReplay(t *testing.T) {
var logs bytes.Buffer
logCfg := logx.Default()
logCfg.Writer = &logs
l, err := logx.New(logCfg)
if err != nil {
t.Fatal(err)
}
logx.SetDefault(l)
defer logx.SetDefault(nil)
defer l.Close()
cat := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"INVALID": {Code: 400002, Message: "项目提示"}, "DENIED": {Status: 403, Code: 9, Message: "no"}}}
policy := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}
app := omni.New(omni.WithErrors(cat), omni.WithResponse(policy), omni.WithLogger(true))
defer app.Close()
meter := metrics.New()
app.Use(meter.Middleware())
cat.Definitions["INVALID"] = apierrors.Definition{Status: 500, Code: 1, Message: "mutated"}
app.Config().Errors.Definitions["INVALID"] = apierrors.Definition{Status: 500, Code: 2, Message: "mutated"}
app.GET("/error", func(c *omni.Context) error {
return fmt.Errorf("wrapped: %w", apierrors.ByCode("INVALID").WithCause(errors.New("private")))
})
app.GET("/direct", func(c *omni.Context) error { c.WriteError(apierrors.ByCode("INVALID")); return nil })
app.GET("/bridge", func(c *omni.Context) error { return apierrors.ByCode("INVALID") }, httpx.WrapMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) })
}))
app.GET("/missing", func(c *omni.Context) error { return apierrors.ByCode("MISSING") })
app.GET("/denied", func(c *omni.Context) error { return apierrors.ByCode("DENIED") })
app.GET("/committed", func(c *omni.Context) error {
if err := c.Text(201, "done"); err != nil {
return err
}
return apierrors.ByCode("INVALID")
})
app.HandleWithResponse("GET", "/callback", response.EncoderFunc(func(r response.Result) (response.Payload, error) {
if !r.IsError() || r.BusinessCode != int64(400002) {
t.Fatal(r)
}
return response.Text(202, "ACK"), nil
}), func(c *omni.Context) error { return apierrors.ByCode("INVALID") })
idem, err := middleware.NewIdempotency(middleware.IdempotencyConfig{Scope: func(*httpx.Context) (string, error) { return "fixture", nil }})
if err != nil {
t.Fatal(err)
}
calls := 0
app.POST("/idempotent", func(c *omni.Context) error { calls++; return apierrors.ByCode("INVALID") }, idem.Middleware())
shared, err := middleware.NewIdempotency(middleware.IdempotencyConfig{Store: &catalogStore{records: map[string]middleware.IdempotencyRecord{}}, Scope: func(*httpx.Context) (string, error) { return "fixture", nil }})
if err != nil {
t.Fatal(err)
}
app.POST("/shared", func(c *omni.Context) error { calls++; return apierrors.ByCode("INVALID") }, shared.Middleware())
for _, path := range []string{"/error", "/direct", "/bridge", "/idempotent", "/idempotent", "/shared", "/shared"} {
method := "GET"
if path == "/idempotent" || path == "/shared" {
method = "POST"
}
w := httptest.NewRecorder()
r := httptest.NewRequest(method, path, nil)
r.Header.Set("Idempotency-Key", "same")
app.ServeHTTP(w, r)
if w.Code != 200 || !strings.Contains(w.Body.String(), `"code":400002`) || !strings.Contains(w.Body.String(), "项目提示") || strings.Contains(w.Body.String(), "private") {
t.Fatal(w.Code, w.Body.String())
}
}
if stats := meter.Snapshot(); stats.Errors != 7 || stats.StatusCounts["200"] != 7 {
t.Fatal("lost business failure metrics", stats)
}
if calls != 2 {
t.Fatal("replay executed business twice")
}
for _, test := range []struct {
path string
status int
body string
}{{"/denied", 403, `"code":9`}, {"/missing", 500, `"code":9999`}, {"/committed", 201, "done"}, {"/callback", 202, "ACK"}} {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", test.path, nil))
if w.Code != test.status || !strings.Contains(w.Body.String(), test.body) {
t.Fatal(test.path, w.Code, w.Body.String())
}
}
for _, line := range strings.Split(strings.TrimSpace(logs.String()), "\n") {
var entry map[string]any
if err := json.Unmarshal([]byte(line), &entry); err != nil {
t.Fatal(err)
}
if entry["msg"] != "http.request" {
continue
}
if entry["failed"] != true {
t.Fatal("lost failure:", entry)
}
if entry["path"] == "/callback" {
if entry["status"] != float64(202) {
t.Fatal(entry)
}
} else if entry["path"] == "/error" || entry["path"] == "/direct" || entry["path"] == "/bridge" || entry["path"] == "/idempotent" || entry["path"] == "/shared" {
if entry["status"] != float64(200) || entry["businessCode"] != float64(400002) || entry["errorKey"] != "INVALID" {
t.Fatal(entry)
}
}
}
// A second application may interpret the same logical identifier differently.
app2 := omni.New(omni.WithErrors(apierrors.Catalog{Definitions: map[string]apierrors.Definition{"INVALID": {Status: 409, Code: "conflict", Message: "other"}}}))
defer app2.Close()
app2.GET("/", func(*omni.Context) error { return apierrors.ByCode("INVALID") })
w := httptest.NewRecorder()
app2.ServeHTTP(w, httptest.NewRequest("GET", "/", nil))
if w.Code != 409 || !strings.Contains(w.Body.String(), "other") {
t.Fatal(w.Code, w.Body.String())
}
}
// A serialized store exercises the shared middleware contract, including replay
// metadata. No external service or project database is used by this fixture.
type catalogStore struct {
mu sync.Mutex
records map[string]middleware.IdempotencyRecord
}
func (s *catalogStore) Claim(_ context.Context, key, fp, owner string) (middleware.IdempotencyRecord, bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
if r, ok := s.records[key]; ok {
return r, false, nil
}
r := middleware.IdempotencyRecord{Fingerprint: fp, Owner: owner, State: "running"}
s.records[key] = r
return r, true, nil
}
func (s *catalogStore) Complete(_ context.Context, key, owner string, response *middleware.StoredResponse, _ time.Duration) error {
s.mu.Lock()
defer s.mu.Unlock()
r := s.records[key]
if r.Owner != owner {
return errors.New("wrong owner")
}
r.State = "failed"
if response != nil {
r.State = "completed"
encoded, err := json.Marshal(response)
if err != nil {
return err
}
var copy middleware.StoredResponse
if err := json.Unmarshal(encoded, &copy); err != nil {
return err
}
r.Response = &copy
}
s.records[key] = r
return nil
}

View File

@@ -0,0 +1,126 @@
package app_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
omni "git.yuntuo.hk/k2safe/go-omni"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/response"
)
func TestRouteInheritsAppErrorCatalog(t *testing.T) {
catalog := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{
"USERNAME_INVALID": {Code: 400002, Message: "用户名格式不正确"},
"TEXT_TOO_LONG": {Status: 200, Code: 400003, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}}},
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "系统错误"},
}}
format := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}
app := omni.New(omni.WithErrors(catalog), omni.WithResponse(format))
defer app.Close()
getError := func(c *httpx.Context) error {
key := c.Header("X-Error")
if key == "TEXT_TOO_LONG" {
return apierrors.ByCode(key).WithParams(map[string]any{"label": "企业名称", "max": 100})
}
return apierrors.ByCode(key)
}
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error {
if c.Header("X-Early") == "true" {
return getError(c)
}
return next(c)
}
})
bridge := httpx.WrapMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) })
})
app.GET("/plain", getError)
// This is exactly the wrapper emitted by v0.11.0, not just the new generator.
legacy := response.MustWithErrors(apierrors.Catalog{}, response.MustNew(format))
app.HandleWithResponse("GET", "/generated", legacy, getError, bridge)
app.HandleWithResponse("GET", "/raw", response.RawJSON(legacy), getError, bridge)
for _, path := range []string{"/plain", "/generated", "/raw"} {
for _, early := range []string{"false", "true"} {
for _, test := range []struct {
key string
status, code int
message string
}{
{"USERNAME_INVALID", 400, 400002, "用户名格式不正确"},
{"TEXT_TOO_LONG", 200, 400003, "企业名称不能超过100个字符"},
{"MISSING", 500, 9999, "系统错误"},
} {
t.Run(path+"/"+early+"/"+test.key, func(t *testing.T) {
r := httptest.NewRequest("GET", path, nil)
r.Header.Set("X-Early", early)
r.Header.Set("X-Error", test.key)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
var value map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &value); err != nil {
t.Fatal(err, w.Body.String())
}
if w.Code != test.status || value["code"] != float64(test.code) || value["msg"] != test.message {
t.Fatal(w.Code, value)
}
if data, ok := value["data"]; !ok || data != nil {
t.Fatal("data:null lost", value)
}
})
}
}
}
}
func TestExplicitRouteErrorCatalogRemainsIndependent(t *testing.T) {
format := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}
global := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{
"INVALID": {Code: 400002, Message: "全局提示"}, "APP_ONLY": {Code: 400003, Message: "全局独有"},
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "全局系统错误"},
}}
local := apierrors.Catalog{DefaultStatus: 409, Definitions: map[string]apierrors.Definition{
"INVALID": {Code: 409001, Message: "路由提示"}, "INTERNAL_ERROR": {Status: 500, Code: 9998, Message: "路由系统错误"},
}}
app := omni.New(omni.WithErrors(global), omni.WithResponse(format))
defer app.Close()
getError := func(c *httpx.Context) error { return apierrors.ByCode(c.Header("X-Error")) }
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error {
if c.Header("X-Early") == "true" {
return getError(c)
}
return next(c)
}
})
routeFormat := format
routeFormat.MessageField = "detail"
localEncoder := response.MustWithErrors(local, response.MustNew(routeFormat))
app.HandleWithResponse("GET", "/", response.RawJSON(localEncoder), getError)
for _, early := range []string{"false", "true"} {
for _, test := range []struct {
key string
status, code int
message string
}{
{"INVALID", 409, 409001, "路由提示"}, {"APP_ONLY", 500, 9998, "路由系统错误"},
} {
r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("X-Early", early)
r.Header.Set("X-Error", test.key)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
var value map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &value); err != nil {
t.Fatal(err)
}
if w.Code != test.status || value["code"] != float64(test.code) || value["detail"] != test.message || value["msg"] != nil {
t.Fatal(w.Code, value)
}
}
}
}

186
app/errorParams_test.go Normal file
View File

@@ -0,0 +1,186 @@
package app_test
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
omni "git.yuntuo.hk/k2safe/go-omni"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx"
"git.yuntuo.hk/k2safe/go-omni/metrics"
"git.yuntuo.hk/k2safe/go-omni/middleware"
"git.yuntuo.hk/k2safe/go-omni/response"
)
func TestTemplateErrorsAcrossPoliciesAndReplay(t *testing.T) {
var logs bytes.Buffer
lc := logx.Default()
lc.Writer = &logs
logger, err := logx.New(lc)
if err != nil {
t.Fatal(err)
}
logx.SetDefault(logger)
defer logx.SetDefault(nil)
defer logger.Close()
cat := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"TOO_LONG": {
Code: 400002, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}},
}}}
registry := apierrors.MustRegistry(cat)
encoder := response.MustWithErrors(cat, response.MustNew(response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}))
app := omni.New(omni.WithLogger(true))
defer app.Close()
meter := metrics.New()
app.Use(meter.Middleware())
// The clone uses the route policy even though the App has a different default.
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error {
err := next(c)
if f := c.Failure(); f != nil && strings.Contains(f.Message, "{label}") {
t.Error("unrendered failure", f)
}
return err
}
})
bridge := httpx.WrapMiddleware(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) })
})
calls := 0
handler := func(c *httpx.Context) error {
calls++
values := map[string]any{"label": "企业名称", "max": 100}
if c.Header("X-Label") == "remark" {
values = map[string]any{"label": "备注", "max": 500}
}
switch c.Header("X-Failure") {
case "missing":
delete(values, "max")
case "type":
values["max"] = "private-value"
}
e := apierrors.ByCode("TOO_LONG").WithParams(values).WithCause(errors.New("private cause"))
if c.Header("X-Resolved") == "true" {
return registry.Resolve(e)
}
return e
}
app.HandleWithResponse("POST", "/direct", encoder, handler, bridge)
for _, shared := range []bool{false, true} {
cfg := middleware.IdempotencyConfig{Scope: func(*httpx.Context) (string, error) { return "fixture", nil }}
path := "/local"
if shared {
path = "/shared"
cfg.Store = &catalogStore{records: map[string]middleware.IdempotencyRecord{}}
}
idem, err := middleware.NewIdempotency(cfg)
if err != nil {
t.Fatal(err)
}
app.HandleWithResponse("POST", path, encoder, handler, idem.Middleware(), bridge)
}
check := func(path, label, key, kind string, status int, want string) *httptest.ResponseRecorder {
t.Helper()
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", path, nil)
r.Header.Set("Idempotency-Key", key)
r.Header.Set("X-Label", label)
r.Header.Set("X-Failure", kind)
r.Header.Set("X-Resolved", "true")
app.ServeHTTP(w, r)
if w.Code != status || !strings.Contains(w.Body.String(), want) || strings.Contains(w.Body.String(), "private") || strings.Contains(w.Body.String(), "{label}") {
t.Fatal(path, w.Code, w.Body.String())
}
if status == 200 && (!strings.Contains(w.Body.String(), `"data":null`) || !strings.Contains(w.Body.String(), `"code":400002`)) {
t.Fatal(w.Body.String())
}
return w
}
for _, path := range []string{"/direct", "/local", "/shared"} {
for _, label := range []string{"company", "remark"} {
want := "企业名称不能超过100个字符"
if label == "remark" {
want = "备注不能超过500个字符"
}
first := check(path, label, label, "", 200, want)
if path != "/direct" {
before := calls
second := check(path, label, label, "", 200, want)
if calls != before || first.Body.String() != second.Body.String() || second.Header().Get("Idempotency-Replayed") != "true" {
t.Fatal("bad replay")
}
}
}
}
check("/direct", "company", "", "missing", 500, "internal server error")
check("/direct", "company", "", "type", 500, "internal server error")
stats := meter.Snapshot()
if stats.Errors != 12 || stats.StatusCounts["200"] != 10 || stats.StatusCounts["500"] != 2 {
t.Fatal(stats)
}
seen, diagnostics := 0, 0
for _, line := range strings.Split(strings.TrimSpace(logs.String()), "\n") {
var entry map[string]any
if err := json.Unmarshal([]byte(line), &entry); err != nil {
t.Fatal(err)
}
if entry["msg"] != "http.request" {
continue
}
seen++
if entry["failed"] != true {
t.Fatal(entry)
}
if entry["status"] == float64(200) && (entry["errorKey"] != "TOO_LONG" || entry["businessCode"] != float64(400002)) {
t.Fatal(entry)
}
if entry["errorParameter"] != nil {
diagnostics++
if strings.Contains(entry["errorParameter"].(string), "private-value") {
t.Fatal(entry)
}
}
}
if seen != 12 || diagnostics != 2 {
t.Fatal(seen, diagnostics)
}
}
func TestResolvedFailureReplaySurvivesCatalogChanges(t *testing.T) {
store := &catalogStore{records: map[string]middleware.IdempotencyRecord{}}
var observed []*apierrors.APIError
makeApp := func(message string) *omni.App {
cat := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"T": {Code: "code", Message: message, Params: []apierrors.Parameter{{Name: "label", Type: "string"}}}}}
app := omni.New(omni.WithErrors(cat))
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error { err := next(c); observed = append(observed, c.Failure()); return err }
})
idem, err := middleware.NewIdempotency(middleware.IdempotencyConfig{Store: store, Scope: func(*httpx.Context) (string, error) { return "fixture", nil }})
if err != nil {
t.Fatal(err)
}
app.POST("/", func(c *httpx.Context) error {
return apierrors.ByCode("T").WithParams(map[string]any{"label": "企业"})
}, idem.Middleware())
return app
}
for _, message := range []string{"{label}原文案", "{label}新文案"} {
app := makeApp(message)
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", nil)
r.Header.Set("Idempotency-Key", "same")
app.ServeHTTP(w, r)
app.Close()
if !strings.Contains(w.Body.String(), "企业原文案") {
t.Fatal(w.Body.String())
}
}
if len(observed) != 2 || observed[0].Message != "企业原文案" || observed[1].Message != "企业原文案" || observed[1].Code != "T" {
t.Fatal(observed)
}
}

View File

@@ -30,6 +30,9 @@ func (a *App) adaptResponse(encoder response.Encoder, handler httpx.Handler, mid
request = request.WithContext(httpx.WithClientIPResolver(request.Context(), a.clientIPResolver)) request = request.WithContext(httpx.WithClientIPResolver(request.Context(), a.clientIPResolver))
} }
requestContext := httpx.NewContext(request, writer, httpx.RouteParams(request.Context())) requestContext := httpx.NewContext(request, writer, httpx.RouteParams(request.Context()))
if provider, ok := a.responseEncoder.(response.ErrorRegistryProvider); ok {
requestContext.SetErrorRegistry(provider.ErrorRegistry())
}
requestContext.SetResponseEncoder(encoder) requestContext.SetResponseEncoder(encoder)
defer func() { httpx.CleanupMultipart(requestContext.Request()) }() defer func() { httpx.CleanupMultipart(requestContext.Request()) }()
if err := wrapped(requestContext); err != nil { if err := wrapped(requestContext); err != nil {

View File

@@ -21,6 +21,12 @@ go run ./cmd/go-omni api validate --api examples/contracts/user.api
后续在生成项目内执行 `go-omni api generate --api api/user.api --dir .`。 后续在生成项目内执行 `go-omni api generate --api api/user.api --dir .`。
项目内可省略 `--api` / `--dir`。`api inspect` 展示路由和鉴权/业务错误,`api export` 向 stdout 导出 OpenAPI,`api generate --dry-run` 预览文件变化。`--check` 只读检查生成产物是否过期。生成器记录所有权,保留 Service / Repository / 配置 / main 的手写内容,遇到生成文件被手动修改则报冲突。新 Service 与中间件占位实现返回 501。支持 `error (...)`、具名 `auth`、路由 `@errors` / `@auth` 和多行 `@doc`;错误目录同步生成 `internal/apierrors` 调用函数。 项目内可省略 `--api` / `--dir`。`api inspect` 展示路由和鉴权/业务错误,`api export` 向 stdout 导出 OpenAPI,`api generate --dry-run` 预览文件变化。`--check` 只读检查生成产物是否过期。生成器记录所有权,保留 Service / Repository / 配置 / main 的手写内容,遇到生成文件被手动修改则报冲突。新 Service 与中间件占位实现返回 501。支持 `error (...)`、具名 `auth`、路由 `@errors` / `@auth` 和多行 `@doc`;错误目录同步生成 `internal/apierrors` 调用函数。
`v0.10.0-preview` 新增 `errorCatalog`:集中配置业务码、提示和 HTTP 状态,支持默认 HTTP 200 与单条覆盖,生成函数、路由策略和 OpenAPI 共用一份定义。语法及配置方式见 [统一错误目录](../../errors/README.md)。
`v0.11.0-preview` 增加有序模板参数,生成带类型的错误函数和具体文档示例;示例见 [errorParams.api](../../examples/contracts/errorParams.api)。新生成代码需要同版或更新框架。
`v0.11.1-preview` 修复未声明错误的生成路由遮蔽 App 错误目录:错误可只在 Go 中注册一次,`.api` 不必重复维护;返回格式和路由参数继续按契约生成。
支持范围、参数语义、目录及安全更新策略见 [API 开发指南](../../API.md)。本版借鉴 go-zero 的工作流,不宣称兼容全部 goctl 语法;仍通过 Omni/httpx 保留网络层替换能力。 支持范围、参数语义、目录及安全更新策略见 [API 开发指南](../../API.md)。本版借鉴 go-zero 的工作流,不宣称兼容全部 goctl 语法;仍通过 Omni/httpx 保留网络层替换能力。
## 日志与对象存储按需装配 ## 日志与对象存储按需装配
@@ -75,8 +81,8 @@ Handler / Service / Repository / Model 与模块注册入口。模块名为 lowe
从远程安装,生成一个带 Handler / Service / Repository / Model 分层的服务骨架: 从远程安装,生成一个带 Handler / Service / Repository / Model 分层的服务骨架:
```sh ```sh
GOPRIVATE=git.yuntuo.hk go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.9.1-preview GOPRIVATE=git.yuntuo.hk go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.11.1-preview
go-omni new --framework-version v0.9.1-preview --module example.com/billing ./billing go-omni new --framework-version v0.11.1-preview --module example.com/billing ./billing
cd billing cd billing
go mod tidy go mod tidy
go run ./cmd/server go run ./cmd/server

View File

@@ -8,6 +8,7 @@ import (
"sort" "sort"
"strings" "strings"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/openapi" "git.yuntuo.hk/k2safe/go-omni/openapi"
"git.yuntuo.hk/k2safe/go-omni/response" "git.yuntuo.hk/k2safe/go-omni/response"
) )
@@ -20,14 +21,18 @@ var credentialName = regexp.MustCompile("^[!#$%&'*+.^_`|~0-9A-Za-z-]+$")
func prepareMetadata(original *Spec) (*Spec, error) { func prepareMetadata(original *Spec) (*Spec, error) {
spec := *original spec := *original
spec.Routes = append([]Route(nil), original.Routes...) spec.Routes = append([]Route(nil), original.Routes...)
spec.Errors = append([]ErrorDefinition(nil), original.Errors...)
if err := validateResponses(&spec); err != nil { if err := validateResponses(&spec); err != nil {
return nil, err return nil, err
} }
definitions := map[string]ErrorDefinition{} definitions := map[string]ErrorDefinition{}
codes, symbols := map[string]bool{}, map[string]bool{} codes, symbols := map[string]bool{}, map[string]bool{"Catalog": true}
for _, e := range spec.Errors { for _, e := range spec.Errors {
if !exportedName.MatchString(e.Name) || e.Status < 400 || e.Status > 599 || !errorCode.MatchString(e.Code) || strings.TrimSpace(e.Message) == "" { if !exportedName.MatchString(e.Name) || !apierrors.ValidErrorStatus(e.Status) || strings.TrimSpace(e.Message) == "" {
return nil, fmt.Errorf("%s: error requires UpperCamelCase name, status 400..599, UPPER_SNAKE_CASE code and a nonempty public message", e.Position) return nil, fmt.Errorf("%s: error requires UpperCamelCase name, a final body-bearing status (200..599 except 204/205/304) and a nonempty public message", e.Position)
}
if e.Catalog == "" && !errorCode.MatchString(e.Code) {
return nil, fmt.Errorf("%s: legacy error declaration requires UPPER_SNAKE_CASE code", e.Position)
} }
if codes[e.Code] { if codes[e.Code] {
return nil, fmt.Errorf("%s: duplicate error code %s", e.Position, e.Code) return nil, fmt.Errorf("%s: duplicate error code %s", e.Position, e.Code)
@@ -41,6 +46,18 @@ func prepareMetadata(original *Spec) (*Spec, error) {
codes[e.Code] = true codes[e.Code] = true
definitions[e.Name] = e definitions[e.Name] = e
} }
registry, err := apierrors.NewRegistry(errorCatalog(&spec, nil))
if err != nil {
return nil, err
}
normalized := registry.Catalog()
for i := range spec.Errors {
spec.Errors[i].BusinessCode = normalized.Definitions[spec.Errors[i].Code].Code
spec.Errors[i].Params = normalized.Definitions[spec.Errors[i].Code].Params
}
if err := validateErrorPolicies(&spec); err != nil {
return nil, err
}
auth := map[string]AuthDefinition{} auth := map[string]AuthDefinition{}
for _, a := range spec.Auth { for _, a := range spec.Auth {
if !exportedName.MatchString(a.Name) || !goName.MatchString(a.Middleware) || strings.EqualFold(a.Middleware, "defaults") || strings.EqualFold(a.Middleware, "registryGen") { if !exportedName.MatchString(a.Name) || !goName.MatchString(a.Middleware) || strings.EqualFold(a.Middleware, "defaults") || strings.EqualFold(a.Middleware, "registryGen") {
@@ -84,7 +101,7 @@ func prepareMetadata(original *Spec) (*Spec, error) {
seen := map[string]bool{} seen := map[string]bool{}
for _, name := range r.Errors { for _, name := range r.Errors {
if _, ok := definitions[name]; !ok { if _, ok := definitions[name]; !ok {
return nil, fmt.Errorf("%s: unknown error %s; declare it in error (...)", r.Position, name) return nil, fmt.Errorf("%s: unknown error %s; declare it in errorCatalog or error (...)", r.Position, name)
} }
if seen[name] { if seen[name] {
return nil, fmt.Errorf("%s: duplicate error %s", r.Position, name) return nil, fmt.Errorf("%s: duplicate error %s", r.Position, name)
@@ -124,6 +141,7 @@ func decorateDocument(doc map[string]any, spec *Spec) {
} }
doc["components"].(map[string]any)["securitySchemes"] = schemes doc["components"].(map[string]any)["securitySchemes"] = schemes
definitions := map[string]ErrorDefinition{} definitions := map[string]ErrorDefinition{}
registry := apierrors.MustRegistry(errorCatalog(spec, nil))
for _, e := range spec.Errors { for _, e := range spec.Errors {
definitions[e.Name] = e definitions[e.Name] = e
} }
@@ -182,15 +200,37 @@ func decorateDocument(doc map[string]any, spec *Spec) {
response = genericErrorResponse(http.StatusText(status), cfg) response = genericErrorResponse(http.StatusText(status), cfg)
responses[key] = response responses[key] = response
} }
media := response["content"].(map[string]any)["application/json"].(map[string]any) content, ok := response["content"].(map[string]any)
if !ok {
content = map[string]any{}
response["content"] = content
}
media, hasJSON := content["application/json"].(map[string]any)
if !hasJSON {
media = map[string]any{}
content["application/json"] = media
}
errorMedia := genericErrorResponse("", cfg)["content"].(map[string]any)["application/json"].(map[string]any)
if exists && status < 400 && media["schema"] != nil {
media["schema"] = map[string]any{"anyOf": []any{media["schema"], errorMedia["schema"]}}
} else if media["schema"] == nil {
media["schema"] = errorMedia["schema"]
}
examples := map[string]any{} examples := map[string]any{}
codes := []any{} codes := []any{}
seenCodes := map[string]bool{} seenCodes := map[string]bool{}
description := []string{} description := []string{}
if exists {
if text, ok := response["description"].(string); ok && text != "" {
description = append(description, text)
}
}
sort.Slice(errs, func(i, j int) bool { return errs[i].Name < errs[j].Name }) sort.Slice(errs, func(i, j int) bool { return errs[i].Name < errs[j].Name })
for _, e := range errs { for _, e := range errs {
value := policy.Value(responseResult(e)) result := responseResult(e)
examples[e.Name] = map[string]any{"summary": e.Message, "value": value} result.Message = registry.Example(e.Code).Message
value := policy.Value(result)
examples[e.Name] = map[string]any{"summary": result.Message, "value": value}
codeKey := fmt.Sprint(value[cfg.CodeField]) codeKey := fmt.Sprint(value[cfg.CodeField])
if !seenCodes[codeKey] { if !seenCodes[codeKey] {
codes = append(codes, value[cfg.CodeField]) codes = append(codes, value[cfg.CodeField])
@@ -207,7 +247,7 @@ func decorateDocument(doc map[string]any, spec *Spec) {
} }
} }
func responseResult(e ErrorDefinition) response.Result { func responseResult(e ErrorDefinition) response.Result {
return response.Result{Status: e.Status, Code: e.Code, Message: e.Message, RequestID: "exampleRequest"} return response.Result{Status: e.Status, Code: e.Code, Message: e.Message, BusinessCode: e.BusinessCode, Failed: true, RequestID: "exampleRequest"}
} }
func genericErrorResponse(description string, cfg response.Config) map[string]any { func genericErrorResponse(description string, cfg response.Config) map[string]any {
if description == "" { if description == "" {

View File

@@ -4,16 +4,21 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
) )
// ErrorDefinition declares a public, stable error. Generated constructors return // ErrorDefinition declares a public, stable error. Generated constructors return
// a fresh error each time so WithCause cannot mutate a shared singleton. // a fresh error each time so WithCause cannot mutate a shared singleton.
type ErrorDefinition struct { type ErrorDefinition struct {
Name string `json:"name"` Name string `json:"name"`
Status int `json:"status"` BusinessCode any `json:"businessCode,omitempty"`
Code string `json:"code"` Params []apierrors.Parameter `json:"params,omitempty"`
Message string `json:"message"` Catalog string `json:"catalog,omitempty"`
Position string `json:"-"` Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
Position string `json:"-"`
} }
type AuthDefinition struct { type AuthDefinition struct {
Name, Type, In, KeyName, Middleware, Description, BearerFormat, Position string Name, Type, In, KeyName, Middleware, Description, BearerFormat, Position string

View File

@@ -0,0 +1,119 @@
package apigen
import (
"encoding/json"
"fmt"
"sort"
"strconv"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/response"
)
// A named catalog groups project definitions under one default HTTP status.
// Each JSON object key supplies the generated Go function's name, while key is
// the independent logical identifier and code is the public string/integer.
func (p *parser) parseErrorCatalog() error {
pos := p.next().position
name, err := p.ident()
if err != nil {
return err
}
if !exportedName.MatchString(name) {
return p.err("errorCatalog requires an UpperCamelCase name")
}
for _, prior := range p.spec.ErrorCatalogs {
if prior == name {
return p.err("duplicate errorCatalog " + name)
}
}
p.spec.ErrorCatalogs = append(p.spec.ErrorCatalogs, name)
p.skip()
props, err := p.metadata()
if err != nil {
return err
}
defaultStatus := 400
if value, ok := props["defaultStatus"]; ok {
defaultStatus, err = strconv.Atoi(value)
if err != nil {
return p.err("defaultStatus must be an integer")
}
}
if !apierrors.ValidErrorStatus(defaultStatus) {
return p.err("invalid errorCatalog defaultStatus")
}
for key := range props {
if key != "defaultStatus" && key != "errors" {
return p.err("unsupported errorCatalog property " + key)
}
}
type entry struct {
Key string `json:"key"`
Code any `json:"code"`
Message string `json:"message"`
Status int `json:"status"`
Params []apierrors.Parameter `json:"params"`
}
var entries map[string]entry
if err := apierrors.DecodeCatalogJSON([]byte(props["errors"]), &entries); err != nil {
return fmt.Errorf("%s: catalog %s: %w", pos, name, err)
}
if len(entries) == 0 {
return p.err("errorCatalog requires errors")
}
names := make([]string, 0, len(entries))
for key := range entries {
names = append(names, key)
}
sort.Strings(names)
for _, fn := range names {
e := entries[fn]
if e.Code == nil {
return p.err("errorCatalog error requires code")
}
if e.Status == 0 {
e.Status = defaultStatus
}
p.spec.Errors = append(p.spec.Errors, ErrorDefinition{Name: fn, Status: e.Status, Code: e.Key, Message: e.Message, BusinessCode: e.Code, Params: e.Params, Catalog: name, Position: pos})
}
return nil
}
func errorCatalog(spec *Spec, names []string) apierrors.Catalog {
c := apierrors.Catalog{Definitions: map[string]apierrors.Definition{}}
selected := map[string]bool{}
for _, n := range names {
selected[n] = true
}
for _, e := range spec.Errors {
if names != nil && !selected[e.Name] {
continue
}
c.Definitions[e.Code] = apierrors.Definition{Status: e.Status, Code: e.BusinessCode, Message: e.Message, Params: e.Params}
}
return c
}
// Validate the exact catalog/template pair used by each route. Different routes
// can use different code types without forcing a global response representation.
func validateErrorPolicies(spec *Spec) error {
for _, r := range spec.Routes {
f, err := response.New(responseConfig(spec, r))
if err != nil {
return err
}
if _, err = response.WithErrors(errorCatalog(spec, append([]string{}, r.Errors...)), f); err != nil {
return fmt.Errorf("%s: %w", r.Position, err)
}
}
return nil
}
func errorCatalogJSON(spec *Spec, names []string) string {
data, err := json.Marshal(errorCatalog(spec, names))
if err != nil {
panic(err)
}
return string(data)
}

View File

@@ -0,0 +1,222 @@
package apigen
import (
"context"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func TestErrorCatalogDocumentAndValidation(t *testing.T) {
path := filepath.Join(frameworkRoot(t), "examples/contracts/errors.api")
source := readFile(t, path)
spec, err := Parse(path)
if err != nil {
t.Fatal(err)
}
compiled, err := Compile(spec)
if err != nil {
t.Fatal(err)
}
for _, path := range []string{"/token", "/raw"} {
method := "post"
if path == "/raw" {
method = "get"
}
op := operation(t, document(t, compiled), path, method)
response := op["responses"].(map[string]any)["200"].(map[string]any)
media := response["content"].(map[string]any)["application/json"].(map[string]any)
alternatives := media["schema"].(map[string]any)["anyOf"].([]any)
if len(alternatives) != 2 {
t.Fatal("lost success or failure shape")
}
if path == "/raw" {
if alternatives[0].(map[string]any)["properties"].(map[string]any)["token"] == nil {
t.Fatal("raw success wrapped")
}
}
examples := media["examples"].(map[string]any)
if len(examples) != 2 {
t.Fatal(examples)
}
for _, name := range []string{"UsernameInvalid", "PasswordInvalid"} {
value := examples[name].(map[string]any)["value"].(map[string]any)
if value["ok"] != false || value["code"] != float64(400002) || value["msg"] == nil {
t.Fatal(value)
}
}
}
for _, change := range [][2]string{
{`defaultStatus: 200`, `defaultStatus: 204`},
{`"status": 403`, `"status": 304`},
{`"PASSWORD_INVALID"`, `"USERNAME_INVALID"`},
{`"code": 400002`, `"code": 2147483648`},
{`"code": 400002`, `"code": "400002"`},
{`"code": 400002`, `"code": 1.5`},
{`"code": 400002`, `"code": null`},
{`"code": 400002`, `"code": 400002, "code": 400003`},
{`"message": "用户名格式不正确"`, `"message": "用户名格式不正确", "typo": true`},
{`@handler token`, `@errors Unknown\n @handler token`},
{`successCode: 0`, `successCode: 0\n codes: {"USERNAME_INVALID": 999}`},
} {
text := strings.Replace(source, change[0], strings.ReplaceAll(change[1], `\n`, "\n"), 1)
s, err := parseText(t, text)
if err == nil {
_, err = Compile(s)
}
if err == nil {
t.Errorf("accepted %s", change[1])
}
}
// The legacy declaration is also allowed to carry a HTTP 200 failure.
old := strings.Replace(declaredAPI, "EmailTaken 409", "EmailTaken 200", 1)
s, err := parseText(t, old)
if err == nil {
_, err = Compile(s)
}
if err != nil {
t.Fatal(err)
}
}
func TestErrorCatalogConsumer(t *testing.T) {
testCatalogConsumer(t, "errors.api", "errorCatalog")
}
func testCatalogConsumer(t *testing.T, apiFile, fixtureName string, extraFiles ...map[string]string) {
t.Helper()
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") == "" {
t.Skip("consumer integration")
}
root := frameworkRoot(t)
dir := filepath.Join(t.TempDir(), "consumer")
opts := Options{API: filepath.Join(root, "examples/contracts", apiFile), Dir: dir, Module: "example.com/errorCatalog", FrameworkDir: root}
cli := os.Getenv("GO_OMNI_RELEASE_CLI")
generate := func(check bool) {
t.Helper()
if cli == "" {
o := opts
o.Check = check
if _, err := Generate(o); err != nil {
t.Fatal(err)
}
return
}
args := []string{"api", "generate", "--api", opts.API, "--dir", dir, "--module", opts.Module}
if check {
args = append(args, "--check")
}
cmd := exec.Command(cli, args...)
cmd.Dir = filepath.Dir(dir)
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("release generate %v\n%s", err, output)
}
}
generate(false)
files := map[string]string{"consumer_test.go": "consumer_test.go"}
for _, extra := range extraFiles {
for target, source := range extra {
files[target] = source
}
}
before := map[string]string{}
for target, source := range files {
fixture := readFile(t, filepath.Join("testdata", fixtureName, source))
writeFile(t, filepath.Join(dir, target), fixture)
before[target] = fixture
}
generate(false)
generate(true)
for target, fixture := range before {
if readFile(t, filepath.Join(dir, target)) != fixture {
t.Fatalf("regeneration overwrote %s", target)
}
}
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "go", "test", "-race", "-mod=mod", "-count=1", "./...")
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GOWORK=off")
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("consumer %v\n%s", err, output)
}
if cli != "" {
cmd = exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GOWORK=off")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err, string(output))
}
var module struct {
Version, Sum string
Replace any
}
if err = json.Unmarshal(output, &module); err != nil {
t.Fatal(err)
}
if module.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || module.Replace != nil || module.Sum == "" {
t.Fatal("not a fixed remote consumer")
}
t.Logf("remote framework %s checksum %s; no local replace", module.Version, module.Sum)
}
}
func TestErrorCatalogImportsAndRouteScopes(t *testing.T) {
dir := t.TempDir()
writeFile(t, filepath.Join(dir, "common.api"), `errorCatalog Numeric (
defaultStatus: 200
errors: {"NumberInvalid":{"key":"number.invalid","code":12,"message":"number"}}
)
errorCatalog Text (
defaultStatus: 409
errors: {"TextInvalid":{"key":"textInvalid","code":"000012","message":"text"}}
)
response NumericResponse (
codeType: int32
successCode: 0
errorCode: 9999
)
`)
writeFile(t, filepath.Join(dir, "service.api"), `import "common.api"
@server (
response: NumericResponse
errors: NumberInvalid
)
service scopes-api {
@handler numeric
get /number
}
@server (
errors: TextInvalid
)
service scopes-api {
@handler text
get /text
}
`)
s, err := Parse(filepath.Join(dir, "service.api"))
if err != nil {
t.Fatal(err)
}
compiled, err := Compile(s)
if err != nil {
t.Fatal(err)
}
doc := document(t, compiled)
for _, tc := range []struct {
path, status, name string
code any
}{{"/number", "200", "NumberInvalid", float64(12)}, {"/text", "409", "TextInvalid", "000012"}} {
response := operation(t, doc, tc.path, "get")["responses"].(map[string]any)[tc.status].(map[string]any)
value := response["content"].(map[string]any)["application/json"].(map[string]any)["examples"].(map[string]any)[tc.name].(map[string]any)["value"].(map[string]any)
if value["code"] != tc.code {
t.Fatal(value)
}
}
}

View File

@@ -0,0 +1,12 @@
package apigen
import "testing"
func TestErrorInheritanceConsumer(t *testing.T) {
testCatalogConsumer(t, "errorInheritance.api", "errorInheritance", map[string]string{
"internal/service/account/plain.go": "service.go",
"internal/service/account/raw.go": "emptyService.go",
"internal/service/account/cleared.go": "emptyService.go",
"internal/handler/account/boundedManual.go": "bounded.go",
})
}

View File

@@ -0,0 +1,86 @@
package apigen
import (
"path/filepath"
"strings"
"testing"
)
func TestErrorParamsConsumer(t *testing.T) { testCatalogConsumer(t, "errorParams.api", "errorParams") }
func TestErrorParamsDocumentAndValidation(t *testing.T) {
path := filepath.Join(frameworkRoot(t), "examples/contracts/errorParams.api")
source := readFile(t, path)
spec, err := Parse(path)
if err != nil {
t.Fatal(err)
}
compiled, err := Compile(spec)
if err != nil {
t.Fatal(err)
}
doc := document(t, compiled)
definitions := doc["x-omni-errors"].([]any)
found := false
for _, value := range definitions {
e := value.(map[string]any)
if e["name"] == "TextTooLong" {
found = true
params := e["params"].([]any)
if len(params) != 2 || params[0].(map[string]any)["name"] != "label" || params[1].(map[string]any)["type"] != "int" || e["message"] != "{label}不能超过{max}个字符" {
t.Fatal(e)
}
}
}
if !found {
t.Fatal("missing template definition")
}
for _, path := range []string{"/token", "/raw"} {
method := "post"
if path == "/raw" {
method = "get"
}
response := operation(t, doc, path, method)["responses"].(map[string]any)["200"].(map[string]any)
media := response["content"].(map[string]any)["application/json"].(map[string]any)
value := media["examples"].(map[string]any)["TextTooLong"].(map[string]any)["value"].(map[string]any)
if value["msg"] != "企业名称不能超过100个字符" || value["code"] != float64(400002) {
t.Fatal(value)
}
if _, ok := value["data"]; !ok {
t.Fatal("data null omitted", value)
}
if len(media["schema"].(map[string]any)["anyOf"].([]any)) != 2 {
t.Fatal("success schema lost")
}
}
for _, change := range [][2]string{
{`"{label}不能超过{max}个字符"`, `"{label}不能超过{missing}个字符"`},
{`"type": "int"`, `"type": "unknown"`},
{`"example": 100`, `"example": "100"`},
{`"name": "max"`, `"name": "label"`},
{`"name": "max"`, `"name": "type"`},
{`successCode: 0`, `successCode: 0\n messages: {"TEXT_TOO_LONG":"{label}不能超过{max}个字符"}`},
} {
changed := strings.Replace(source, change[0], strings.ReplaceAll(change[1], `\n`, "\n"), 1)
s, err := parseText(t, changed)
if err == nil {
_, err = Compile(s)
}
if err == nil {
t.Fatal("accepted", change)
}
}
// Removing examples still produces a concrete, deterministic example.
noExamples := strings.ReplaceAll(strings.ReplaceAll(source, `, "example": "企业名称"`, ""), `, "example": 100`, "")
s, err := parseText(t, noExamples)
if err != nil {
t.Fatal(err)
}
c, err := Compile(s)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(c.Document), "label不能超过1个字符") {
t.Fatal("missing generated example")
}
}

View File

@@ -19,7 +19,7 @@ import (
) )
const frameworkModule = "git.yuntuo.hk/k2safe/go-omni" const frameworkModule = "git.yuntuo.hk/k2safe/go-omni"
const minimumVersion = "v0.9.0-preview" const minimumVersion = "v0.11.0-preview"
const generatedHeader = "// Code generated by go-omni api. DO NOT EDIT.\n" const generatedHeader = "// Code generated by go-omni api. DO NOT EDIT.\n"
type Options struct { type Options struct {
@@ -140,13 +140,13 @@ func generate(o Options, c *Compiled, starter bool) (Output, error) {
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path = filepath.Join(directory, path) path = filepath.Join(directory, path)
} }
if _, err := os.Stat(filepath.Join(path, "contract", "scalar.go")); err == nil { if _, err := os.Stat(filepath.Join(path, "errors", "params.go")); err == nil {
localCurrent = true localCurrent = true
} }
} }
} }
if !localCurrent { if !localCurrent {
return Output{}, fmt.Errorf("existing framework dependency predates migration extensions; use %s or a current local replacement", minimumVersion) return Output{}, fmt.Errorf("existing framework dependency predates error template parameter support; use %s or a current local replacement", minimumVersion)
} }
} }
if o.FrameworkDir != "" { if o.FrameworkDir != "" {
@@ -312,12 +312,28 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
generated, seeds := map[string][]byte{}, map[string][]byte{} generated, seeds := map[string][]byte{}, map[string][]byte{}
put := func(files map[string][]byte, name, content string) { files[name] = []byte(content) } put := func(files map[string][]byte, name, content string) { files[name] = []byte(content) }
if len(c.Spec.Errors) > 0 { if len(c.Spec.Errors) > 0 {
content := "package apierrors\nimport omnierrors " + strconv.Quote(frameworkModule+"/errors") + "\n" content := "package apierrors\nimport(\"encoding/json\"; omnierrors " + strconv.Quote(frameworkModule+"/errors") + ")\n"
content += fmt.Sprintf("func Catalog()omnierrors.Catalog{var c omnierrors.Catalog;if err:=json.Unmarshal([]byte(%q),&c);err!=nil{panic(err)};return c}\nvar errorRegistry=omnierrors.MustRegistry(Catalog())\n", errorCatalogJSON(c.Spec, nil))
for _, e := range c.Spec.Errors { for _, e := range c.Spec.Errors {
content += fmt.Sprintf("const Code%s = %q\n// %s returns a fresh public error declared in the API contract.\nfunc %s()*omnierrors.APIError{return omnierrors.New(%d,Code%s,%q)}\n", e.Name, e.Code, e.Name, e.Name, e.Status, e.Name, e.Message) var args, values []string
for _, p := range e.Params {
name := p.Name
switch name {
case "string", "any", "omnierrors", "errorRegistry", "int", "int32", "int64", "float64", "bool":
name = "_" + name
}
args = append(args, name+" "+p.Type)
values = append(values, strconv.Quote(p.Name)+":"+name)
}
ref := fmt.Sprintf("omnierrors.ByCode(Code%s)", e.Name)
if len(args) > 0 {
ref += ".WithParams(map[string]any{" + strings.Join(values, ",") + "})"
}
content += fmt.Sprintf("const Code%s=%q\n// %s returns a fresh, resolved error declared in the API contract.\nfunc %s(%s)*omnierrors.APIError{return errorRegistry.Resolve(%s)}\n", e.Name, e.Code, e.Name, e.Name, strings.Join(args, ","), ref)
} }
put(generated, "internal/apierrors/errorsGen.go", generatedHeader+content) put(generated, "internal/apierrors/errorsGen.go", generatedHeader+content)
} }
types := "package types\n\n" types := "package types\n\n"
if len(c.Spec.Scalars) > 0 { if len(c.Spec.Scalars) > 0 {
types += "import(" types += "import("
@@ -343,7 +359,7 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
put(generated, "internal/types/typesGen.go", generatedHeader+types) put(generated, "internal/types/typesGen.go", generatedHeader+types)
put(generated, "docs/openapi.json", string(c.Document)+"\n") put(generated, "docs/openapi.json", string(c.Document)+"\n")
put(generated, "internal/handler/openapiGen.go", generatedHeader+fmt.Sprintf("package handler\nimport (\"net/http\"; %q)\nconst OpenAPIDocument = %q\nfunc OpenAPI(c *httpx.Context) error { return c.WriteResponse(200,http.Header{\"Content-Type\": {\"application/json; charset=utf-8\"}},[]byte(OpenAPIDocument)) }\n", frameworkModule+"/httpx", string(c.Document)+"\n")) put(generated, "internal/handler/openapiGen.go", generatedHeader+fmt.Sprintf("package handler\nimport (\"net/http\"; %q)\nconst OpenAPIDocument = %q\nfunc OpenAPI(c *httpx.Context) error { return c.WriteResponse(200,http.Header{\"Content-Type\": {\"application/json; charset=utf-8\"}},[]byte(OpenAPIDocument)) }\n", frameworkModule+"/httpx", string(c.Document)+"\n"))
policySource := fmt.Sprintf("package handler\nimport(\"encoding/json\";\"strings\";%q)\nvar responseTemplates=map[string]response.Encoder{\"\":response.Default(),\n", frameworkModule+"/response") policySource := fmt.Sprintf("package handler\nimport(\"encoding/json\";\"strings\";%q; omnierrors %q)\nvar responseTemplates=map[string]response.Encoder{\"\":response.Default(),\n", frameworkModule+"/response", frameworkModule+"/errors")
for _, definition := range c.Spec.Responses { for _, definition := range c.Spec.Responses {
data, err := json.Marshal(definition.Config) data, err := json.Marshal(definition.Config)
if err != nil { if err != nil {
@@ -351,7 +367,8 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
} }
policySource += fmt.Sprintf("%q: newResponseTemplate(%q),\n", definition.Name, string(data)) policySource += fmt.Sprintf("%q: newResponseTemplate(%q),\n", definition.Name, string(data))
} }
policySource += "}\nfunc newResponseTemplate(raw string)response.Encoder{var cfg response.Config;decoder:=json.NewDecoder(strings.NewReader(raw));decoder.UseNumber();if err:=decoder.Decode(&cfg);err!=nil{panic(err)};return response.MustNew(cfg)}\nfunc responseEncoder(name string,raw bool)response.Encoder{encoder:=responseTemplates[name];if encoder==nil{panic(\"unknown response template\")};if raw{encoder=response.RawJSON(encoder)};return encoder}\n" policySource += "}\nfunc newResponseTemplate(raw string)response.Encoder{var cfg response.Config;decoder:=json.NewDecoder(strings.NewReader(raw));decoder.UseNumber();if err:=decoder.Decode(&cfg);err!=nil{panic(err)};return response.MustNew(cfg)}\n"
policySource += "func responseEncoder(name string,raw bool,catalogJSON string)response.Encoder{encoder:=responseTemplates[name];if encoder==nil{panic(\"unknown response template\")};var catalog omnierrors.Catalog;if err:=json.Unmarshal([]byte(catalogJSON),&catalog);err!=nil{panic(err)};if len(catalog.Definitions)>0{encoder=response.MustWithErrors(catalog,encoder)};if raw{encoder=response.RawJSON(encoder)};return encoder}\n"
put(generated, "internal/handler/responseGen.go", generatedHeader+policySource) put(generated, "internal/handler/responseGen.go", generatedHeader+policySource)
imports := fmt.Sprintf("import (\"fmt\"; %q; %q; %q;", frameworkModule+"/httpx", frameworkModule+"/openapi", o.Module+"/internal/servicecontext") imports := fmt.Sprintf("import (\"fmt\"; %q; %q; %q;", frameworkModule+"/httpx", frameworkModule+"/openapi", o.Module+"/internal/servicecontext")
routes := "func RegisterRoutes(router openapi.Registrar, deps *servicecontext.Context) error {\n if router==nil || deps==nil {return fmt.Errorf(\"router and dependencies required\")}\n" routes := "func RegisterRoutes(router openapi.Registrar, deps *servicecontext.Context) error {\n if router==nil || deps==nil {return fmt.Errorf(\"router and dependencies required\")}\n"
@@ -412,9 +429,9 @@ func renderProject(o Options, c *Compiled, starter bool) (map[string][]byte, map
name := upper(r.Handler) name := upper(r.Handler)
// The response policy and body reader cap are installed before App middleware. // The response policy and body reader cap are installed before App middleware.
if r.MaxBodyBytes > 0 { if r.MaxBodyBytes > 0 {
routes += fmt.Sprintf("httpx.HandleWithBodyLimit(router,%q,%q,%d,responseEncoder(%q,%t),h%d.%s", strings.ToUpper(r.Method), r.Path, r.MaxBodyBytes, responseName(r), r.ResponseFormat == "raw", i, name) routes += fmt.Sprintf("httpx.HandleWithBodyLimit(router,%q,%q,%d,responseEncoder(%q,%t,%q),h%d.%s", strings.ToUpper(r.Method), r.Path, r.MaxBodyBytes, responseName(r), r.ResponseFormat == "raw", errorCatalogJSON(c.Spec, append([]string{}, r.Errors...)), i, name)
} else { } else {
routes += fmt.Sprintf("httpx.HandleWithResponse(router,%q,%q,responseEncoder(%q,%t),h%d.%s", strings.ToUpper(r.Method), r.Path, responseName(r), r.ResponseFormat == "raw", i, name) routes += fmt.Sprintf("httpx.HandleWithResponse(router,%q,%q,responseEncoder(%q,%t,%q),h%d.%s", strings.ToUpper(r.Method), r.Path, responseName(r), r.ResponseFormat == "raw", errorCatalogJSON(c.Spec, append([]string{}, r.Errors...)), i, name)
} }
for _, m := range r.Middleware { for _, m := range r.Middleware {
routes += fmt.Sprintf(",deps.Middlewares[%q]", m) routes += fmt.Sprintf(",deps.Middlewares[%q]", m)

View File

@@ -137,7 +137,7 @@ func TestDeclaredErrorsAndAuthentication(t *testing.T) {
func TestInvalidDeclarations(t *testing.T) { func TestInvalidDeclarations(t *testing.T) {
replacements := [][2]string{ replacements := [][2]string{
{`"EMAIL_TAKEN"`, `"UNAUTHENTICATED"`}, {`"EMAIL_TAKEN"`, `"UNAUTHENTICATED"`},
{`EmailTaken 409`, `EmailTaken 200`}, {`EmailTaken 409`, `EmailTaken 204`},
{`EmailTaken 409`, `emailTaken 409`}, {`EmailTaken 409`, `emailTaken 409`},
{`NameTaken 409`, `CodeEmailTaken 409`}, {`NameTaken 409`, `CodeEmailTaken 409`},
{`auth: UserAuth`, `auth: Unknown`}, {`auth: UserAuth`, `auth: Unknown`},

View File

@@ -16,6 +16,7 @@ import (
type Spec struct { type Spec struct {
Title, Version, Service, Description string Title, Version, Service, Description string
Errors []ErrorDefinition Errors []ErrorDefinition
ErrorCatalogs []string
Auth []AuthDefinition Auth []AuthDefinition
Responses []ResponseDefinition Responses []ResponseDefinition
Scalars []ScalarDefinition Scalars []ScalarDefinition
@@ -266,6 +267,10 @@ func (p *parser) parse() error {
} }
p.imports = append(p.imports, v) p.imports = append(p.imports, v)
} }
case "errorCatalog":
if err := p.parseErrorCatalog(); err != nil {
return err
}
case "error": case "error":
if err := p.parseErrors(); err != nil { if err := p.parseErrors(); err != nil {
return err return err

View File

@@ -0,0 +1,92 @@
package consumer_test
import (
"context"
"errors"
"io"
"net/http/httptest"
"strings"
"testing"
"example.com/errorCatalog/internal/apierrors"
"example.com/errorCatalog/internal/handler"
"example.com/errorCatalog/internal/servicecontext"
omni "git.yuntuo.hk/k2safe/go-omni"
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx"
)
func TestGeneratedCatalog(t *testing.T) {
first := apierrors.UsernameInvalid().WithCause(errors.New("private"))
second := apierrors.UsernameInvalid()
if first == second || second.Cause != nil || second.Status != 200 || second.BusinessCode != int64(400002) {
t.Fatal("shared or incomplete generated error")
}
copy := apierrors.Catalog()
delete(copy.Definitions, apierrors.CodeUsernameInvalid)
if len(apierrors.Catalog().Definitions) != 3 {
t.Fatal("shared catalog")
}
cfg := logx.Default()
cfg.Writer = io.Discard
logger, err := logx.New(cfg)
if err != nil {
t.Fatal(err)
}
defer logger.Close()
deps, err := servicecontext.New(context.Background(), logger)
if err != nil {
t.Fatal(err)
}
defer deps.Close()
app := omni.New()
defer app.Close()
// Global middleware exits early: generated route policy must already be active.
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error {
switch c.Header("X-Fixture") {
case "username":
return omnierrors.ByCode(apierrors.CodeUsernameInvalid).WithCause(errors.New("private"))
case "password":
return apierrors.PasswordInvalid()
case "denied":
return apierrors.PermissionDenied()
case "unknown":
return omnierrors.ByCode("MISSING")
default:
return c.OK(map[string]string{"token": "ok"})
}
}
})
if err := handler.RegisterRoutes(app, deps); err != nil {
t.Fatal(err)
}
for _, path := range []string{"/token", "/raw"} {
for _, test := range []struct {
kind string
status int
want string
}{{"username", 200, "用户名格式不正确"}, {"password", 200, "密码格式不正确"}, {"denied", 403, "没有权限"}, {"unknown", 500, "internal server error"}, {"", 200, `"token":"ok"`}} {
method := "POST"
if path == "/raw" {
method = "GET"
}
w := httptest.NewRecorder()
r := httptest.NewRequest(method, path, nil)
r.Header.Set("X-Fixture", test.kind)
app.ServeHTTP(w, r)
if w.Code != test.status || !strings.Contains(w.Body.String(), test.want) || strings.Contains(w.Body.String(), "private") {
t.Fatal(path, test.kind, w.Code, w.Body.String())
}
if test.kind == "username" || test.kind == "password" {
if !strings.Contains(w.Body.String(), `"code":400002`) || !strings.Contains(w.Body.String(), `"ok":false`) {
t.Fatal(w.Body.String())
}
}
if path == "/raw" && test.kind == "" && strings.Contains(w.Body.String(), "code") {
t.Fatal("raw success wrapped")
}
}
}
}

View File

@@ -0,0 +1,13 @@
package account
import (
"git.yuntuo.hk/k2safe/go-omni/httpx"
"io"
)
func (h *Handler) Bounded(c *httpx.Context) error {
if _, err := io.Copy(io.Discard, c.Request().Body); err != nil {
return err
}
return c.Text(200, "ACK")
}

View File

@@ -0,0 +1,129 @@
package consumer_test
import (
"context"
"encoding/json"
"io"
"net/http/httptest"
"strings"
"testing"
"example.com/errorCatalog/internal/handler"
"example.com/errorCatalog/internal/servicecontext"
omni "git.yuntuo.hk/k2safe/go-omni"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx"
"git.yuntuo.hk/k2safe/go-omni/response"
)
func TestGoOwnedCatalogOnGeneratedRoutes(t *testing.T) {
catalog := apierrors.Catalog{DefaultStatus: 400, Definitions: map[string]apierrors.Definition{
"USERNAME_INVALID": {Code: 400002, Message: "用户名格式不正确"},
"TEXT_TOO_LONG": {Status: 200, Code: 400003, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}}},
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "系统错误"},
"BODY_TOO_LARGE": {Status: 413, Code: 413001, Message: "请求体超限"},
}}
lc := logx.Default()
lc.Writer = io.Discard
logger, err := logx.New(lc)
if err != nil {
t.Fatal(err)
}
defer logger.Close()
deps, err := servicecontext.New(context.Background(), logger)
if err != nil {
t.Fatal(err)
}
defer deps.Close()
format := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always", RequestIDField: "-"}
app := omni.New(omni.WithErrors(catalog), omni.WithResponse(format))
defer app.Close()
resolve := func(c *httpx.Context) error {
switch c.Request().URL.Query().Get("kind") {
case "long":
return apierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称", "max": 100})
case "unknown":
return apierrors.ByCode("NOT_REGISTERED")
default:
return apierrors.ByCode("USERNAME_INVALID")
}
}
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error {
if c.Header("X-Early") == "true" {
return resolve(c)
}
return next(c)
}
})
app.GET("/ordinary", resolve)
if err := handler.RegisterRoutes(app, deps); err != nil {
t.Fatal(err)
}
// Explicit route policy stays independent of App errors and response fields.
local := apierrors.Catalog{Definitions: map[string]apierrors.Definition{"USERNAME_INVALID": {Status: 409, Code: 409001, Message: "路由提示"}}}
localFormat := format
localFormat.MessageField = "detail"
app.HandleWithResponse("GET", "/independent", response.MustWithErrors(local, response.MustNew(localFormat)), resolve)
for _, early := range []string{"false", "true"} {
for _, test := range []struct {
kind string
status, code int
message string
}{
{"invalid", 400, 400002, "用户名格式不正确"}, {"long", 200, 400003, "企业名称不能超过100个字符"}, {"unknown", 500, 9999, "系统错误"},
} {
var ordinary string
for _, path := range []string{"/ordinary", "/plain", "/raw", "/cleared"} {
r := httptest.NewRequest("GET", path+"?kind="+test.kind, nil)
r.Header.Set("X-Early", early)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
var body map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatal(err, w.Body.String())
}
if w.Code != test.status || body["code"] != float64(test.code) || body["msg"] != test.message {
t.Fatal(path, early, w.Code, body)
}
if value, ok := body["data"]; !ok || value != nil {
t.Fatal("data:null missing", body)
}
if path == "/ordinary" {
ordinary = w.Body.String()
} else if w.Body.String() != ordinary {
t.Fatal("generated response differs", ordinary, w.Body.String())
}
}
}
r := httptest.NewRequest("GET", "/independent", nil)
r.Header.Set("X-Early", early)
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
if w.Code != 409 || !strings.Contains(w.Body.String(), `"detail":"路由提示"`) || !strings.Contains(w.Body.String(), `"code":409001`) {
t.Fatal(w.Code, w.Body.String())
}
}
for _, path := range []string{"/plain", "/raw"} {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", path+"?kind=success", nil))
if w.Code != 200 || !strings.Contains(w.Body.String(), `"ok":true`) {
t.Fatal(w.Code, w.Body.String())
}
if path == "/raw" && strings.Contains(w.Body.String(), "code") {
t.Fatal("raw success wrapped")
}
}
for _, size := range []int{8, 65} {
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("POST", "/bounded", strings.NewReader(strings.Repeat("x", size))))
if size == 8 {
if w.Code != 200 || w.Body.String() != "ACK" {
t.Fatal(w.Code, w.Body.String())
}
} else if w.Code != 413 || !strings.Contains(w.Body.String(), "请求体超限") {
t.Fatal(w.Code, w.Body.String())
}
}
}

View File

@@ -0,0 +1 @@
package account

View File

@@ -0,0 +1,29 @@
package account
import (
"context"
"example.com/errorCatalog/internal/types"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
)
func check(kind string) (*types.CheckResponse, error) {
switch kind {
case "success":
return &types.CheckResponse{OK: true}, nil
case "long":
return nil, apierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称", "max": 100})
case "unknown":
return nil, apierrors.ByCode("NOT_REGISTERED")
default:
return nil, apierrors.ByCode("USERNAME_INVALID")
}
}
func (s *Service) Plain(ctx context.Context, req *types.CheckRequest) (*types.CheckResponse, error) {
return check(req.Kind)
}
func (s *Service) Raw(ctx context.Context, req *types.CheckRequest) (*types.CheckResponse, error) {
return check(req.Kind)
}
func (s *Service) Cleared(ctx context.Context, req *types.CheckRequest) (*types.CheckResponse, error) {
return check(req.Kind)
}

View File

@@ -0,0 +1,149 @@
package consumer_test
import (
"context"
"encoding/json"
"errors"
"io"
"net/http/httptest"
"strings"
"sync"
"testing"
"example.com/errorCatalog/internal/apierrors"
"example.com/errorCatalog/internal/handler"
"example.com/errorCatalog/internal/servicecontext"
omni "git.yuntuo.hk/k2safe/go-omni"
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx"
)
func TestGeneratedTemplateErrors(t *testing.T) {
var document map[string]any
if err := json.Unmarshal([]byte(handler.OpenAPIDocument), &document); err != nil {
t.Fatal(err)
}
operation := document["paths"].(map[string]any)["/token"].(map[string]any)["post"].(map[string]any)
media := operation["responses"].(map[string]any)["200"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)
example := media["examples"].(map[string]any)["TextTooLong"].(map[string]any)["value"].(map[string]any)
if example["msg"] != "企业名称不能超过100个字符" || example["code"] != float64(400002) {
t.Fatal(example)
}
if len(media["schema"].(map[string]any)["anyOf"].([]any)) != 2 {
t.Fatal("missing success/failure schema")
}
found := false
for _, raw := range document["x-omni-errors"].([]any) {
entry := raw.(map[string]any)
if entry["name"] == "TextTooLong" {
found = true
params := entry["params"].([]any)
if len(params) != 2 || params[0].(map[string]any)["type"] != "string" || params[1].(map[string]any)["type"] != "int" {
t.Fatal(params)
}
}
}
if !found {
t.Fatal("missing template metadata")
}
// Assigning to a typed function also verifies declaration order and Go types.
var typed func(string, int) *omnierrors.APIError = apierrors.TextTooLong
private := errors.New("private cause")
first := typed("企业名称", 100).WithCause(private)
second := typed("备注", 500)
if first.Message != "企业名称不能超过100个字符" || second.Message != "备注不能超过500个字符" || second.Cause != nil || !errors.Is(first, private) {
t.Fatal(first, second)
}
if apierrors.UsernameInvalid().Message != "用户名格式不正确" {
t.Fatal("fixed compatibility")
}
cat := apierrors.Catalog()
cat.Definitions["TEXT_TOO_LONG"].Params[0].Type = "bool"
if typed("企业名称", 100).Message != first.Message {
t.Fatal("mutable catalog")
}
var wg sync.WaitGroup
for n := 0; n < 40; n++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
if n%2 == 0 {
if typed("企业名称", 100).Message != first.Message {
t.Error("isolation")
}
} else {
if typed("备注", 500).Message != second.Message {
t.Error("isolation")
}
}
}(n)
}
wg.Wait()
cfg := logx.Default()
cfg.Writer = io.Discard
logger, err := logx.New(cfg)
if err != nil {
t.Fatal(err)
}
defer logger.Close()
deps, err := servicecontext.New(context.Background(), logger)
if err != nil {
t.Fatal(err)
}
defer deps.Close()
app := omni.New()
defer app.Close()
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error {
switch c.Header("X-Fixture") {
case "company":
return typed("企业名称", 100).WithCause(private)
case "remark":
return omnierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "备注", "max": 500})
case "missing":
return omnierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称"})
case "type":
return omnierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": "企业名称", "max": "100"})
case "fixed":
return apierrors.UsernameInvalid()
case "denied":
return apierrors.ResourceDenied("资源")
default:
return c.OK(map[string]string{"token": "ok"})
}
}
})
if err := handler.RegisterRoutes(app, deps); err != nil {
t.Fatal(err)
}
for _, path := range []string{"/token", "/raw"} {
for _, test := range []struct {
kind string
status int
want string
}{
{"company", 200, first.Message}, {"remark", 200, second.Message}, {"missing", 500, "internal server error"}, {"type", 500, "internal server error"}, {"fixed", 200, "用户名格式不正确"}, {"denied", 403, "资源不可用"}, {"", 200, `"token":"ok"`},
} {
method := "POST"
if path == "/raw" {
method = "GET"
}
w := httptest.NewRecorder()
r := httptest.NewRequest(method, path, nil)
r.Header.Set("X-Fixture", test.kind)
app.ServeHTTP(w, r)
if w.Code != test.status || !strings.Contains(w.Body.String(), test.want) || strings.Contains(w.Body.String(), "private") || strings.Contains(w.Body.String(), "{label}") {
t.Fatal(test.kind, w.Code, w.Body.String())
}
if test.kind == "company" || test.kind == "remark" {
if !strings.Contains(w.Body.String(), `"code":400002`) || !strings.Contains(w.Body.String(), `"data":null`) {
t.Fatal(w.Body.String())
}
}
if path == "/raw" && test.kind == "" && strings.Contains(w.Body.String(), "code") {
t.Fatal("raw response wrapped")
}
}
}
}

View File

@@ -10,7 +10,7 @@ const frameworkModule = "git.yuntuo.hk/k2safe/go-omni"
// Source builds can carry an unpublished VCS version (including +dirty). // Source builds can carry an unpublished VCS version (including +dirty).
// Keep this fallback in sync with the next release; installed CLIs use their // Keep this fallback in sync with the next release; installed CLIs use their
// downloaded module version, which also has a module checksum. // downloaded module version, which also has a module checksum.
const defaultFrameworkVersion = "v0.9.1-preview" const defaultFrameworkVersion = "v0.11.1-preview"
// This target is not a claim that the release already exists. Source users // This target is not a claim that the release already exists. Source users
// select a local framework checkout until the module templates are published. // select a local framework checkout until the module templates are published.

View File

@@ -4,6 +4,7 @@ import (
"strings" "strings"
"time" "time"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx" "git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx" "git.yuntuo.hk/k2safe/go-omni/logx"
"git.yuntuo.hk/k2safe/go-omni/response" "git.yuntuo.hk/k2safe/go-omni/response"
@@ -25,18 +26,19 @@ type TLSConfig struct {
// Config controls the default runtime behavior of an App. // Config controls the default runtime behavior of an App.
type Config struct { type Config struct {
Address string `json:"address"` Address string `json:"address"`
Transport string `json:"transport"` Transport string `json:"transport"`
TLS TLSConfig `json:"tls"` TLS TLSConfig `json:"tls"`
HTTP HTTPConfig `json:"http"` HTTP HTTPConfig `json:"http"`
RequestIDHeader string `json:"requestIdHeader"` RequestIDHeader string `json:"requestIdHeader"`
EnableLogger bool `json:"enableLogger"` EnableLogger bool `json:"enableLogger"`
SlowRequestThreshold time.Duration `json:"slowRequestThreshold"` SlowRequestThreshold time.Duration `json:"slowRequestThreshold"`
TrustedProxies []string `json:"trustedProxies"` TrustedProxies []string `json:"trustedProxies"`
ShutdownTimeout time.Duration `json:"shutdownTimeout"` ShutdownTimeout time.Duration `json:"shutdownTimeout"`
Log logx.Config `json:"log"` Log logx.Config `json:"log"`
Response response.Config `json:"response"` Errors apierrors.Catalog `json:"errors"`
ResponseEncoder response.Encoder `json:"-"` Response response.Config `json:"response"`
ResponseEncoder response.Encoder `json:"-"`
} }
func Default() Config { func Default() Config {
@@ -51,6 +53,18 @@ func Default() Config {
// Validate checks configuration values before an application starts. // Validate checks configuration values before an application starts.
func (c Config) Validate() error { func (c Config) Validate() error {
if err := c.Errors.Validate(); err != nil {
return err
}
if c.ResponseEncoder == nil {
f, err := response.New(c.Response)
if err != nil {
return err
}
if err = f.ValidateErrors(c.Errors); err != nil {
return err
}
}
if err := c.Response.Validate(); err != nil { if err := c.Response.Validate(); err != nil {
return err return err
} }
@@ -151,11 +165,13 @@ func WithTrustedProxies(proxies ...string) Option {
// environment. Later options can still override individual fields. // environment. Later options can still override individual fields.
func WithConfig(settings Config) Option { func WithConfig(settings Config) Option {
settings.Response = settings.Response.Clone() settings.Response = settings.Response.Clone()
settings.Errors = settings.Errors.Clone()
settings.TrustedProxies = append([]string(nil), settings.TrustedProxies...) settings.TrustedProxies = append([]string(nil), settings.TrustedProxies...)
settings.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...) settings.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...)
return func(cfg *Config) { return func(cfg *Config) {
*cfg = settings *cfg = settings
cfg.Response = settings.Response.Clone() cfg.Response = settings.Response.Clone()
cfg.Errors = settings.Errors.Clone()
cfg.TrustedProxies = append([]string(nil), settings.TrustedProxies...) cfg.TrustedProxies = append([]string(nil), settings.TrustedProxies...)
cfg.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...) cfg.Log.RedactKeys = append([]string(nil), settings.Log.RedactKeys...)
} }
@@ -190,3 +206,9 @@ func WithShutdownTimeout(timeout time.Duration) Option {
} }
} }
} }
// WithErrors installs a project-owned, app-local catalog.
func WithErrors(catalog apierrors.Catalog) Option {
catalog = catalog.Clone()
return func(cfg *Config) { cfg.Errors = catalog.Clone() }
}

View File

@@ -19,6 +19,7 @@ func LoadFromEnv(prefix string) (Config, error) {
// ApplyEnv overlays nonempty environment values on a caller's defaults or file // ApplyEnv overlays nonempty environment values on a caller's defaults or file
// settings, then validates the result. Caller-owned slices are not retained. // settings, then validates the result. Caller-owned slices are not retained.
func ApplyEnv(defaults Config, prefix string) (Config, error) { func ApplyEnv(defaults Config, prefix string) (Config, error) {
defaults.Errors = defaults.Errors.Clone()
defaults.TrustedProxies = append([]string(nil), defaults.TrustedProxies...) defaults.TrustedProxies = append([]string(nil), defaults.TrustedProxies...)
defaults.Log.RedactKeys = append([]string(nil), defaults.Log.RedactKeys...) defaults.Log.RedactKeys = append([]string(nil), defaults.Log.RedactKeys...)
return applyEnv(defaults, prefix) return applyEnv(defaults, prefix)

View File

@@ -0,0 +1,75 @@
package config_test
import (
"os"
"path/filepath"
"strings"
"testing"
"git.yuntuo.hk/k2safe/go-omni/config"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/logx"
)
func TestCatalogConfigFiles(t *testing.T) {
for _, file := range []struct{ name, body string }{
{"server.json", `{"response":{"codeType":"int64","successCode":0,"errorCode":9999},"errors":{"defaultStatus":200,"definitions":{"LIMIT":{"code":9223372036854775807,"message":"limit"}}}}`},
{"server.yaml", "response:\n codeType: int64\n successCode: 0\n errorCode: 9999\nerrors:\n defaultStatus: 200\n definitions:\n LIMIT:\n code: 9223372036854775807\n message: limit\n"},
} {
t.Run(file.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), file.name)
if err := os.WriteFile(path, []byte(file.body), 0600); err != nil {
t.Fatal(err)
}
cfg, err := config.LoadFile(path, "CATALOG_FIXTURE_")
if err != nil {
t.Fatal(err)
}
e := apierrors.MustRegistry(cfg.Errors).Resolve(apierrors.ByCode("LIMIT"))
if e.Status != 200 || e.BusinessCode != int64(9223372036854775807) {
t.Fatal(e)
}
})
}
for _, body := range []string{
`{"errors":{"definitions":{"A":{"message":"a","code":1},"A":{"message":"b","code":2}}}}`,
`{"response":{"codeType":"int32","successCode":0,"errorCode":99},"errors":{"definitions":{"A":{"message":"a","code":2147483648}}}}`,
`{"errors":{"definitions":{"A":{"message":"a","code":"a","status":204}}}}`,
} {
path := filepath.Join(t.TempDir(), "bad.json")
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
t.Fatal(err)
}
if _, err := config.LoadFile(path, "CATALOG_FIXTURE_"); err == nil {
t.Fatal("accepted invalid catalog", body)
}
}
}
func TestCatalogReloadSnapshotIsolation(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.json")
original := `{"errors":{"defaultStatus":200,"definitions":{"invalid":{"code":"invalid","message":"original"}}}}`
if err := os.WriteFile(path, []byte(original), 0600); err != nil {
t.Fatal(err)
}
logger, err := logx.New(logx.Default())
if err != nil {
t.Fatal(err)
}
defer logger.Close()
reloader, err := config.NewReloader(path, "CATALOG_RELOAD_", logger)
if err != nil {
t.Fatal(err)
}
snapshot := reloader.Snapshot()
snapshot.Errors.Definitions["invalid"] = apierrors.Definition{Message: "changed"}
if reloader.Snapshot().Errors.Definitions["invalid"].Message != "original" {
t.Fatal("snapshot changed live catalog")
}
if err := os.WriteFile(path, []byte(strings.Replace(original, "original", "new message", 1)), 0600); err != nil {
t.Fatal(err)
}
if err := reloader.Reload(); err == nil || reloader.Snapshot().Errors.Definitions["invalid"].Message != "original" {
t.Fatal("catalog changed without restart", err)
}
}

View File

@@ -0,0 +1,44 @@
package config_test
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"git.yuntuo.hk/k2safe/go-omni/config"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
)
func TestTemplateConfigFiles(t *testing.T) {
for _, file := range []struct{ name, body string }{
{"config.json", `{"errors":{"defaultStatus":200,"definitions":{"LIMIT":{"code":"limit","message":"{label}:{max}","params":[{"name":"label","type":"string","example":"字段"},{"name":"max","type":"int64","example":9223372036854775807}]}}}}`},
{"config.yaml", "errors:\n defaultStatus: 200\n definitions:\n LIMIT:\n code: limit\n message: '{label}:{max}'\n params:\n - name: label\n type: string\n example: 字段\n - name: max\n type: int64\n example: 9223372036854775807\n"},
} {
t.Run(file.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), file.name)
if err := os.WriteFile(path, []byte(file.body), 0600); err != nil {
t.Fatal(err)
}
cfg, err := config.LoadFile(path, "TEMPLATE_TEST_")
if err != nil {
t.Fatal(err)
}
r := apierrors.MustRegistry(cfg.Errors)
if r.Example("LIMIT").Message != "字段:9223372036854775807" {
t.Fatal(r.Example("LIMIT"))
}
cfg.Errors.Definitions["LIMIT"].Params[0].Name = "mutated"
var supplied map[string]any
if err := apierrors.DecodeCatalogJSON([]byte(`{"label":"字段","max":9223372036854775807}`), &supplied); err != nil {
t.Fatal(err)
}
if _, ok := supplied["max"].(json.Number); !ok {
t.Fatal("number precision lost")
}
if r.Resolve(apierrors.ByCode("LIMIT").WithParams(supplied)).Message != "字段:9223372036854775807" {
t.Fatal("parameter decode or snapshot failure")
}
})
}
}

View File

@@ -5,8 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"git.yuntuo.hk/k2safe/go-omni/logx"
"gopkg.in/yaml.v3"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@@ -14,6 +12,10 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/logx"
"gopkg.in/yaml.v3"
) )
// DecodeFile merges a bounded JSON/YAML document into defaults. It uses JSON // DecodeFile merges a bounded JSON/YAML document into defaults. It uses JSON
@@ -45,15 +47,9 @@ func DecodeFile[T any](path string, defaults T) (T, error) {
return zero, errors.New("config: one YAML document required") return zero, errors.New("config: one YAML document required")
} }
case ".json": case ".json":
dec := json.NewDecoder(bytes.NewReader(data)) if err := apierrors.DecodeCatalogJSON(data, &value); err != nil {
dec.UseNumber()
if err := dec.Decode(&value); err != nil {
return zero, err return zero, err
} }
var extra any
if err := dec.Decode(&extra); err != io.EOF {
return zero, errors.New("config: one JSON document required")
}
default: default:
return zero, errors.New("config: expected .json, .yaml or .yml") return zero, errors.New("config: expected .json, .yaml or .yml")
} }
@@ -182,6 +178,7 @@ func (r *Reloader) Snapshot() Config {
defer r.mu.Unlock() defer r.mu.Unlock()
cfg := r.current cfg := r.current
cfg.Response = r.current.Response.Clone() cfg.Response = r.current.Response.Clone()
cfg.Errors = r.current.Errors.Clone()
cfg.TrustedProxies = append([]string(nil), cfg.TrustedProxies...) cfg.TrustedProxies = append([]string(nil), cfg.TrustedProxies...)
cfg.Log.RedactKeys = append([]string(nil), cfg.Log.RedactKeys...) cfg.Log.RedactKeys = append([]string(nil), cfg.Log.RedactKeys...)
return cfg return cfg

211
errors/README.md Normal file
View File

@@ -0,0 +1,211 @@
# 统一错误目录
从 `v0.10.0-preview` 起,业务可以只返回逻辑错误标识,框架从项目目录取得 HTTP 状态、对外业务码与公开提示。目录不依赖具体业务、数据库、身份模型或网络层,也不定义固定返回体。
## 基本规则
- `key` / `Definitions` 的 map key 是唯一逻辑标识,例如 `USERNAME_INVALID`、`user.notFound`。API 新目录允许项目自己的标识命名;旧 `error (...)` 声明保持原有大写标识规则。
- `code` 是对外业务码,支持字符串、整数,同一个码可以由多个不同标识使用。JSON/YAML 和 `.api` 读取整数时保留 int64 精度。
- `DefaultStatus` / `defaultStatus` 只为目录中未写状态的条目提供默认值,省略时为 400。项目可设为 200;单项 `Status` / `status` 优先。
- 目录条目允许有响应体的最终 HTTP 状态:200..599,排除 204、205、304。成功输出与错误输出分开;HTTP 200 并不把错误变成成功。
- 数字码类型与范围遵守选中响应的 `codeType`(string/int32/int64;integer 是旧别名)。不同路由可以选择不同类型的公共响应与目录。
- 新目录声明完整公开提示;每条错误的业务码和提示优先于统一 `errorCode` / `errorMessage` 兜底。旧的同标识 `codes` / `messages` 若与目录冲突,初始化或契约编译报错;相同值允许保留,便于逐步迁移。
## 手写项目
```go
import (
omni "git.yuntuo.hk/k2safe/go-omni"
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/response"
)
catalog := omnierrors.Catalog{
DefaultStatus: 200,
Definitions: map[string]omnierrors.Definition{
"USERNAME_INVALID": {Code: 400002, Message: "用户名格式不正确"},
"PASSWORD_INVALID": {Code: 400002, Message: "密码格式不正确"},
"FORBIDDEN": {Status: 403, Code: 403001, Message: "没有权限"},
},
}
app := omni.New(
omni.WithErrors(catalog),
omni.WithResponse(response.Config{
CodeType: "int32", SuccessCode: 0, ErrorCode: 9999,
MessageField: "msg", DataMode: "always",
}),
)
```
Service 返回 `omnierrors.ByCode("USERNAME_INVALID")`,或 `.WithCause(err)`。业务无需接收 App/HTTP Context。目录属于 App,初始化时校验并复制;后续修改原 map 不影响正在服务的应用。配置快照也不暴露内部 map。
`NewRegistry(catalog)` / `MustRegistry(catalog)` 可在 CLI/任务消费者等非 HTTP 入口使用;`registry.Resolve(err)` 返回独立的 `*APIError`,包含逻辑 `Code`、对外 `BusinessCode`、Status、Message 与内部 Cause。每次调用都创建新对象;不要自行将一个可变错误对象保存为共享单例再调用 WithCause。
原有 `New(status, key, message)` 等明确错误也能被同标识目录条目覆盖;未在目录中的明确错误沿用原状态与提示。未配置目录时,原有行为保持。Go 目录允许 `Definition.Code == nil` 以继续使用旧 Codes/errorCode 映射;新 `.api errorCatalog` 要求显式 code。
## Go 维护错误目录,生成路由继承 App(v0.11.1-preview)
错误可以只在 Go 中定义,并在 `omni.New(omni.WithErrors(catalog), ...)` 注册一次。`.api` 只写接口、请求/响应 DTO 和响应格式,**无需再声明 errorCatalog,也无需为每条路由列出错误**。业务直接返回 `ByCode(key)` 或 `ByCode(key).WithParams(...)`。
选择规则:
- 生成路由没有有效的 `@errors` / `@server errors` 列表时,继承 App 目录,包括 App 配置的 INTERNAL_ERROR 中文兜底。
- 路由显式引用了错误定义时,使用该路由自己的目录;不与 App 目录合并,保持独立语义。
- `@errors none` 清空 `.api` 分组继承的错误列表;清空后按没有路由目录处理,继承 App,而不是禁用错误处理。
- 错误目录与响应格式独立选择。继承 App 错误不覆盖 `.api` 的响应模板、RawJSON、绑定方式和请求体限额;响应 `codeType` 需能表示目录中的业务码。
- App 全局中间件提前返回错误、普通 Handler 和 Context 克隆遵循相同规则。
`response.WithErrors` / `MustWithErrors` 收到空目录时返回原 Encoder,不建立空 Registry,也不在编码时再次解析原始 error。已有 v0.11.0 生成代码中的空包装因此也能通过升级运行时修复;同步升级 CLI 重新生成则不再产生空包装。
可生成的示例见 [errorInheritance.api](../examples/contracts/errorInheritance.api)。Go 目录是在运行时注册的,CLI 不执行项目 Go 代码;静态 OpenAPI 保留 `.api` 中的响应结构,不会自动枚举 Go 目录的具体错误码、提示及状态。这不影响运行时接入,也不要求重复维护目录。
## 文件配置
下面是生成项目的 `etc/config.yaml` 结构;直接 `config.LoadFile` 加载框架配置时去掉外面的 `server`。手写 main 需显式将加载结果传入 `WithConfig`。
```yaml
server:
errors:
defaultStatus: 200
definitions:
USERNAME_INVALID:
code: 400002
message: 用户名格式不正确
FORBIDDEN:
status: 403
code: 403001
message: 没有权限
response:
codeType: int32
successCode: 0
errorCode: 9999
messageField: msg
dataMode: always
```
重复 JSON/YAML key、未知字段、非法状态、非整数数值及类型/范围错误会拒绝加载。目录不支持运行时热更新;修改后重新启动,避免请求与文档在运行期间漂移。
## .api:定义一次,生成调用与文档
完整示例见 [errors.api](../examples/contracts/errors.api)。语法:
```text
errorCatalog CommonErrors (
defaultStatus: 200
errors: {
"UsernameInvalid": {"key": "USERNAME_INVALID", "code": 400002, "message": "用户名格式不正确"},
"PasswordInvalid": {"key": "PASSWORD_INVALID", "code": 400002, "message": "密码格式不正确"}
}
)
```
目录可放进公共 `.api` 再 import;也可以拆成多个命名目录,各自设置默认状态。生成器合并定义时检查目录名、函数名、逻辑标识冲突。`Catalog` 是生成包的保留函数名。目录对象的键是生成的 UpperCamelCase 函数名,条目的 `key` 是运行时逻辑标识,二者用途不同。
在 `@server` 的 `errors` 或路由 `@errors` 中引用函数名,响应仍通过原有 `response` 声明选择:
```text
@server (
response: CommonResponse
errors: UsernameInvalid, PasswordInvalid
)
service account-api {
@handler token
post /token returns (TokenResponse)
}
```
生成结果包括:
- `apierrors.UsernameInvalid()` 等无参数函数,每次返回独立错误,支持 WithCause。
- `apierrors.CodeUsernameInvalid` 等逻辑标识常量。
- `apierrors.Catalog()`:整个契约的可复制运行时目录,方便手写路由复用。
- 自动绑定到生成路由的目录:只包含该路由通过 `@errors` / `@server errors` 声明的错误,在 App 全局中间件之前生效,支持认证提前返回和缓冲克隆。
- OpenAPI 的错误响应与示例。同一 HTTP 200 下用 `anyOf` 同时描述成功 DTO 和错误体;多个错误共用数字码时保持多个示例;raw 成功体与失败公共体也分开描述。
业务直接 `return TokenResponse{}, apierrors.UsernameInvalid()`。也可以返回 `omnierrors.ByCode(apierrors.CodeUsernameInvalid)`;使用标识引用时,当前生效的目录必须包含该错误。采用 `.api` 管理路由独立目录时,新增错误需补充该路由的 `@errors`;采用 Go 注册 App 目录时,无需在 `.api` 重复声明。
显式声明了错误列表的生成路由使用 `.api` 目录,未声明的路由继承 App;响应格式始终按各自配置选择。手写路由可在 main 用 `omni.WithErrors(apierrors.Catalog())` 复用目录并选择兼容的 `WithResponse`。已有手写文件保持不动。
## 提示模板参数(v0.11.0-preview)
模板属于错误目录,业务只传具体值。参数用**有序列表**声明,顺序就是生成函数的参数顺序;每个参数均为必传,`example` 只用于文档。
```text
errorCatalog CommonErrors (
defaultStatus: 200
errors: {
"TextTooLong": {
"key": "TEXT_TOO_LONG",
"code": 400002,
"message": "{label}不能超过{max}个字符",
"params": [
{"name": "label", "type": "string", "example": "企业名称"},
{"name": "max", "type": "int", "example": 100}
]
}
}
)
```
与数字码匹配的公共响应及路由声明见完整 [errorParams.api](../examples/contracts/errorParams.api)。生成后:
```go
apierrors.TextTooLong("企业名称", 100) // Message:企业名称不能超过100个字符
apierrors.TextTooLong("备注", 500) // Message:备注不能超过500个字符
apierrors.UsernameInvalid() // 原有固定错误保持无参数
```
生成函数在返回前完成解析,非 HTTP Service、任务或 CLI 同样能直接读取完整 Message。调用 `.WithCause(err)` 保留内部原因及 `errors.Is/As` 错误链,不自动把原因加入提示。每次返回新对象,不能把可变错误保存成共享单例。
运行时入口使用同一个解析器:
```go
err := omnierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{
"label": "企业名称",
"max": 100,
})
// HTTP:将 err 返回,当前 App/路由目录负责解析。
// 非 HTTP:使用项目自己的目录显式解析。
registry := omnierrors.MustRegistry(apierrors.Catalog())
resolved := registry.Resolve(err)
fmt.Println(resolved.Message) // 企业名称不能超过100个字符
```
Go 配置中的 `Definition.Params` 是 `[]omnierrors.Parameter`,JSON/YAML 使用相同的 `params` 数组。目录在初始化时复制并编译模板;`WithParams` 返回新错误并复制参数,之后修改原 map 不会改变该错误。重复解析、路由策略和 Context 缓冲克隆保留参数。
规则如下:
- 参数名使用 lowerCamelCase,并且不是 Go 关键字。顺序是调用 API 的一部分,重排或变更类型需要同步调整调用方。
- 支持 `string`、`bool`、`int`、`int32`、`int64`、`float64`。运行时允许整数类型之间按声明范围归一化,拒绝将字符串或浮点数隐式转成整数;float64 要求有限数值。`int` 范围随 Go 目标平台变化,需要固定位宽时选 int32/int64。
- 只接受标量,不接受 nil、切片、map、指针、任意对象或 Stringer。通过 JSON 构建运行时参数时使用 `Decoder.UseNumber()`,避免丢失大整数精度。
- 模板使用 `{name}`;同一参数可重复出现。`{{` / `}}` 表示字面花括号。插入值不会再次解析,即使 label 本身包含 `{max}`,也只是普通文本。
- **省略 params** 表示旧固定提示,字面花括号不变。不接受空 params 数组;重复参数、未声明占位符、未使用参数、不匹配的花括号、非法类型及错误的 example 会在目录加载/契约校验时失败。
- 运行时缺参、多传参数、类型或范围错误转为系统错误兜底;不会将原始模板或参数值作为诊断发给客户端。内部 cause 中可通过 `errors.As` 获取 `*omnierrors.ParamError`,访问日志带 `errorParameter` 诊断,不自动记录参数值。
- 项目可以配置固定的 INTERNAL_ERROR 兜底;若它自身也需要参数且无法解析,则回退框架固定 500,避免递归失败。
- 模板错误的提示只放目录中;同标识再配置旧 `messages` 会被拒绝。响应字段、HTTP 状态、`data:null` 仍由项目策略决定,例如 `dataMode: always` 保留空 data。
OpenAPI 的 `x-omni-errors` 保留模板和有序参数定义,响应 examples 使用同一个解析器生成具体文案。未提供 example 时使用稳定样例(string 使用参数名,bool 使用 false,数值使用 1);这些样例不充当运行时默认值,也不是新增 HTTP 请求参数。
幂等重放保存当次**已解析的公开错误快照**,保留原响应与失败信息。目录更新后,仍在有效期内的重放不会被新文案覆盖;快照不持久化原始参数或内部 cause。整数业务码以不受 JSON 中间层浮点转换影响的方式保存。旧缓存仅保留已有错误标识,不能补推当时缺失的提示与业务码。共享存储的错误快照另受 MaxResponseBytes 限额约束,超限保留不可重放记录,避免重复执行业务。
## 特殊协议与失败信息
`response.WithErrors(catalog, encoder)` / `MustWithErrors` 可绑定路由专用策略,再通过 `HandleWithResponse` 注册。内置模板校验目录码类型与旧映射;自定义 Encoder 自行决定最终 HTTP 状态、媒体类型和字段结构,可将失败转换成第三方要求的文本/XML 回执。一个 Encoder 作用域不叠加不同目录;外层空目录或相同目录不会覆盖已有目录。
Encoder 的 `response.Result` 包含逻辑 `Code`、对外 `BusinessCode`、公开 `Message`、原始 `Err` 及 `Failed`。使用 `result.IsError()` 判断失败,不要仅判断 `Status >= 400`。`RawJSON` 仅让成功结果保持未包装,HTTP 200 的失败仍使用选定错误策略。`c.Text` / `c.XML` / `c.JSON` 等直接输出及已提交的标准 Handler 响应不会被目录重新包装。
启用访问日志时,记录最终写出的 HTTP 状态,并保留 `failed`、`errorKey` 和可用的 `businessCode`。内置指标将已处理的业务失败计入 Errors;链路记录保留 HTTP 200 和业务失败标记。标准中间件桥接和本地/共享幂等重放保留失败标识;已有缓存条目没有新增元数据时无法追溯识别业务失败,等待正常过期即可。
未知的 ByCode 标识、普通 error、panic 默认走 `INTERNAL_ERROR`,公开兜底不暴露 cause。项目可显式配置 INTERNAL_ERROR 条目,但目录 DefaultStatus 本身不会改变系统兜底。启用访问日志后,可通过原始 error 追查未知标识和内部原因。
## 升级
```sh
go get git.yuntuo.hk/k2safe/go-omni@v0.11.1-preview
go install git.yuntuo.hk/k2safe/go-omni/cmd/go-omni@v0.11.1-preview
go-omni api generate --dry-run
go-omni api generate
go test ./...
```
先升级框架依赖,再用同版 CLI 生成;新生成代码最低依赖 v0.11.0-preview。原来的错误构造函数、旧 `error (...)`、Codes/Messages 与已生成代码可继续运行。把业务中散落的状态/提示逐步替换为生成函数属于项目迁移工作,框架不会自动改写业务逻辑。

250
errors/catalog.go Normal file
View File

@@ -0,0 +1,250 @@
package apierrors
import (
"bytes"
"encoding/json"
stdErrors "errors"
"fmt"
"io"
"math"
"reflect"
"strings"
)
// Definition belongs to the project. Code is the public string/integer code;
// the Catalog map key is the distinct logical identifier used by ByCode.
// Nil Code retains legacy response code mapping (useful when migrating).
type Definition struct {
Status int `json:"status,omitempty"`
Code any `json:"code,omitempty"`
Message string `json:"message"`
Params []Parameter `json:"params,omitempty"`
}
type Catalog struct {
DefaultStatus int `json:"defaultStatus,omitempty"`
Definitions map[string]Definition `json:"definitions"`
}
// UnmarshalJSON preserves int64 precision and rejects ambiguous duplicate keys.
func (c *Catalog) UnmarshalJSON(data []byte) error {
type plain Catalog
value := plain(c.Clone())
if err := DecodeCatalogJSON(data, &value); err != nil {
return err
}
*c = Catalog(value)
return nil
}
// DecodeCatalogJSON strictly decodes catalog metadata without float conversion.
// It is also used by the API compiler so runtime and contracts agree.
func DecodeCatalogJSON(data []byte, dst any) error {
scan := json.NewDecoder(bytes.NewReader(data))
scan.UseNumber()
var read func() error
read = func() error {
token, err := scan.Token()
if err != nil {
return err
}
if d, ok := token.(json.Delim); ok {
switch d {
case '{':
seen := map[string]bool{}
for scan.More() {
k, err := scan.Token()
if err != nil {
return err
}
key, ok := k.(string)
if !ok {
return fmt.Errorf("errors: object key required")
}
if seen[key] {
return fmt.Errorf("errors: duplicate key %q", key)
}
seen[key] = true
if err := read(); err != nil {
return err
}
}
case '[':
for scan.More() {
if err := read(); err != nil {
return err
}
}
default:
return fmt.Errorf("errors: invalid JSON delimiter")
}
_, err = scan.Token()
return err
}
return nil
}
if err := read(); err != nil {
return err
}
if _, err := scan.Token(); err != io.EOF {
return fmt.Errorf("errors: one JSON value required")
}
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
dec.DisallowUnknownFields()
return dec.Decode(dst)
}
func (c Catalog) Clone() Catalog {
out := c
out.Definitions = make(map[string]Definition, len(c.Definitions))
for key, d := range c.Definitions {
if d.Params != nil {
params := make([]Parameter, len(d.Params))
copy(params, d.Params)
d.Params = params
}
out.Definitions[key] = d
}
return out
}
// Registry is immutable after construction and may be shared across requests.
// It has no process-global state. Every Resolve call returns a fresh error.
type Registry struct {
catalog Catalog
messages map[string][]messagePart
}
func NewRegistry(c Catalog) (*Registry, error) {
c = c.Clone()
if c.DefaultStatus == 0 {
c.DefaultStatus = 400
}
if !ValidErrorStatus(c.DefaultStatus) {
return nil, fmt.Errorf("errors: invalid defaultStatus %d", c.DefaultStatus)
}
messages := map[string][]messagePart{}
for key, d := range c.Definitions {
if strings.TrimSpace(key) == "" || strings.TrimSpace(key) != key || strings.ContainsAny(key, "\r\n\t") {
return nil, fmt.Errorf("errors: invalid logical identifier %q", key)
}
if d.Status == 0 {
d.Status = c.DefaultStatus
}
if !ValidErrorStatus(d.Status) {
return nil, fmt.Errorf("errors: %s has invalid status %d", key, d.Status)
}
if strings.TrimSpace(d.Message) == "" {
return nil, fmt.Errorf("errors: %s requires a public message", key)
}
if d.Code != nil {
code, err := normalizeCatalogCode(d.Code)
if err != nil {
return nil, fmt.Errorf("errors: %s code: %w", key, err)
}
d.Code = code
}
parts, err := compileMessage(&d)
if err != nil {
return nil, fmt.Errorf("errors: %s: %w", key, err)
}
messages[key] = parts
c.Definitions[key] = d
}
return &Registry{catalog: c, messages: messages}, nil
}
// ValidErrorStatus permits final responses with a JSON body, including HTTP 200.
func ValidErrorStatus(status int) bool {
return status >= 200 && status <= 599 && status != 204 && status != 205 && status != 304
}
func MustRegistry(c Catalog) *Registry {
r, err := NewRegistry(c)
if err != nil {
panic(err)
}
return r
}
func (c Catalog) Validate() error { _, err := NewRegistry(c); return err }
func (r *Registry) Catalog() Catalog {
if r == nil {
return Catalog{}
}
return r.catalog.Clone()
}
// ByCode identifies an error without coupling business code to its HTTP policy.
// Resolution occurs in the application's/request's registry. An unknown reference
// becomes an internal error; its identifier and cause stay available to logs.
func ByCode(key string) *APIError { return &APIError{Code: key, reference: true} }
func (r *Registry) Resolve(err error) *APIError {
if err == nil {
return nil
}
var source *APIError
if !stdErrors.As(err, &source) || source == nil {
return r.internal(err)
}
if source.paramErr != nil {
return r.internal(stdErrors.Join(source.paramErr, err))
}
if r != nil {
if d, ok := r.catalog.Definitions[source.Code]; ok {
message, paramErr := r.render(source.Code, d, source.params)
if paramErr != nil {
return r.internal(stdErrors.Join(paramErr, err))
}
out := source.Clone()
out.Status, out.Code, out.Message, out.BusinessCode = d.Status, source.Code, message, d.Code
out.reference = false
return out
}
}
if source.reference {
return r.internal(err)
}
return source.Clone()
}
// Internal fallback is deliberately bounded: a misconfigured parameterized
// INTERNAL_ERROR cannot recursively fail while reporting a rendering problem.
func (r *Registry) internal(cause error) *APIError {
out := Internal(cause)
if r != nil {
if d, ok := r.catalog.Definitions[out.Code]; ok {
if message, err := r.render(out.Code, d, nil); err == nil {
out.Status, out.Message, out.BusinessCode = d.Status, message, d.Code
}
}
}
return out
}
func normalizeCatalogCode(value any) (any, error) {
if v, ok := value.(string); ok {
if v == "" {
return nil, fmt.Errorf("must be nonempty")
}
return v, nil
}
if v, ok := value.(json.Number); ok {
n, err := v.Int64()
if err != nil {
return nil, fmt.Errorf("must be an exact int64")
}
return n, nil
}
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if v.Uint() <= math.MaxInt64 {
return int64(v.Uint()), nil
}
}
return nil, fmt.Errorf("must be a string or signed 64-bit integer")
}

100
errors/catalog_test.go Normal file
View File

@@ -0,0 +1,100 @@
package apierrors_test
import (
"encoding/json"
"errors"
"fmt"
"math"
"sync"
"testing"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
)
func TestCatalogIsolationDefaultsAndCauses(t *testing.T) {
catalog := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{
"FIRST": {Code: 400002, Message: "first"}, "SECOND": {Code: 400002, Message: "second"}, "DENIED": {Status: 403, Code: "denied", Message: "denied"},
}}
r, err := apierrors.NewRegistry(catalog)
if err != nil {
t.Fatal(err)
}
catalog.Definitions["FIRST"] = apierrors.Definition{Message: "changed"}
r.Catalog().Definitions["FIRST"] = apierrors.Definition{Message: "also changed"}
cause := errors.New("private secret")
var wg sync.WaitGroup
for i := 0; i < 40; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ref := apierrors.ByCode("FIRST").WithCause(cause)
e := r.Resolve(fmt.Errorf("wrapped: %w", ref))
if e.Status != 200 || e.BusinessCode != int64(400002) || e.Message != "first" || !errors.Is(e, cause) {
t.Error(e)
}
e.WithCause(errors.New("different"))
if ref.Cause != cause || r.Resolve(apierrors.ByCode("FIRST")).Cause != nil {
t.Error("shared state")
}
}()
}
wg.Wait()
if got := r.Resolve(apierrors.ByCode("SECOND")); got.Message != "second" || got.BusinessCode != int64(400002) {
t.Fatal(got)
}
if got := r.Resolve(apierrors.ByCode("DENIED")); got.Status != 403 || got.BusinessCode != "denied" {
t.Fatal(got)
}
other := apierrors.MustRegistry(apierrors.Catalog{Definitions: map[string]apierrors.Definition{"FIRST": {Status: 409, Code: 9, Message: "other"}}})
if got := other.Resolve(apierrors.ByCode("FIRST")); got.Status != 409 || got.Message != "other" {
t.Fatal(got)
}
if got := r.Resolve(apierrors.ByCode("MISSING").WithCause(cause)); got.Status != 500 || got.Code != "INTERNAL_ERROR" || got.Message == cause.Error() || !errors.Is(got, cause) {
t.Fatal(got)
}
if got := apierrors.From(apierrors.ByCode("MISSING")); got.Status != 500 {
t.Fatal(got)
}
legacy := apierrors.New(422, "OLD", "legacy")
if got := r.Resolve(legacy); got == legacy || got.Status != 422 || got.Message != "legacy" {
t.Fatal(got)
}
}
func TestCatalogStrictJSONAndValues(t *testing.T) {
raw := `{"defaultStatus":200,"definitions":{"LARGE":{"code":9223372036854775807,"message":"large"}}}`
var c apierrors.Catalog
if err := json.Unmarshal([]byte(raw), &c); err != nil {
t.Fatal(err)
}
e := apierrors.MustRegistry(c).Resolve(apierrors.ByCode("LARGE"))
if e.BusinessCode != int64(math.MaxInt64) {
t.Fatal(e.BusinessCode)
}
for _, raw := range []string{
`{"definitions":{"A":{"code":1,"message":"a"},"A":{"code":2,"message":"b"}}}`,
`{"definitions":{"A":{"code":1,"code":2,"message":"a"}}}`,
`{"definitions":{"A":{"code":1,"message":"a","typo":true}}}`,
`{"defaultStatus":204,"definitions":{}}`,
`{"definitions":{"A":{"code":1.5,"message":"a"}}}`,
`{"definitions":{"A":{"code":9223372036854775808,"message":"a"}}}`,
} {
var invalid apierrors.Catalog
err := json.Unmarshal([]byte(raw), &invalid)
if err == nil {
err = invalid.Validate()
}
if err == nil {
t.Errorf("accepted %s", raw)
}
}
for _, status := range []int{0, 99, 199, 204, 205, 304, 600} {
c := apierrors.Catalog{Definitions: map[string]apierrors.Definition{"A": {Status: status, Code: "a", Message: "a"}}}
if status == 0 {
continue
}
if c.Validate() == nil {
t.Fatal(status)
}
}
}

View File

@@ -6,12 +6,17 @@ import (
) )
// APIError is the public error shape used by handlers and middleware. // APIError is the public error shape used by handlers and middleware.
// Cause is retained for logging and unwrapping, but is never serialized. // Cause is retained for logging and unwrapping; default response encoders and
// replay snapshots never serialize it.
type APIError struct { type APIError struct {
Status int Status int
Code string Code string
Message string Message string
Cause error Cause error
BusinessCode any // Optional public code resolved from a project catalog.
reference bool
params map[string]any
paramErr error
} }
func (e *APIError) Error() string { func (e *APIError) Error() string {
@@ -68,6 +73,9 @@ func From(err error) *APIError {
} }
var apiErr *APIError var apiErr *APIError
if stdErrors.As(err, &apiErr) && apiErr != nil { if stdErrors.As(err, &apiErr) && apiErr != nil {
if apiErr.reference {
return Internal(err)
}
return apiErr return apiErr
} }
return Internal(err) return Internal(err)

272
errors/params.go Normal file
View File

@@ -0,0 +1,272 @@
package apierrors
import (
"encoding/json"
"fmt"
"go/token"
"math"
"reflect"
"regexp"
"strconv"
"strings"
)
// Parameter is ordered: declaration order is the generated function's argument
// order. Example is documentation-only and never substitutes a missing value.
type Parameter struct {
Name string `json:"name"`
Type string `json:"type"`
Example any `json:"example,omitempty"`
}
type messagePart struct{ text, param string }
var parameterName = regexp.MustCompile(`^[a-z][a-zA-Z0-9]*$`)
// ParamError describes a programming/configuration error without including the
// supplied value. It remains available through errors.As on the internal cause.
type ParamError struct{ Code, Param, Problem string }
func (e *ParamError) Error() string {
return fmt.Sprintf("errors: %s parameter %q: %s", e.Code, e.Param, e.Problem)
}
// WithParams returns a detached error. It never retains the caller's map or any
// mutable parameter value. Catalog-specific validation happens during Resolve.
func (e *APIError) WithParams(values map[string]any) *APIError {
out := e.Clone()
out.params = make(map[string]any, len(values))
out.paramErr = nil
for name, value := range values {
scalar, err := copyScalar(value)
if err != nil {
out.paramErr = &ParamError{Code: out.Code, Param: name, Problem: "expected a scalar value"}
continue
}
out.params[name] = scalar
}
return out
}
// Clone detaches request-specific parameters; Cause remains an error chain.
func (e *APIError) Clone() *APIError {
if e == nil {
return nil
}
out := *e
out.params = make(map[string]any, len(e.params))
for k, v := range e.params {
out.params[k] = v
}
return &out
}
func copyScalar(v any) (any, error) {
if n, ok := v.(json.Number); ok {
return n, nil
}
if v == nil {
return nil, fmt.Errorf("nil scalar")
}
r := reflect.ValueOf(v)
switch r.Kind() {
case reflect.String:
return r.String(), nil
case reflect.Bool:
return r.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return r.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return r.Uint(), nil
case reflect.Float32, reflect.Float64:
return r.Float(), nil
}
return nil, fmt.Errorf("unsupported scalar")
}
func paramValue(kind string, v any) (any, error) {
value, err := copyScalar(v)
if err != nil {
return nil, err
}
switch kind {
case "string":
if s, ok := value.(string); ok {
return s, nil
}
case "bool":
if b, ok := value.(bool); ok {
return b, nil
}
case "int", "int32", "int64":
var n int64
switch x := value.(type) {
case int64:
n = x
case uint64:
if x > math.MaxInt64 {
return nil, fmt.Errorf("integer overflow")
}
n = int64(x)
case json.Number:
n, err = x.Int64()
if err != nil {
return nil, fmt.Errorf("expected exact integer")
}
default:
return nil, fmt.Errorf("expected %s", kind)
}
if (kind == "int32" || kind == "int" && strconv.IntSize == 32) && (n < math.MinInt32 || n > math.MaxInt32) {
return nil, fmt.Errorf("%s overflow", kind)
}
return n, nil
case "float64":
var n float64
switch x := value.(type) {
case float64:
n = x
case json.Number:
n, err = x.Float64()
if err != nil {
return nil, fmt.Errorf("expected finite float64")
}
default:
return nil, fmt.Errorf("expected float64")
}
if math.IsNaN(n) || math.IsInf(n, 0) {
return nil, fmt.Errorf("expected finite float64")
}
return n, nil
}
return nil, fmt.Errorf("expected %s", kind)
}
func compileMessage(d *Definition) ([]messagePart, error) {
if d.Params == nil {
return []messagePart{{text: d.Message}}, nil
}
if len(d.Params) == 0 {
return nil, fmt.Errorf("params must be nonempty; omit params for a fixed message")
}
declared := map[string]bool{}
for i, p := range d.Params {
if !parameterName.MatchString(p.Name) || token.Lookup(p.Name).IsKeyword() || declared[p.Name] {
return nil, fmt.Errorf("invalid or duplicate parameter %q", p.Name)
}
switch p.Type {
case "string", "bool", "int", "int32", "int64", "float64":
default:
return nil, fmt.Errorf("unsupported parameter type %q", p.Type)
}
declared[p.Name] = true
if p.Example != nil {
example, err := paramValue(p.Type, p.Example)
if err != nil {
return nil, fmt.Errorf("example for %s: %w", p.Name, err)
}
d.Params[i].Example = example
}
}
var parts []messagePart
var literal strings.Builder
used := map[string]bool{}
flush := func() {
if literal.Len() > 0 {
parts = append(parts, messagePart{text: literal.String()})
literal.Reset()
}
}
for i := 0; i < len(d.Message); {
ch := d.Message[i]
if ch != '{' && ch != '}' {
literal.WriteByte(ch)
i++
continue
}
if i+1 < len(d.Message) && d.Message[i+1] == ch {
literal.WriteByte(ch)
i += 2
continue
}
if ch == '}' {
return nil, fmt.Errorf("unescaped closing brace")
}
end := strings.IndexByte(d.Message[i+1:], '}')
if end < 0 {
return nil, fmt.Errorf("unclosed placeholder")
}
name := d.Message[i+1 : i+1+end]
if !declared[name] {
return nil, fmt.Errorf("undeclared placeholder %q", name)
}
flush()
parts = append(parts, messagePart{param: name})
used[name] = true
i += end + 2
}
flush()
for _, p := range d.Params {
if !used[p.Name] {
return nil, fmt.Errorf("unused parameter %q", p.Name)
}
}
return parts, nil
}
func (r *Registry) render(key string, d Definition, params map[string]any) (string, error) {
values := make(map[string]any, len(d.Params))
for _, p := range d.Params {
v, ok := params[p.Name]
if !ok {
return "", &ParamError{key, p.Name, "missing value"}
}
normalized, err := paramValue(p.Type, v)
if err != nil {
return "", &ParamError{key, p.Name, err.Error()}
}
values[p.Name] = normalized
}
for name := range params {
if _, ok := values[name]; !ok {
return "", &ParamError{key, name, "not declared"}
}
}
var result strings.Builder
for _, part := range r.messages[key] {
if part.param != "" {
fmt.Fprint(&result, values[part.param])
} else {
result.WriteString(part.text)
}
}
return result.String(), nil
}
// Example resolves documentation values using the same path as runtime errors.
// Missing examples receive deterministic samples, never runtime defaults.
func (r *Registry) Example(key string) *APIError {
if r == nil {
return r.Resolve(ByCode(key))
}
d, ok := r.catalog.Definitions[key]
if !ok {
return r.Resolve(ByCode(key))
}
values := map[string]any{}
for _, p := range d.Params {
v := p.Example
if v == nil {
switch p.Type {
case "string":
v = p.Name
case "bool":
v = false
case "float64":
v = float64(1)
default:
v = int64(1)
}
}
values[p.Name] = v
}
return r.Resolve(ByCode(key).WithParams(values))
}

160
errors/params_test.go Normal file
View File

@@ -0,0 +1,160 @@
package apierrors_test
import (
"encoding/json"
"errors"
"fmt"
"math"
"strings"
"sync"
"testing"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
)
func templateCatalog() apierrors.Catalog {
return apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{
"TEXT_TOO_LONG": {Code: 400002, Message: "{label}不能超过{max}个字符", Params: []apierrors.Parameter{{Name: "label", Type: "string", Example: "企业名称"}, {Name: "max", Type: "int", Example: 100}}},
"FIXED": {Code: 400003, Message: "literal {unchanged}"},
}}
}
func TestMessageParamsIsolationAndResolution(t *testing.T) {
catalog := templateCatalog()
r := apierrors.MustRegistry(catalog)
catalog.Definitions["TEXT_TOO_LONG"].Params[0].Type = "bool"
r.Catalog().Definitions["TEXT_TOO_LONG"].Params[0].Name = "changed"
private := errors.New("private cause")
values := map[string]any{"label": "企业名称", "max": 100}
reference := apierrors.ByCode("TEXT_TOO_LONG").WithParams(values).WithCause(private)
values["label"] = "changed"
first := r.Resolve(fmt.Errorf("wrapped: %w", reference))
for n := 0; n < 3; n++ {
first = r.Resolve(first)
if first.Message != "企业名称不能超过100个字符" || first.Status != 200 || first.BusinessCode != int64(400002) || !errors.Is(first, private) {
t.Fatal(first)
}
}
second := r.Resolve(first.WithParams(map[string]any{"label": "备注", "max": 500}))
if first.Message != "企业名称不能超过100个字符" || second.Message != "备注不能超过500个字符" {
t.Fatal(first, second)
}
if r.Example("TEXT_TOO_LONG").Message != first.Message {
t.Fatal("example and runtime differ")
}
if r.Resolve(apierrors.ByCode("FIXED")).Message != "literal {unchanged}" {
t.Fatal("legacy braces changed")
}
var wg sync.WaitGroup
for n := 0; n < 80; n++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
label, max := "企业名称", 100
if n%2 == 1 {
label, max = "备注", 500
}
got := r.Resolve(apierrors.ByCode("TEXT_TOO_LONG").WithParams(map[string]any{"label": label, "max": max}).WithCause(private))
if got.Message != fmt.Sprintf("%s不能超过%d个字符", label, max) || !errors.Is(got, private) {
t.Error(got)
}
}(n)
}
wg.Wait()
}
func TestInvalidTemplateParamsFailClosed(t *testing.T) {
r := apierrors.MustRegistry(templateCatalog())
cause := errors.New("private")
for _, values := range []map[string]any{
nil, {"label": "x"}, {"label": "x", "max": "100"}, {"label": 100, "max": 100},
{"label": "x", "max": 100.0}, {"label": "x", "max": 100, "extra": "private-value"},
{"label": "x", "max": nil}, {"label": map[string]any{"secret": "private-value"}, "max": 100},
} {
result := r.Resolve(apierrors.ByCode("TEXT_TOO_LONG").WithParams(values).WithCause(cause))
var diagnostic *apierrors.ParamError
if result.Code != "INTERNAL_ERROR" || result.Status != 500 || strings.Contains(result.Message, "{") || !errors.As(result, &diagnostic) || !errors.Is(result, cause) {
t.Fatal(result)
}
if strings.Contains(diagnostic.Error(), "private-value") {
t.Fatal("diagnostic leaked value")
}
}
// A parameterized fallback cannot recurse or expose its template.
c := templateCatalog()
c.Definitions["INTERNAL_ERROR"] = c.Definitions["TEXT_TOO_LONG"]
if result := apierrors.MustRegistry(c).Resolve(errors.New("private")); result.Message != "internal server error" || result.Status != 500 {
t.Fatal(result)
}
for _, change := range []func(*apierrors.Definition){
func(d *apierrors.Definition) { d.Message = "{label}{missing}" },
func(d *apierrors.Definition) { d.Message = "{label}" },
func(d *apierrors.Definition) { d.Message = "{label}{max" },
func(d *apierrors.Definition) { d.Message = "{label}{max}}" },
func(d *apierrors.Definition) { d.Params[1].Name = "label" },
func(d *apierrors.Definition) { d.Params[1].Name = "type" },
func(d *apierrors.Definition) { d.Params[1].Type = "interface{}" },
func(d *apierrors.Definition) { d.Params[1].Example = "100" },
func(d *apierrors.Definition) { d.Params = []apierrors.Parameter{} },
} {
c := templateCatalog()
d := c.Definitions["TEXT_TOO_LONG"]
change(&d)
c.Definitions["TEXT_TOO_LONG"] = d
if _, err := apierrors.NewRegistry(c); err == nil {
t.Fatal("accepted invalid template", d)
}
}
}
func TestParamTypesEscapingAndSnapshot(t *testing.T) {
c := apierrors.Catalog{Definitions: map[string]apierrors.Definition{"T": {Code: "t", Message: "{{literal}} {value} {value}", Params: []apierrors.Parameter{{Name: "value", Type: "string"}}}}}
r := apierrors.MustRegistry(c)
got := r.Resolve(apierrors.ByCode("T").WithParams(map[string]any{"value": "{other}"}))
if got.Message != "{literal} {other} {other}" {
t.Fatal("recursive substitution", got)
}
for _, test := range []struct {
kind string
good, bad any
want string
}{
{"int32", int64(math.MaxInt32), int64(math.MaxInt32) + 1, "2147483647"},
{"int64", json.Number("9223372036854775807"), json.Number("9223372036854775808"), "9223372036854775807"},
{"bool", true, "true", "true"}, {"float64", float64(1.25), math.Inf(1), "1.25"},
} {
d := c.Definitions["T"]
d.Params = []apierrors.Parameter{{Name: "value", Type: test.kind}}
c.Definitions["T"] = d
r = apierrors.MustRegistry(c)
if x := r.Resolve(apierrors.ByCode("T").WithParams(map[string]any{"value": test.good})); x.Message != "{literal} "+test.want+" "+test.want {
t.Fatal(test.kind, x)
}
if x := r.Resolve(apierrors.ByCode("T").WithParams(map[string]any{"value": test.bad})); x.Status != 500 {
t.Fatal(test.kind, x)
}
}
original := apierrors.New(200, "T", "complete").WithCause(errors.New("private"))
original.BusinessCode = int64(math.MaxInt64)
raw, err := json.Marshal(original.Snapshot())
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(raw), "private") || strings.Contains(string(raw), "params") {
t.Fatal(string(raw))
}
// Simulate a store rewriting its envelope with ordinary JSON numbers.
var storage map[string]any
if err = json.Unmarshal(raw, &storage); err != nil {
t.Fatal(err)
}
raw, err = json.Marshal(storage)
if err != nil {
t.Fatal(err)
}
var snapshot apierrors.Snapshot
if err = json.Unmarshal(raw, &snapshot); err != nil {
t.Fatal(err)
}
restored := snapshot.ErrorValue()
if restored.Cause != nil || restored.Message != "complete" || restored.BusinessCode != int64(math.MaxInt64) {
t.Fatal(restored)
}
}

71
errors/snapshot.go Normal file
View File

@@ -0,0 +1,71 @@
package apierrors
import "encoding/json"
// Snapshot contains only the resolved public error, suitable for replay
// metadata. Parameters and internal causes are deliberately excluded.
type Snapshot struct {
Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
BusinessCode any `json:"businessCode,omitempty"`
}
// Encode the public code as opaque JSON text so shared stores (including Redis
// Lua/cjson) cannot round an int64 while rewriting their record envelopes.
func (s Snapshot) MarshalJSON() ([]byte, error) {
var encoded string
if s.BusinessCode != nil {
code, err := normalizeCatalogCode(s.BusinessCode)
if err != nil {
return nil, err
}
raw, err := json.Marshal(code)
if err != nil {
return nil, err
}
encoded = string(raw)
}
return json.Marshal(struct {
Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
BusinessCodeJSON string `json:"businessCodeJson,omitempty"`
}{s.Status, s.Code, s.Message, encoded})
}
func (s *Snapshot) UnmarshalJSON(data []byte) error {
type plain Snapshot
var out struct {
plain
BusinessCodeJSON string `json:"businessCodeJson,omitempty"`
}
if err := DecodeCatalogJSON(data, &out); err != nil {
return err
}
if out.BusinessCodeJSON != "" {
if err := DecodeCatalogJSON([]byte(out.BusinessCodeJSON), &out.BusinessCode); err != nil {
return err
}
}
if out.BusinessCode != nil {
code, err := normalizeCatalogCode(out.BusinessCode)
if err != nil {
return err
}
out.BusinessCode = code
}
*s = Snapshot(out.plain)
return nil
}
func (e *APIError) Snapshot() *Snapshot {
if e == nil {
return nil
}
return &Snapshot{Status: e.Status, Code: e.Code, Message: e.Message, BusinessCode: e.BusinessCode}
}
func (s *Snapshot) ErrorValue() *APIError {
if s == nil {
return nil
}
return &APIError{Status: s.Status, Code: s.Code, Message: s.Message, BusinessCode: s.BusinessCode}
}

View File

@@ -0,0 +1,41 @@
syntax = "v1"
// Errors are registered once in Go with omni.WithErrors.
response CommonResponse (
codeType: int32
successCode: 0
errorCode: 9999
body: {"code": "${code}", "msg": "${message}", "data": "${data}"}
dataMode: always
)
type CheckRequest {
Kind string `form:"kind,optional"`
}
type CheckResponse {
OK bool `json:"ok"`
}
@server (
group: account
response: CommonResponse
)
service inherited-errors-api {
@handler plain
get /plain (CheckRequest) returns (CheckResponse)
@response raw
@handler raw
get /raw (CheckRequest) returns (CheckResponse)
@errors none
@handler cleared
get /cleared (CheckRequest) returns (CheckResponse)
@handlerMode manual
@body (mode: raw
maxBytes: 64)
@produces "text/plain"
@handler bounded
post /bounded
}

View File

@@ -0,0 +1,48 @@
syntax = "v1"
errorCatalog CommonErrors (
defaultStatus: 200
errors: {
"TextTooLong": {
"key": "TEXT_TOO_LONG",
"code": 400002,
"message": "{label}不能超过{max}个字符",
"params": [
{"name": "label", "type": "string", "example": "企业名称"},
{"name": "max", "type": "int", "example": 100}
]
},
"UsernameInvalid": {"key": "USERNAME_INVALID", "code": 400003, "message": "用户名格式不正确"},
"ResourceDenied": {
"key": "RESOURCE_DENIED", "status": 403, "code": 403001,
"message": "{label}不可用",
"params": [{"name": "label", "type": "string", "example": "资源"}]
}
}
)
response CommonResponse (
codeType: int32
successCode: 0
errorCode: 9999
body: {"code": "${code}", "msg": "${message}", "data": "${data}"}
dataMode: always
)
type TokenResponse {
Token string `json:"token"`
}
@server (
group: account
response: CommonResponse
errors: TextTooLong, UsernameInvalid, ResourceDenied
)
service error-params-api {
@handler token
post /token returns (TokenResponse)
@response raw
@handler raw
get /raw returns (TokenResponse)
}

View File

@@ -0,0 +1,36 @@
syntax = "v1"
errorCatalog CommonErrors (
defaultStatus: 200
errors: {
"UsernameInvalid": {"key": "USERNAME_INVALID", "code": 400002, "message": "用户名格式不正确"},
"PasswordInvalid": {"key": "PASSWORD_INVALID", "code": 400002, "message": "密码格式不正确"},
"PermissionDenied": {"key": "PERMISSION_DENIED", "status": 403, "code": 403001, "message": "没有权限"}
}
)
response CommonResponse (
codeType: int32
successCode: 0
errorCode: 9999
body: {"ok": "${ok}", "code": "${code}", "data": "${data}"}
errorBody: {"ok": "${ok}", "code": "${code}", "msg": "${message}"}
)
type TokenResponse {
Token string `json:"token"`
}
@server (
group: account
response: CommonResponse
errors: UsernameInvalid, PasswordInvalid, PermissionDenied
)
service errors-api {
@handler token
post /token returns (TokenResponse)
@response raw
@handler raw
get /raw returns (TokenResponse)
}

View File

@@ -45,9 +45,17 @@ func WrapMiddleware(middleware func(http.Handler) http.Handler) Middleware {
child.writer.(interface{ inheritCommit(int) }).inheritCommit(c.Status()) child.writer.(interface{ inheritCommit(int) }).inheritCommit(c.Status())
} }
defer CleanupMultipart(r) defer CleanupMultipart(r)
defer func() { c.values = child.values; c.encoder = child.encoder }() defer func() {
if err := next(child); err != nil && !c.Responded() { c.values = child.values
child.WriteError(err) c.encoder = child.encoder
c.errorRegistry = child.errorRegistry
c.failure = child.failure
}()
if err := next(child); err != nil {
child.RecordError(err)
if !c.Responded() {
child.WriteError(err)
}
} }
})) }))
if wrapped == nil { if wrapped == nil {

View File

@@ -88,13 +88,15 @@ func RoutePattern(ctx context.Context) string {
// only standard-library HTTP types, so it remains usable when the network // only standard-library HTTP types, so it remains usable when the network
// implementation is replaced. // implementation is replaced.
type Context struct { type Context struct {
request *http.Request request *http.Request
writer http.ResponseWriter writer http.ResponseWriter
params map[string]string params map[string]string
values map[string]any values map[string]any
responded bool responded bool
status int status int
encoder response.Encoder encoder response.Encoder
errorRegistry *apierrors.Registry
failure *apierrors.APIError
} }
// NewContext creates a transport-neutral GoOmni context. // NewContext creates a transport-neutral GoOmni context.
@@ -133,6 +135,7 @@ func (c *Context) Raw() http.ResponseWriter { return c.writer }
func (c *Context) Clone(ctx context.Context, writer http.ResponseWriter) *Context { func (c *Context) Clone(ctx context.Context, writer http.ResponseWriter) *Context {
clone := NewContext(c.request.Clone(ctx), writer, c.params) clone := NewContext(c.request.Clone(ctx), writer, c.params)
clone.encoder = c.encoder clone.encoder = c.encoder
clone.errorRegistry = c.errorRegistry
for key, value := range c.values { for key, value := range c.values {
clone.values[key] = value clone.values[key] = value
} }
@@ -279,15 +282,47 @@ func (c *Context) WriteError(err error) {
if errors.As(err, &bodyErr) { if errors.As(err, &bodyErr) {
err = bodyTooLarge() err = bodyTooLarge()
} }
apiErr := apierrors.From(err) apiErr := c.ResolveError(err)
c.failure = apiErr
if writeErr := c.writeResult(response.Result{ if writeErr := c.writeResult(response.Result{
Status: apiErr.Status, BusinessCode: apiErr.BusinessCode,
Code: apiErr.Code, Failed: true,
Message: apiErr.Message, Status: apiErr.Status,
RequestID: c.RequestID(), Code: apiErr.Code,
Err: err, Message: apiErr.Message,
RequestID: c.RequestID(),
Err: err,
}); writeErr != nil && !c.responded { }); writeErr != nil && !c.responded {
// An invalid custom encoder must not recurse or leak a different envelope. // An invalid custom encoder must not recurse or leak a different envelope.
_ = c.WriteResponse(http.StatusInternalServerError, nil, nil) _ = c.WriteResponse(http.StatusInternalServerError, nil, nil)
} }
} }
// SetErrorRegistry sets an application fallback. Route encoders can supply their
// own nonempty registry; an encoder without a registry inherits this fallback.
// Clones retain both policies without any process-global lookup.
func (c *Context) SetErrorRegistry(r *apierrors.Registry) { c.errorRegistry = r }
func (c *Context) ResolveError(err error) *apierrors.APIError {
registry := c.errorRegistry
if provider, ok := c.encoder.(response.ErrorRegistryProvider); ok {
if scoped := provider.ErrorRegistry(); scoped != nil {
registry = scoped
}
}
return registry.Resolve(err)
}
// RecordError preserves failure semantics when a middleware commits buffered bytes.
func (c *Context) RecordError(err error) { c.failure = c.ResolveError(err) }
// RecordFailure propagates an already resolved failure without reapplying a
// different parent/catalog policy. Used with committed or buffered responses.
func (c *Context) RecordFailure(err *apierrors.APIError) { c.failure = err.Clone() }
// Failure returns a detached description of a failure already handled by Context.
func (c *Context) Failure() *apierrors.APIError {
if c.failure == nil {
return nil
}
return c.failure.Clone()
}

View File

@@ -7,7 +7,6 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx" "git.yuntuo.hk/k2safe/go-omni/httpx"
) )
@@ -46,10 +45,10 @@ func (m *Metrics) Middleware() httpx.Middleware {
err := next(ctx) err := next(ctx)
status := ctx.Status() status := ctx.Status()
if err != nil && !ctx.Responded() { if err != nil && !ctx.Responded() {
status = apierrors.From(err).Status status = ctx.ResolveError(err).Status
} }
m.requests.Add(1) m.requests.Add(1)
if err != nil || status >= 400 { if err != nil || ctx.Failure() != nil || status >= 400 {
m.errors.Add(1) m.errors.Add(1)
} }
if status >= 0 && status < len(m.statuses) { if status >= 0 && status < len(m.statuses) {

View File

@@ -31,6 +31,8 @@ type IdempotencyConfig struct {
type replay struct { type replay struct {
fingerprint [32]byte fingerprint [32]byte
errorCode string
failure *apierrors.Snapshot
status int status int
headers http.Header headers http.Header
body []byte body []byte
@@ -150,8 +152,14 @@ func (i *Idempotency) Middleware() httpx.Middleware {
return apierrors.New(409, "IDEMPOTENCY_UNAVAILABLE", "request is in progress or its outcome cannot be replayed") return apierrors.New(409, "IDEMPOTENCY_UNAVAILABLE", "request is in progress or its outcome cannot be replayed")
} }
status, headers, data := saved.status, saved.headers.Clone(), append([]byte(nil), saved.body...) status, headers, data := saved.status, saved.headers.Clone(), append([]byte(nil), saved.body...)
errorCode, failure := saved.errorCode, saved.failure
i.mu.Unlock() i.mu.Unlock()
headers.Set("Idempotency-Replayed", "true") headers.Set("Idempotency-Replayed", "true")
if failure != nil {
c.RecordFailure(failure.ErrorValue())
} else if errorCode != "" {
c.RecordFailure(apierrors.New(status, errorCode, ""))
}
return c.WriteResponse(status, headers, data) return c.WriteResponse(status, headers, data)
} }
if len(i.items) >= i.config.MaxEntries { if len(i.items) >= i.config.MaxEntries {
@@ -171,15 +179,26 @@ func (i *Idempotency) Middleware() httpx.Middleware {
saved.failed = true saved.failed = true
saved.expires = i.now().Add(i.config.TTL) saved.expires = i.now().Add(i.config.TTL)
size := buffer.body.Len() size := buffer.body.Len()
failure := c.Failure().Snapshot()
var metadataErr error
if failure != nil {
var metadata []byte
metadata, metadataErr = json.Marshal(failure)
size += len(metadata) + len(failure.Code)
}
for k, values := range buffer.headers { for k, values := range buffer.headers {
size += len(k) size += len(k)
for _, v := range values { for _, v := range values {
size += len(v) size += len(v)
} }
} }
if buffer.complete && buffer.err == nil && buffer.status < 500 && buffer.headers.Get("Set-Cookie") == "" && !strings.Contains(strings.ToLower(buffer.headers.Get("Cache-Control")), "no-store") && size <= i.config.MaxBytes-i.bytes { if metadataErr == nil && buffer.complete && buffer.err == nil && buffer.status < 500 && buffer.headers.Get("Set-Cookie") == "" && !strings.Contains(strings.ToLower(buffer.headers.Get("Cache-Control")), "no-store") && size <= i.config.MaxBytes-i.bytes {
saved.failed = false saved.failed = false
saved.status = buffer.status saved.status = buffer.status
if failure := c.Failure(); failure != nil {
saved.errorCode = failure.Code
saved.failure = failure.Snapshot()
}
saved.headers = buffer.headers.Clone() saved.headers = buffer.headers.Clone()
saved.body = append([]byte(nil), buffer.body.Bytes()...) saved.body = append([]byte(nil), buffer.body.Bytes()...)
saved.size = size saved.size = size
@@ -202,6 +221,9 @@ func (i *Idempotency) Middleware() httpx.Middleware {
if buffer.status == 0 { if buffer.status == 0 {
buffer.status = 200 buffer.status = 200
} }
if failure := child.Failure(); failure != nil {
c.RecordFailure(failure)
}
buffer.complete = true buffer.complete = true
return c.WriteResponse(buffer.status, buffer.headers, buffer.body.Bytes()) return c.WriteResponse(buffer.status, buffer.headers, buffer.body.Bytes())
} }

View File

@@ -1,6 +1,7 @@
package middleware package middleware
import ( import (
"errors"
"fmt" "fmt"
"runtime/debug" "runtime/debug"
"time" "time"
@@ -73,10 +74,10 @@ func AccessLog(options ...AccessLogOption) httpx.Middleware {
return func(ctx *httpx.Context) error { return func(ctx *httpx.Context) error {
started := time.Now() started := time.Now()
err := next(ctx) err := next(ctx)
status := ctx.Status()
if err != nil && !ctx.Responded() { if err != nil && !ctx.Responded() {
status = apierrors.From(err).Status ctx.WriteError(err)
} }
status := ctx.Status()
duration := time.Since(started) duration := time.Since(started)
slow := settings.slowThreshold > 0 && duration >= settings.slowThreshold slow := settings.slowThreshold > 0 && duration >= settings.slowThreshold
attrs := []any{ attrs := []any{
@@ -87,6 +88,20 @@ func AccessLog(options ...AccessLogOption) httpx.Middleware {
"durationMs", duration.Seconds() * 1000, "durationMs", duration.Seconds() * 1000,
"clientIp", ctx.ClientIP(), "clientIp", ctx.ClientIP(),
} }
failure := ctx.Failure()
if failure == nil && err != nil {
failure = ctx.ResolveError(err)
}
if failure != nil {
attrs = append(attrs, "failed", true, "errorKey", failure.Code)
var parameterErr *apierrors.ParamError
if errors.As(failure, &parameterErr) {
attrs = append(attrs, "errorParameter", parameterErr.Error())
}
if failure.BusinessCode != nil {
attrs = append(attrs, "businessCode", failure.BusinessCode)
}
}
if slow { if slow {
attrs = append(attrs, "slow", true) attrs = append(attrs, "slow", true)
} }

View File

@@ -0,0 +1,81 @@
package redis
import (
"context"
"errors"
"fmt"
"math"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
omni "git.yuntuo.hk/k2safe/go-omni"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/middleware"
"git.yuntuo.hk/k2safe/go-omni/response"
goredis "github.com/redis/go-redis/v9"
)
func TestTemplateErrorRedisReplay(t *testing.T) {
address := os.Getenv("GO_OMNI_REDIS_TEST_ADDR")
if address == "" {
t.Skip("requires isolated Redis")
}
client := goredis.NewClient(&goredis.Options{Addr: address, PoolSize: 2, MaxRetries: -1})
defer client.Close()
backend, err := NewIdempotencyStore(client, fmt.Sprint("template:", time.Now().UnixNano()), IdempotencyConfig{MaxEntries: 4})
if err != nil {
t.Fatal(err)
}
defer client.Del(context.Background(), backend.keys...)
var observed []*apierrors.APIError
calls := 0
var bodies []string
for _, template := range []string{"{label}不能超过{max}个字符", "changed: {label}/{max}"} {
cat := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"LIMIT": {Code: int64(math.MaxInt64), Message: template, Params: []apierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int64"}}}}}
app := omni.New(omni.WithErrors(cat), omni.WithResponse(response.Config{CodeType: "int64", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"}))
idem, err := middleware.NewIdempotency(middleware.IdempotencyConfig{Store: backend, Scope: func(*httpx.Context) (string, error) { return "fixture", nil }})
if err != nil {
t.Fatal(err)
}
app.Use(func(next httpx.Handler) httpx.Handler {
return func(c *httpx.Context) error { err := next(c); observed = append(observed, c.Failure()); return err }
})
app.POST("/", func(c *httpx.Context) error {
calls++
return apierrors.ByCode("LIMIT").WithParams(map[string]any{"label": "备注", "max": int64(math.MaxInt64)}).WithCause(errors.New("privateCause"))
}, idem.Middleware())
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", nil)
r.Header.Set("Idempotency-Key", "same")
app.ServeHTTP(w, r)
app.Close()
if w.Code != 200 || !strings.Contains(w.Body.String(), "备注不能超过9223372036854775807个字符") || !strings.Contains(w.Body.String(), `"code":9223372036854775807`) || !strings.Contains(w.Body.String(), `"data":null`) || strings.Contains(w.Body.String(), "privateCause") {
t.Fatal(w.Code, w.Body.String())
}
bodies = append(bodies, w.Body.String())
}
if calls != 1 || bodies[0] != bodies[1] || len(observed) != 2 {
t.Fatal(calls, bodies, observed)
}
for _, e := range observed {
if e == nil || e.BusinessCode != int64(math.MaxInt64) || e.Message != "备注不能超过9223372036854775807个字符" {
t.Fatal(e)
}
}
if observed[1].Cause != nil {
t.Fatal("persisted internal cause")
}
records, err := client.HVals(context.Background(), backend.keys[0]).Result()
if err != nil {
t.Fatal(err)
}
for _, raw := range records {
if strings.Contains(raw, "privateCause") || strings.Contains(raw, `"params"`) {
t.Fatal("private replay metadata")
}
}
}

View File

@@ -4,19 +4,23 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"encoding/json"
"errors" "errors"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx"
"net/http" "net/http"
"strings" "strings"
"time" "time"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx"
) )
type StoredResponse struct { type StoredResponse struct {
Status int `json:"status"` Status int `json:"status"`
Headers http.Header `json:"headers"` ErrorCode string `json:"errorCode,omitempty"`
Body []byte `json:"body"` Failure *apierrors.Snapshot `json:"failure,omitempty"`
Headers http.Header `json:"headers"`
Body []byte `json:"body"`
} }
type IdempotencyRecord struct { type IdempotencyRecord struct {
Fingerprint string `json:"fingerprint"` Fingerprint string `json:"fingerprint"`
@@ -54,6 +58,11 @@ func (i *Idempotency) shared(c *httpx.Context, next httpx.Handler, id, fingerpri
response := record.Response response := record.Response
headers := response.Headers.Clone() headers := response.Headers.Clone()
headers.Set("Idempotency-Replayed", "true") headers.Set("Idempotency-Replayed", "true")
if response.Failure != nil {
c.RecordFailure(response.Failure.ErrorValue())
} else if response.ErrorCode != "" {
c.RecordFailure(apierrors.New(response.Status, response.ErrorCode, ""))
}
return c.WriteResponse(response.Status, headers, response.Body) return c.WriteResponse(response.Status, headers, response.Body)
} }
completed := false completed := false
@@ -84,9 +93,22 @@ func (i *Idempotency) shared(c *httpx.Context, next httpx.Handler, id, fingerpri
if buffer.status == 0 { if buffer.status == 0 {
buffer.status = 200 buffer.status = 200
} }
if failure := child.Failure(); failure != nil {
c.RecordFailure(failure)
}
var response *StoredResponse var response *StoredResponse
if buffer.status < 500 && buffer.headers.Get("Set-Cookie") == "" && !strings.Contains(strings.ToLower(buffer.headers.Get("Cache-Control")), "no-store") { if buffer.status < 500 && buffer.headers.Get("Set-Cookie") == "" && !strings.Contains(strings.ToLower(buffer.headers.Get("Cache-Control")), "no-store") {
response = &StoredResponse{Status: buffer.status, Headers: buffer.headers.Clone(), Body: append([]byte(nil), buffer.body.Bytes()...)} response = &StoredResponse{Status: buffer.status, Headers: buffer.headers.Clone(), Body: append([]byte(nil), buffer.body.Bytes()...)}
if failure := child.Failure(); failure != nil {
response.ErrorCode = failure.Code
response.Failure = failure.Snapshot()
}
}
if response != nil && response.Failure != nil {
metadata, encodeErr := json.Marshal(response.Failure)
if encodeErr != nil || len(metadata) > i.config.MaxResponseBytes {
response = nil
}
} }
if err = finish(response); err != nil { if err = finish(response); err != nil {
return apierrors.New(503, "IDEMPOTENCY_UNAVAILABLE", "outcome requires reconciliation").WithCause(err) return apierrors.New(503, "IDEMPOTENCY_UNAVAILABLE", "outcome requires reconciliation").WithCause(err)

View File

@@ -49,6 +49,8 @@ func WithRequestIDHeader(header string) Option { return config.WithRequestIDHead
func WithLogger(enabled bool) Option { return config.WithLogger(enabled) } func WithLogger(enabled bool) Option { return config.WithLogger(enabled) }
func WithErrors(catalog apierrors.Catalog) Option { return config.WithErrors(catalog) }
func WithResponse(settings response.Config) Option { return config.WithResponse(settings) } func WithResponse(settings response.Config) Option { return config.WithResponse(settings) }
func WithResponseEncoder(encoder response.Encoder) Option { return config.WithResponseEncoder(encoder) } func WithResponseEncoder(encoder response.Encoder) Option { return config.WithResponseEncoder(encoder) }

View File

@@ -22,3 +22,15 @@
环境:`go version go1.27.1 darwin/arm64`。记录时间:2026-09-23T18:17:16.897323+00:00。 环境:`go version go1.27.1 darwin/arm64`。记录时间:2026-09-23T18:17:16.897323+00:00。
独立 `TransportHTTP2` 的逐请求读写时限未实现,本次没有进行 Linux soak、代理长测或完整业务迁移验收。 独立 `TransportHTTP2` 的逐请求读写时限未实现,本次没有进行 Linux soak、代理长测或完整业务迁移验收。
## 远程验收
发布后两条独立远程验收均通过;各自使用全新 module cache,消费者依赖固定版本,无本地 replace。
- 发布标签:`v0.9.1-preview`;源码:`20e84e5ff86d872f42301cd3c256030fc41c8ce2`。
- module 校验和:`h1:yCqjVGv4rH8UN2M3WzjraNEfdEI6WJD2A64F4HFP0FI=`。
- Connector/HTTP 远程消费者执行全部专项 race 测试及真实 MySQL TLS 用例,无跳过;耗时 16.49 秒,详见 [Connector 远程记录](connectorRemote.json)。
- 从远程安装 CLI,生成消费者,验证标量、动态 JSON、上传/原文签名、下载、请求体上限和重生成保护;耗时 13.27 秒,详见 [CLI 远程记录](connectorCompatibilityRemote.json)。
- 两条验收脚本确认相同框架校验和;临时消费者、缓存、数据库容器与测试证书/凭据已清理。fixture 哈希与当前文件核对一致。
结果在 main 单独追加,已发布标签保持不可变。

View File

@@ -0,0 +1,31 @@
{
"passed": true,
"frameworkVersion": "v0.9.1-preview",
"frameworkSum": "h1:yCqjVGv4rH8UN2M3WzjraNEfdEI6WJD2A64F4HFP0FI=",
"freshModuleCache": true,
"noLocalReplace": true,
"durationSeconds": 13.27,
"checks": [
"remoteCLIInstall",
"scalarContractValidation",
"manualProjectGeneration",
"int64MaxValueOnWire",
"dynamicJSON",
"rawBodySignature",
"routeBodyLimits",
"multipartTempCleanup",
"unwrappedDownloads",
"handwrittenFilesPreserved",
"generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"
],
"sourceHashes": {
"scripts/verifyCompatibilityRelease.py": "90e487d3203ed4eb4bc721da8086356413345b3e05193e473250a834aeb8d293",
"cmd/go-omni/apigen/compatibility_test.go": "98cf5fbe6cb6413c12f709806d385726f3dec1468575f76967786de4707667ba",
"examples/contracts/compatibility.api": "3ede909e7f60386012e55c916a8e4887dd08562e7f023665f663288c809873d4",
"cmd/go-omni/apigen/testdata/compatibility/consumer_test.go": "0a99ffd6a5cc16d47ae8208d9140c8d6afd9c19106fac8e5e61096cb90d8c463",
"cmd/go-omni/apigen/testdata/compatibility/hooks.go": "435baf37c7f8adffd18d4be0353a791124e0bc5dcb79abdcc345276b6f9867f4",
"cmd/go-omni/apigen/testdata/compatibility/echo.go": "9e13cb18a0951974e723a1fd915c0f4610d823c95eadb752c3d1cd75c44fa293",
"cmd/go-omni/apigen/testdata/compatibility/download.go": "e7f4bcdb34c90ec63fdab1f3c4e524964ac153944352d8a9053a9f19b74030fd"
}
}

View File

@@ -0,0 +1,70 @@
{
"passed": true,
"frameworkVersion": "v0.9.1-preview",
"localSource": false,
"frameworkSum": "h1:yCqjVGv4rH8UN2M3WzjraNEfdEI6WJD2A64F4HFP0FI=",
"freshModuleCache": true,
"noLocalReplace": true,
"mysqlImage": "sha256:1afd25fe334a1be64a3866495dd43a6bdba3c55dd1d6190ea12933163fe2843c",
"mysqlLimits": {
"cpus": 1,
"memoryMiB": 768,
"pids": 128
},
"durationSeconds": 16.49,
"tests": [
"TestHTTPConfiguredStandardServer",
"TestHTTPInvalidStartupReleasesResources",
"TestHTTPConfiguredNetworkLimits/nethttp/slowHeaders",
"TestHTTPConfiguredNetworkLimits/nethttp/slowBody",
"TestHTTPConfiguredNetworkLimits/nethttp/idle",
"TestHTTPConfiguredNetworkLimits/nethttp/writeBackpressure",
"TestHTTPFileAndEnvironment/server.json",
"TestHTTPFileAndEnvironment/server.yaml",
"TestHTTPFileAndEnvironment",
"TestHTTPInvalidAndUnsupportedSettings/READ_HEADER_TIMEOUTinvalid",
"TestHTTPInvalidAndUnsupportedSettings/READ_HEADER_TIMEOUT-1",
"TestHTTPInvalidAndUnsupportedSettings/READ_TIMEOUTinvalid",
"TestHTTPInvalidAndUnsupportedSettings/READ_TIMEOUT-1",
"TestHTTPInvalidAndUnsupportedSettings/WRITE_TIMEOUTinvalid",
"TestHTTPInvalidAndUnsupportedSettings/WRITE_TIMEOUT-1",
"TestHTTPInvalidAndUnsupportedSettings/IDLE_TIMEOUTinvalid",
"TestHTTPInvalidAndUnsupportedSettings/IDLE_TIMEOUT-1",
"TestHTTPInvalidAndUnsupportedSettings/MAX_HEADER_BYTESinvalid",
"TestHTTPInvalidAndUnsupportedSettings/MAX_HEADER_BYTES-1",
"TestHTTPInvalidAndUnsupportedSettings",
"TestConnectorIndependentPoolsAndContext",
"TestConnectorRejectsInvalidInputWithoutTakingOwnership",
"TestConnectorStartupFailuresReleaseResources/connect",
"TestConnectorStartupFailuresReleaseResources/ping",
"TestConnectorStartupFailuresReleaseResources",
"TestConnectorTimeoutBoundsConnectAndPing/connect",
"TestConnectorTimeoutBoundsConnectAndPing/ping",
"TestConnectorTimeoutBoundsConnectAndPing",
"TestConnectorHealthBoundsPoolWaitAndRecovers",
"TestConnectorIdleZeroAndLifetime/noIdle",
"TestConnectorIdleZeroAndLifetime/expired",
"TestConnectorIdleZeroAndLifetime",
"TestConnectorTransactionsKeepCommitAndRollbackSemantics",
"TestMySQLConnectorDriverLoggerIsolation",
"TestMySQLConnectorTLSAndPoolIsolation/untrustedCA",
"TestMySQLConnectorTLSAndPoolIsolation/wrongHostname",
"TestMySQLConnectorTLSAndPoolIsolation",
"TestHTTPConfiguredNetworkLimits/nethttp/headerLimit",
"TestHTTPConfiguredNetworkLimits/nethttp",
"TestHTTPConfiguredNetworkLimits/native/slowHeaders",
"TestHTTPConfiguredNetworkLimits/native/slowBody",
"TestHTTPConfiguredNetworkLimits/native/idle",
"TestHTTPConfiguredNetworkLimits/native/writeBackpressure",
"TestHTTPConfiguredNetworkLimits/native/headerLimit",
"TestHTTPConfiguredNetworkLimits/native",
"TestHTTPConfiguredNetworkLimits"
],
"sourceHashes": {
"database/connector_test.go": "0663dfc9794ba24130225fa3b6418a6d5f75d57d0f01686151f70c7ea3f592c0",
"database/mysqlConnector_test.go": "e1cdd11ef76b09f1332ba92bfdde5b1917d84c85cc42fac48f65a8b23f5e9720",
"app/httpConfig_test.go": "b060dc0b76fb8e5feebae7e3b12cb7bca845693fcc097e2a0496a5ba937c837a",
"config/http_test.go": "0cae3dcf213369cfaff5487bf630c2bc3ec097739ca1d40794124e69c3880db8",
"scripts/verifyConnector.py": "a0ba3fa29a4d1efcbed5dad82c09afb97d8ddecd904f20fee1e3aa74ec0f9e06"
}
}

View File

@@ -0,0 +1,42 @@
# v0.10.0-preview 统一错误目录验收
## 范围
项目定义错误目录,框架解析逻辑标识、HTTP 状态、对外业务码及公开提示;支持 HTTP 200 的业务失败,并贯通 `.api` 生成、响应、日志、指标、链路和错误重放。没有修改 eTax/ePay 业务代码,也没有使用业务服务器压测。
目录与响应模板分离;不同 App、路由可选择不同目录和协议。系统兜底不受 DefaultStatus 隐式影响,自定义 Encoder 继续控制最终输出。
## 本地回归
环境:go1.27.1 darwin/arm64。验收日期:2026-09-24。
- `GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./...` 全部通过,包含实际生成并编译的脚手架/契约消费者。
- 全量之后增加配置快照隔离与 Encoder 包装策略测试,完成 `go test -race -count=1 ./response ./config ./cmd/go-omni/apigen`。
- 本地/共享幂等错误元数据补充用例通过 `go test -race -count=1 ./app ./config ./httpx ./middleware/... ./metrics ./response ./telemetry`。
- `go vet ./...`、`go build ./...`、gofmt、`git diff --check` 与 CLI 校验 `examples/contracts/errors.api` 通过。
## 专项覆盖
- 多 App 隔离、目录复制、配置快照隔离、静态配置变更要求重启;并发解析及 WithCause 不互相污染。
- JSON/YAML 精确 int64、字符串/整数类型与范围、重复 key、非法状态/字段/映射冲突拒绝。重复对外业务码允许保留独立提示。
- HTTP 200 业务失败、403 单项覆盖、未知标识 500、内部原因不进入默认公开响应、已提交响应不追加、文本 ACK 由自定义 Encoder 输出。
- RawJSON 成功不包装、失败保持错误响应;嵌套相同/空目录保留已有策略,不同目录叠加明确拒绝。
- 访问日志记录最终状态和业务失败;指标计入 HTTP 200 失败,链路保留状态与失败标记。标准中间件、本地/共享幂等重放保留失败语义。
- 多文件导入、多个目录和不同码类型的路由作用域;200 成功/失败 OpenAPI anyOf、多错误同码的独立示例。
- 实际生成项目的无参数错误函数、全局中间件提前返回时的路由目录、unknown fallback、raw 成功/失败、手写文件重生成保护。
## 验证边界
共享幂等新增用例使用 JSON 序列化的内存替身,验证字段在持久化边界上保留;未为本特性运行外部 Redis 服务。没有进行 Linux soak、代理长测或完整业务迁移验收。业务项目仍需升级框架/CLI,并逐步将散落错误改为目录调用。
## 远程固定版本验收
发布后两条独立远程验收均通过;各自从全新 module cache 安装远程 CLI,生成的消费者依赖固定版本,没有本地 replace。
- 发布标签:`v0.10.0-preview`;源码:`ebfd279f9ada059bcb5d229e463759122b8a3669`。
- module 校验和:`h1:bbBDhMtp6ax+krphTGu9M1xnHGFORSg/ahFxWbEILyQ=`,两条验收结果一致。
- 错误目录远程消费者:实际生成代码编译、HTTP 200 失败、同码不同提示、状态覆盖、未知标识兜底、cause 隔离、raw 输出、中间件提前返回、重生成保护与 race 测试通过;耗时 14.15 秒,详见 [错误目录远程记录](errorCatalogRemote.json)。
- 迁移兼容性消费者:标量转换、int64 精度、动态 JSON、原始请求体验签、路由体积限制、上传临时文件清理、下载及重生成保护通过;耗时 14.48 秒,详见 [兼容性远程记录](errorCatalogCompatibilityRemote.json)。
- 验收 fixture 哈希与当前文件一致;临时目录及缓存已清理。
结果在 main 单独追加,已发布标签保持不可变。

View File

@@ -0,0 +1,31 @@
{
"passed": true,
"frameworkVersion": "v0.10.0-preview",
"frameworkSum": "h1:bbBDhMtp6ax+krphTGu9M1xnHGFORSg/ahFxWbEILyQ=",
"freshModuleCache": true,
"noLocalReplace": true,
"durationSeconds": 14.48,
"checks": [
"remoteCLIInstall",
"scalarContractValidation",
"manualProjectGeneration",
"int64MaxValueOnWire",
"dynamicJSON",
"rawBodySignature",
"routeBodyLimits",
"multipartTempCleanup",
"unwrappedDownloads",
"handwrittenFilesPreserved",
"generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"
],
"sourceHashes": {
"scripts/verifyCompatibilityRelease.py": "90e487d3203ed4eb4bc721da8086356413345b3e05193e473250a834aeb8d293",
"cmd/go-omni/apigen/compatibility_test.go": "98cf5fbe6cb6413c12f709806d385726f3dec1468575f76967786de4707667ba",
"examples/contracts/compatibility.api": "3ede909e7f60386012e55c916a8e4887dd08562e7f023665f663288c809873d4",
"cmd/go-omni/apigen/testdata/compatibility/consumer_test.go": "0a99ffd6a5cc16d47ae8208d9140c8d6afd9c19106fac8e5e61096cb90d8c463",
"cmd/go-omni/apigen/testdata/compatibility/hooks.go": "435baf37c7f8adffd18d4be0353a791124e0bc5dcb79abdcc345276b6f9867f4",
"cmd/go-omni/apigen/testdata/compatibility/echo.go": "9e13cb18a0951974e723a1fd915c0f4610d823c95eadb752c3d1cd75c44fa293",
"cmd/go-omni/apigen/testdata/compatibility/download.go": "e7f4bcdb34c90ec63fdab1f3c4e524964ac153944352d8a9053a9f19b74030fd"
}
}

View File

@@ -0,0 +1,29 @@
{
"passed": true,
"frameworkVersion": "v0.10.0-preview",
"frameworkSum": "h1:bbBDhMtp6ax+krphTGu9M1xnHGFORSg/ahFxWbEILyQ=",
"freshModuleCache": true,
"noLocalReplace": true,
"durationSeconds": 14.15,
"checks": [
"remoteCLIInstall",
"errorCatalogValidation",
"generatedErrorConstructors",
"http200BusinessFailure",
"duplicatePublicCodeDistinctMessages",
"perErrorHTTPStatus",
"unknownErrorFallback",
"causeNotExposed",
"rawSuccessAndWrappedFailure",
"earlyMiddlewareCatalog",
"handwrittenFilesPreserved",
"generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"
],
"sourceHashes": {
"scripts/verifyErrorCatalogRelease.py": "53b00d6069296903ba8392c68dfd6d9ae6d8c83e9b82555e55e8be0ec0f2b828",
"cmd/go-omni/apigen/errorCatalog_test.go": "9053ca2b103c32d6374ca013580b9f9f18d2319e15e445477a5c61795b32f2ae",
"examples/contracts/errors.api": "bfbe0e81c6d0f87981f511f21d4d3e138cdf9287c5f6615856ac04a825b14435",
"cmd/go-omni/apigen/testdata/errorCatalog/consumer_test.go": "804cee8cdbca5fe05e6340b8ac21e5bbb7d2a5c38fa6b06b121fc745aaea62ac"
}
}

View File

@@ -0,0 +1,27 @@
# v0.11.1-preview App 错误目录继承验收
## 问题与修复
在 Go 中用 `omni.WithErrors` 注册目录后,v0.11.0 生成路由无条件创建空目录 Encoder。Context 选择了该空目录,编码阶段又按空目录解析原始错误,造成已配置错误变成 500/英文系统提示。
修复让空 `WithErrors` 保持 Encoder 不变,生成器仅为非空路由目录绑定策略。Context 原有的 App fallback 因而能够生效;错误目录与响应格式仍独立选择。兼容旧生成代码中的空包装,非空路由目录保持独立、不与 App 合并。
## 复现与回归
- 修复前,`TestRouteInheritsAppErrorCatalog` 的普通路由通过;生成/RawJSON 路由在 Handler 与提前返回中间件两种入口、固定/模板/未知错误三种情况,共 12 项失败,均输出 500 和英文系统提示。
- 修复后该用例全部通过;包括 HTTP 400/数字 400002/中文提示/data:null,HTTP 200 模板提示,未知错误使用项目配置的中文 INTERNAL_ERROR。
- 独立路由用例验证相同错误标识使用路由自己的 409/业务码/提示,App 独有标识不渗入;路由的 detail 字段保持独立。
- EmptyCatalog 编码用例验证完全透传已解析结果,保留原始 error 时也不会二次查空表;非法空目录配置仍拒绝。
- 实际生成项目的 `.api` 没有任何 errorCatalog/error 定义。生成 Handler 调用手写 Service;普通路由、生成路由和全局中间件返回体完全一致,含模板参数、未知兜底和 data:null。
- 生成消费者还覆盖 RawJSON 成功不包装、@errors none 继承 App、手写原文回执、路由 body limit 及超限中文错误、手写 Service 的重生成保护。
## 本地验证
环境:go1.27.1 darwin/arm64;日期:2026-09-24。
- `GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./...` 全部通过。
- `go vet ./...`、`go build ./...`、gofmt、`git diff --check` 通过。
本轮仅本地测试及独立生成消费者,没有修改 eTax/ePay 业务代码,没有访问业务服务器;未重跑外部 Redis/MySQL、Linux soak 或完整业务迁移。
远程固定版本验收在发布后追加,标签保持不可变。

View File

@@ -0,0 +1,38 @@
# v0.11.0-preview 提示模板参数验收
## 范围与环境
通用错误目录增加模板参数及生成能力;字段名称、限制值、业务码、提示和 HTTP 状态均由项目提供。本轮没有修改 eTax/ePay 业务代码,没有访问业务服务器。
环境为 go1.27.1 darwin/arm64,验收日期 2026-09-24。网络消费者使用本地测试请求;Redis 使用独立本地临时容器,1 CPU、128 MiB、64 PID,只绑定回环地址,关闭持久化,完成后删除。
## 本地验收
- `GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -p=1 -count=1 -timeout=300s ./...` 全部通过,包含真实生成并编译的项目。
- `go vet ./...`、`go build ./...`、gofmt、`git diff --check` 通过。
- 模板专项覆盖:企业名称/100 与备注/500、缺参/多参/类型错误、整型范围/精度、并发隔离、修改原 map/目录副本、重复解析、WithCause 错误链、转义及非递归替换。
- 目录加载覆盖:重复/非法参数名、Go 关键字、不支持类型、未声明/未使用占位符、非法花括号、空 params、错误 example、同标识旧 messages 冲突。旧固定提示的字面花括号保持不变。
- JSON/YAML 配置保留 int64 示例与运行时参数精度;固定错误函数兼容。
- HTTP 测试覆盖:生成错误与运行时错误共用逻辑,HTTP 200、data:null、403 覆盖、500 兜底、cause 不进入公开响应、RawJSON 成功与失败策略;标准中间件、路由策略及 Context 缓冲克隆保留参数。
- 访问日志保持实际 HTTP 状态、业务码及失败标记;参数诊断不包含参数值。HTTP 200 失败计入指标。原有链路回归通过。
- 本地/共享幂等重放不重复执行业务,保存已解析公开快照;更新目录后重放仍保持原响应及错误信息。
- 最后补充生成消费者对 OpenAPI 参数类型、具体提示示例和 200 成功/失败 schema 的校验,再执行 `GO_OMNI_SCAFFOLD_INTEGRATION=1 go test -race -count=1 ./cmd/go-omni/apigen -run '^TestErrorParams'`。
## 真实 Redis 验收
`scripts/verifyErrorParamsRedis.py` 在独立容器中执行 `middleware/redis` 全部测试,3 项通过、0 跳过。确认模板重放、目录更新后快照不变、int64 最大值业务码通过 Lua/cjson 仍保持精度、原始参数和内部 cause 不进入持久化快照。原有共享存储并发/过期回归同时通过。
镜像、时间及资源限制见 [Redis 记录](errorParamsRedis.json)。临时容器已删除。未进行 Linux soak、代理长测或完整业务迁移验收。
## 远程固定版本验收
三条独立远程验收均通过,各自从全新 module cache 安装固定版本 CLI。生成消费者固定依赖远程版本,没有本地 replace。
- 发布标签:`v0.11.0-preview`;源码:`6e3f025aad4e498217ef4e7346725fc7d892940c`。
- module 校验和:`h1:arCeNRbzXsP3/Ocic2zTmm6Cr2nUWR1AcdzlOtoCBfo=`,三条验收一致。
- 提示模板消费者:参数顺序和类型、企业名称/100 与备注/500、生成函数/运行时一致、并发隔离、缺参/类型错误兜底、内部 cause 隔离、HTTP 200/data:null、403 覆盖、raw 输出、OpenAPI 具体示例和参数定义通过。耗时 16.91 秒,见 [模板远程记录](errorParamsRemote.json)。
- 旧固定错误目录消费者:无参数调用、同码不同提示、HTTP 状态、未知错误兜底、中间件提前返回和重生成保护通过。耗时 16.30 秒,见 [目录兼容记录](errorParamsCatalogRemote.json)。
- 迁移消费者:自定义标量、动态 JSON、上传、下载、原始请求体验签、请求体限制及重生成保护通过。耗时 16.61 秒,见 [迁移兼容记录](errorParamsCompatibilityRemote.json)。
- 三条消费者的 race 测试通过,fixture 哈希与当前文件一致;临时项目和 module cache 已清理。
验收记录在 main 单独追加,已发布标签保持不可变。

View File

@@ -0,0 +1,29 @@
{
"passed": true,
"frameworkVersion": "v0.11.0-preview",
"frameworkSum": "h1:arCeNRbzXsP3/Ocic2zTmm6Cr2nUWR1AcdzlOtoCBfo=",
"freshModuleCache": true,
"noLocalReplace": true,
"durationSeconds": 16.3,
"checks": [
"remoteCLIInstall",
"errorCatalogValidation",
"generatedErrorConstructors",
"http200BusinessFailure",
"duplicatePublicCodeDistinctMessages",
"perErrorHTTPStatus",
"unknownErrorFallback",
"causeNotExposed",
"rawSuccessAndWrappedFailure",
"earlyMiddlewareCatalog",
"handwrittenFilesPreserved",
"generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"
],
"sourceHashes": {
"scripts/verifyErrorCatalogRelease.py": "53b00d6069296903ba8392c68dfd6d9ae6d8c83e9b82555e55e8be0ec0f2b828",
"cmd/go-omni/apigen/errorCatalog_test.go": "0ee4365dc46dfb4ac4a3fafd2f2f0196a1e10db777ff9d931ea8f9fae4dda384",
"examples/contracts/errors.api": "bfbe0e81c6d0f87981f511f21d4d3e138cdf9287c5f6615856ac04a825b14435",
"cmd/go-omni/apigen/testdata/errorCatalog/consumer_test.go": "804cee8cdbca5fe05e6340b8ac21e5bbb7d2a5c38fa6b06b121fc745aaea62ac"
}
}

View File

@@ -0,0 +1,31 @@
{
"passed": true,
"frameworkVersion": "v0.11.0-preview",
"frameworkSum": "h1:arCeNRbzXsP3/Ocic2zTmm6Cr2nUWR1AcdzlOtoCBfo=",
"freshModuleCache": true,
"noLocalReplace": true,
"durationSeconds": 16.61,
"checks": [
"remoteCLIInstall",
"scalarContractValidation",
"manualProjectGeneration",
"int64MaxValueOnWire",
"dynamicJSON",
"rawBodySignature",
"routeBodyLimits",
"multipartTempCleanup",
"unwrappedDownloads",
"handwrittenFilesPreserved",
"generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"
],
"sourceHashes": {
"scripts/verifyCompatibilityRelease.py": "90e487d3203ed4eb4bc721da8086356413345b3e05193e473250a834aeb8d293",
"cmd/go-omni/apigen/compatibility_test.go": "98cf5fbe6cb6413c12f709806d385726f3dec1468575f76967786de4707667ba",
"examples/contracts/compatibility.api": "3ede909e7f60386012e55c916a8e4887dd08562e7f023665f663288c809873d4",
"cmd/go-omni/apigen/testdata/compatibility/consumer_test.go": "0a99ffd6a5cc16d47ae8208d9140c8d6afd9c19106fac8e5e61096cb90d8c463",
"cmd/go-omni/apigen/testdata/compatibility/hooks.go": "435baf37c7f8adffd18d4be0353a791124e0bc5dcb79abdcc345276b6f9867f4",
"cmd/go-omni/apigen/testdata/compatibility/echo.go": "9e13cb18a0951974e723a1fd915c0f4610d823c95eadb752c3d1cd75c44fa293",
"cmd/go-omni/apigen/testdata/compatibility/download.go": "e7f4bcdb34c90ec63fdab1f3c4e524964ac153944352d8a9053a9f19b74030fd"
}
}

View File

@@ -0,0 +1,25 @@
{
"passed": true,
"image": "sha256:f5c58b2e6e6cba5589931e8822c0ced59c507e96603d370bae3913e143f57546",
"timestamp": "2026-09-24T07:00:14.532535+00:00",
"goVersion": "go version go1.27.1 darwin/arm64",
"durationSeconds": 2.35,
"passedTests": [
"TestTemplateErrorRedisReplay",
"TestSharedRedisIntegration",
"TestExpiredTargetBeyondCleanupBatch"
],
"skippedTests": [],
"checks": [
"templateReplay",
"int64CodePrecisionThroughLua",
"catalogChangeKeepsSnapshot",
"noPersistedCauseOrParams",
"http200Failure",
"dataNull",
"oneBusinessExecution"
],
"cpuLimit": 1,
"memoryMiB": 128,
"loopbackOnly": true
}

View File

@@ -0,0 +1,35 @@
{
"passed": true,
"frameworkVersion": "v0.11.0-preview",
"frameworkSum": "h1:arCeNRbzXsP3/Ocic2zTmm6Cr2nUWR1AcdzlOtoCBfo=",
"freshModuleCache": true,
"noLocalReplace": true,
"durationSeconds": 16.91,
"checks": [
"remoteCLIInstall",
"errorCatalogValidation",
"generatedErrorConstructors",
"http200BusinessFailure",
"openAPIConcreteExampleAndParameters",
"typedParameterOrder",
"companyAndRemarkMessages",
"runtimeAndGeneratedAgreement",
"missingAndWrongParameterFallback",
"concurrentIsolation",
"fixedErrorCompatibility",
"perErrorHTTPStatus",
"causeNotExposed",
"rawSuccessAndWrappedFailure",
"earlyMiddlewareCatalog",
"handwrittenFilesPreserved",
"generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"
],
"sourceHashes": {
"scripts/verifyErrorParamsRelease.py": "4e9ed06a1e86a701da4eaed813ef5a2456ccfb6e8bdf88f657cb7abfede9d3c3",
"cmd/go-omni/apigen/errorParams_test.go": "0e6f79d3dec842c07cdac9d1e247f5593f189a0984ed25be7b0f9b6da6cba47d",
"cmd/go-omni/apigen/errorCatalog_test.go": "0ee4365dc46dfb4ac4a3fafd2f2f0196a1e10db777ff9d931ea8f9fae4dda384",
"examples/contracts/errorParams.api": "d6d09d60eb8079283e6622983e62a8a8c2ad2e070843e99bae6f8c8da491a44c",
"cmd/go-omni/apigen/testdata/errorParams/consumer_test.go": "01d0826cbf85126ad3c6d25e45d881655385931c6f97f242818f4d49c3416f4a"
}
}

101
response/catalog_test.go Normal file
View File

@@ -0,0 +1,101 @@
package response_test
import (
"encoding/json"
"strings"
"testing"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/response"
)
func TestCatalogHTTP200AndRawJSON(t *testing.T) {
c := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"INVALID": {Code: 400002, Message: "invalid"}}}
cfg := response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, ErrorMessage: "fallback", Body: map[string]any{"ok": "${ok}", "code": "${code}", "data": "${data}"}, ErrorBody: map[string]any{"failed": "${ok}", "code": "${code}", "msg": "${message}"}}
enc, err := response.WithErrors(c, response.MustNew(cfg))
if err != nil {
t.Fatal(err)
}
for _, encoder := range []response.Encoder{enc, response.RawJSON(enc)} {
p, err := encoder.Encode(response.Result{Status: 500, Err: apierrors.ByCode("INVALID")})
if err != nil {
t.Fatal(err)
}
var body map[string]any
if err = json.Unmarshal(p.Body, &body); err != nil {
t.Fatal(err)
}
if p.Status != 200 || body["failed"] != false || body["code"] != float64(400002) || body["msg"] != "invalid" || body["data"] != nil {
t.Fatal(string(p.Body))
}
}
p, err := response.RawJSON(enc).Encode(response.Result{Status: 200, Data: map[string]bool{"value": true}})
if err != nil || strings.Contains(string(p.Body), "code") {
t.Fatal(string(p.Body), err)
}
for _, change := range []response.Config{
{CodeType: "string"},
{CodeType: "int32", SuccessCode: 0, ErrorCode: 9, Codes: map[string]any{"INVALID": 1}},
{CodeType: "int32", SuccessCode: 0, ErrorCode: 9, Messages: map[string]string{"INVALID": "different"}},
} {
if _, err := response.WithErrors(c, response.MustNew(change)); err == nil {
t.Fatal("accepted conflicting catalog mapping")
}
}
enc, err = response.WithErrors(c, response.EncoderFunc(func(r response.Result) (response.Payload, error) {
if !r.IsError() || r.Status != 200 || r.BusinessCode != int64(400002) {
t.Fatal(r)
}
return response.Text(202, "ACK"), nil
}))
if err != nil {
t.Fatal(err)
}
p, err = enc.Encode(response.Result{Err: apierrors.ByCode("INVALID")})
if err != nil || p.Status != 202 || string(p.Body) != "ACK" {
t.Fatal(p, err)
}
}
func TestCatalogWrapperPreservesScope(t *testing.T) {
catalog := apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{
"invalid": {Code: "invalid", Message: "project message"},
}}
inner := response.MustWithErrors(catalog, response.Default())
for _, encoder := range []response.Encoder{inner, response.RawJSON(inner)} {
for _, outer := range []apierrors.Catalog{{}, catalog} {
bound, err := response.WithErrors(outer, encoder)
if err != nil {
t.Fatal(err)
}
payload, err := bound.Encode(response.Result{Err: apierrors.ByCode("invalid")})
if err != nil || payload.Status != 200 || !strings.Contains(string(payload.Body), "project message") {
t.Fatal(payload, err)
}
}
other := catalog.Clone()
other.Definitions["invalid"] = apierrors.Definition{Status: 409, Code: "other", Message: "other"}
if _, err := response.WithErrors(other, encoder); err == nil {
t.Fatal("stacked conflicting policies")
}
}
}
func TestEmptyCatalogIsTransparent(t *testing.T) {
formatter := response.MustNew(response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999, MessageField: "msg", DataMode: "always"})
for _, inner := range []response.Encoder{formatter, response.RawJSON(formatter)} {
wrapped, err := response.WithErrors(apierrors.Catalog{}, inner)
if err != nil || wrapped != inner {
t.Fatal("empty catalog introduced a route policy", err)
}
// Context resolved this through the App. Original Err must not cause a
// second lookup to replace its status, public code or rendered message.
payload, err := wrapped.Encode(response.Result{Status: 400, Code: "INVALID", BusinessCode: int64(400002), Message: "项目提示", Failed: true, Err: apierrors.ByCode("INVALID")})
if err != nil || payload.Status != 400 || !strings.Contains(string(payload.Body), "项目提示") {
t.Fatal(payload, err)
}
}
if _, err := response.WithErrors(apierrors.Catalog{DefaultStatus: 204}, formatter); err == nil {
t.Fatal("invalid empty catalog bypassed validation")
}
}

View File

@@ -162,7 +162,7 @@ func (f *Formatter) Config() Config {
func (f *Formatter) Value(r Result) map[string]any { func (f *Formatter) Value(r Result) map[string]any {
c := f.config c := f.config
code, message := any(r.Code), r.Message code, message := any(r.Code), r.Message
isError := r.Err != nil || r.Status >= 400 isError := r.IsError()
if isError { if isError {
if c.ErrorCode != nil { if c.ErrorCode != nil {
code = c.ErrorCode code = c.ErrorCode
@@ -184,6 +184,9 @@ func (f *Formatter) Value(r Result) map[string]any {
if mapped, ok := c.Messages[r.Code]; ok { if mapped, ok := c.Messages[r.Code]; ok {
message = mapped message = mapped
} }
if isError && r.BusinessCode != nil {
code, message = r.BusinessCode, r.Message
}
body := c.Body body := c.Body
if isError && c.ErrorBody != nil { if isError && c.ErrorBody != nil {
body = c.ErrorBody body = c.ErrorBody
@@ -206,7 +209,12 @@ func (f *Formatter) Value(r Result) map[string]any {
} }
return value return value
} }
func (f *Formatter) Encode(r Result) (Payload, error) { return JSON(r.Status, f.Value(r)) } func (f *Formatter) Encode(r Result) (Payload, error) {
if err := f.validateError(r); err != nil {
return Payload{}, err
}
return JSON(r.Status, f.Value(r))
}
func normalizeCode(value any, kind string) (any, error) { func normalizeCode(value any, kind string) (any, error) {
if kind == "string" { if kind == "string" {

View File

@@ -3,6 +3,8 @@ package response
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
) )
// Result is the transport-independent input to an Encoder. Err is available for // Result is the transport-independent input to an Encoder. Err is available for
@@ -12,8 +14,13 @@ type Result struct {
Code, Message, RequestID string Code, Message, RequestID string
Data any Data any
Err error Err error
BusinessCode any
Failed bool
} }
// IsError preserves failure semantics independently of the HTTP status.
func (r Result) IsError() bool { return r.Failed || r.Err != nil || r.Status >= 400 }
// Payload is a complete response. Encoders must be safe for concurrent use. // Payload is a complete response. Encoders must be safe for concurrent use.
// Status and headers are intentionally part of the contract: callback protocols // Status and headers are intentionally part of the contract: callback protocols
// may acknowledge an error using a different HTTP status or media type. // may acknowledge an error using a different HTTP status or media type.
@@ -47,14 +54,30 @@ func Default() Encoder { return defaultEncoder }
// RawJSON leaves successful data unwrapped. Errors use the selected error // RawJSON leaves successful data unwrapped. Errors use the selected error
// policy; callback protocols with custom acknowledgments should use EncoderFunc. // policy; callback protocols with custom acknowledgments should use EncoderFunc.
type rawJSONEncoder struct{ errors Encoder }
func (e *rawJSONEncoder) ErrorRegistry() *apierrors.Registry {
if p, ok := e.errors.(ErrorRegistryProvider); ok {
return p.ErrorRegistry()
}
return nil
}
func (e *rawJSONEncoder) Encode(r Result) (Payload, error) {
if r.IsError() {
return e.errors.Encode(r)
}
return JSON(r.Status, r.Data)
}
func RawJSON(errors Encoder) Encoder { func RawJSON(errors Encoder) Encoder {
if errors == nil { if errors == nil {
errors = Default() errors = Default()
} }
return EncoderFunc(func(r Result) (Payload, error) { return &rawJSONEncoder{errors: errors}
if r.Err != nil || r.Status >= 400 { }
return errors.Encode(r)
} func (e *rawJSONEncoder) ValidateErrors(catalog apierrors.Catalog) error {
return JSON(r.Status, r.Data) if checker, ok := e.errors.(interface{ ValidateErrors(apierrors.Catalog) error }); ok {
}) return checker.ValidateErrors(catalog)
}
return nil
} }

89
response/errors.go Normal file
View File

@@ -0,0 +1,89 @@
package response
import (
"fmt"
"reflect"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
)
// ErrorRegistryProvider attaches an immutable error catalog to a route policy.
// Contexts and buffered clones use it before encoding or running middleware.
type ErrorRegistryProvider interface{ ErrorRegistry() *apierrors.Registry }
type errorEncoder struct {
next Encoder
registry *apierrors.Registry
}
func (e *errorEncoder) ErrorRegistry() *apierrors.Registry { return e.registry }
func (e *errorEncoder) Encode(r Result) (Payload, error) {
if r.Err != nil {
err := e.registry.Resolve(r.Err)
r.Status, r.Code, r.Message, r.BusinessCode, r.Failed = err.Status, err.Code, err.Message, err.BusinessCode, true
}
return e.next.Encode(r)
}
// WithErrors binds a catalog to an encoder without imposing a response shape.
// Built-in templates are checked for incompatible code types and conflicting
// legacy mappings. An empty catalog leaves next unchanged, so Context can
// inherit its App catalog without a second, empty lookup during encoding.
// Custom encoders remain responsible for their wire contract.
func WithErrors(catalog apierrors.Catalog, next Encoder) (Encoder, error) {
registry, err := apierrors.NewRegistry(catalog)
if err != nil {
return nil, err
}
if next == nil {
next = Default()
}
if len(catalog.Definitions) == 0 {
return next, nil
}
if provider, ok := next.(ErrorRegistryProvider); ok && provider.ErrorRegistry() != nil {
if reflect.DeepEqual(registry.Catalog(), provider.ErrorRegistry().Catalog()) {
return next, nil
}
return nil, fmt.Errorf("response: cannot stack different error catalogs; choose one policy per scope")
}
if checker, ok := next.(interface{ ValidateErrors(apierrors.Catalog) error }); ok {
if err := checker.ValidateErrors(registry.Catalog()); err != nil {
return nil, err
}
}
return &errorEncoder{next: next, registry: registry}, nil
}
func MustWithErrors(catalog apierrors.Catalog, next Encoder) Encoder {
e, err := WithErrors(catalog, next)
if err != nil {
panic(err)
}
return e
}
func (f *Formatter) ValidateErrors(catalog apierrors.Catalog) error {
for key, d := range catalog.Definitions {
if _, ok := f.config.Messages[key]; ok && len(d.Params) > 0 {
return fmt.Errorf("response: templated error %s must define its message only in the catalog", key)
}
if err := f.validateError(Result{Code: key, Message: d.Message, BusinessCode: d.Code, Failed: true}); err != nil {
return err
}
}
return nil
}
func (f *Formatter) validateError(r Result) error {
if !r.IsError() || r.BusinessCode == nil {
return nil
}
normalized, err := normalizeCode(r.BusinessCode, f.config.CodeType)
if err != nil {
return fmt.Errorf("response: catalog %s code does not match codeType %s: %w", r.Code, f.config.CodeType, err)
}
if code, ok := f.config.Codes[r.Code]; ok && !reflect.DeepEqual(code, normalized) {
return fmt.Errorf("response: conflicting catalog/code mapping for %s", r.Code)
}
if msg, ok := f.config.Messages[r.Code]; ok && msg != r.Message {
return fmt.Errorf("response: conflicting catalog/message mapping for %s", r.Code)
}
return nil
}

View File

@@ -0,0 +1,78 @@
#!/usr/bin/env python3
"""Install the released CLI in a fresh cache, then test its generated consumer.
The fixture tests project-owned error catalogs, HTTP 200 business failures,
route policies, generated constructors and regeneration protection. Only local temporary resources
are used; the framework and CLI are fetched from the specified remote version.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
import time
ROOT = Path(__file__).resolve().parents[1]
MODULE = "git.yuntuo.hk/k2safe/go-omni"
def run(args, cwd, env, timeout=300):
result = subprocess.run([str(arg) for arg in args], cwd=cwd, env=env,
capture_output=True, text=True, timeout=timeout)
output = re.sub(r"https://[^/@\s]+:[^/@\s]+@", "https://", result.stdout + result.stderr)
if result.returncode:
raise RuntimeError(f"{args[0]} failed ({result.returncode}):\n{output}")
return output
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--version", required=True)
parser.add_argument("--report", type=Path, default=ROOT / "performance/errorCatalogRemote.json")
args = parser.parse_args()
if not re.fullmatch(r"v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?", args.version):
parser.error("a fixed release version is required")
env = {k: v for k, v in os.environ.items() if not k.startswith("GO_OMNI_")}
env.update(GOWORK="off", GOFLAGS="", GOPRIVATE="git.yuntuo.hk", GONOSUMDB="git.yuntuo.hk")
start = time.monotonic()
with tempfile.TemporaryDirectory(prefix="goOmniErrorCatalogRelease-") as directory:
temp = Path(directory)
(temp / "bin").mkdir()
env.update(GOBIN=str(temp / "bin"), GOMODCACHE=str(temp / "modcache"))
print(f"Installing CLI {args.version} in an empty module cache", flush=True)
run(["go", "install", MODULE + "/cmd/go-omni@" + args.version], temp, env)
cli = temp / "bin/go-omni"
run([cli, "api", "validate", "--api", ROOT / "examples/contracts/errors.api"], temp, env)
env.update(GO_OMNI_SCAFFOLD_INTEGRATION="1", GO_OMNI_RELEASE_CLI=str(cli), GO_OMNI_RELEASE_VERSION=args.version)
print("Testing remote error catalog generation, HTTP 200 failures and raw responses", flush=True)
output = run(["go", "test", "-race", "-count=1", "-json", "-timeout=180s",
"./cmd/go-omni/apigen", "-run", "^TestErrorCatalogConsumer$"], ROOT, env)
events = [json.loads(line) for line in output.splitlines() if line.startswith("{")]
passed = [e["Test"] for e in events if e.get("Action") == "pass" and "Test" in e]
assert passed == ["TestErrorCatalogConsumer"], output
evidence = "".join(e.get("Output", "") for e in events)
checksum = re.search(r"checksum (h1:\S+); no local replace", evidence)
assert checksum, "consumer did not verify its remote dependency"
report = {
"passed": True, "frameworkVersion": args.version, "frameworkSum": checksum.group(1),
"freshModuleCache": True, "noLocalReplace": True,
"durationSeconds": round(time.monotonic() - start, 2),
"checks": ["remoteCLIInstall", "errorCatalogValidation", "generatedErrorConstructors",
"http200BusinessFailure", "duplicatePublicCodeDistinctMessages", "perErrorHTTPStatus",
"unknownErrorFallback", "causeNotExposed", "rawSuccessAndWrappedFailure",
"earlyMiddlewareCatalog", "handwrittenFilesPreserved", "generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"],
"sourceHashes": {name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest() for name in
("scripts/verifyErrorCatalogRelease.py", "cmd/go-omni/apigen/errorCatalog_test.go",
"examples/contracts/errors.api",
"cmd/go-omni/apigen/testdata/errorCatalog/consumer_test.go")},
}
args.report.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
print(f"PASS remote error catalog contract {args.version}; temporary files removed", flush=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""Install the released CLI in a fresh cache, then test its generated consumer.
The fixture tests Go-owned App catalogs on generated routes, early middleware, raw responses,
body limits, independent route policies and regeneration protection. Only local temporary resources
are used; the framework and CLI are fetched from the specified remote version.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
import time
ROOT = Path(__file__).resolve().parents[1]
MODULE = "git.yuntuo.hk/k2safe/go-omni"
def run(args, cwd, env, timeout=300):
result = subprocess.run([str(arg) for arg in args], cwd=cwd, env=env,
capture_output=True, text=True, timeout=timeout)
output = re.sub(r"https://[^/@\s]+:[^/@\s]+@", "https://", result.stdout + result.stderr)
if result.returncode:
raise RuntimeError(f"{args[0]} failed ({result.returncode}):\n{output}")
return output
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--version", required=True)
parser.add_argument("--report", type=Path, default=ROOT / "performance/errorInheritanceRemote.json")
args = parser.parse_args()
if not re.fullmatch(r"v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?", args.version):
parser.error("a fixed release version is required")
env = {k: v for k, v in os.environ.items() if not k.startswith("GO_OMNI_")}
env.update(GOWORK="off", GOFLAGS="", GOPRIVATE="git.yuntuo.hk", GONOSUMDB="git.yuntuo.hk")
start = time.monotonic()
with tempfile.TemporaryDirectory(prefix="goOmniErrorInheritanceRelease-") as directory:
temp = Path(directory)
(temp / "bin").mkdir()
env.update(GOBIN=str(temp / "bin"), GOMODCACHE=str(temp / "modcache"))
print(f"Installing CLI {args.version} in an empty module cache", flush=True)
run(["go", "install", MODULE + "/cmd/go-omni@" + args.version], temp, env)
cli = temp / "bin/go-omni"
run([cli, "api", "validate", "--api", ROOT / "examples/contracts/errorInheritance.api"], temp, env)
env.update(GO_OMNI_SCAFFOLD_INTEGRATION="1", GO_OMNI_RELEASE_CLI=str(cli), GO_OMNI_RELEASE_VERSION=args.version)
print("Testing remote App catalog inheritance on generated handlers and early middleware", flush=True)
output = run(["go", "test", "-race", "-count=1", "-json", "-timeout=180s",
"./cmd/go-omni/apigen", "-run", "^TestErrorInheritanceConsumer$"], ROOT, env)
events = [json.loads(line) for line in output.splitlines() if line.startswith("{")]
passed = [e["Test"] for e in events if e.get("Action") == "pass" and "Test" in e]
assert passed == ["TestErrorInheritanceConsumer"], output
evidence = "".join(e.get("Output", "") for e in events)
checksum = re.search(r"checksum (h1:\S+); no local replace", evidence)
assert checksum, "consumer did not verify its remote dependency"
report = {
"passed": True, "frameworkVersion": args.version, "frameworkSum": checksum.group(1),
"freshModuleCache": True, "noLocalReplace": True,
"durationSeconds": round(time.monotonic() - start, 2),
"checks": ["remoteCLIInstall", "noAPICatalogRequired", "ordinaryAndGeneratedResponseEquality",
"typedMessageParameters", "configuredUnknownFallback", "independentRouteCatalog",
"rawSuccess", "dataNull", "earlyMiddlewareInheritance", "routeBodyLimit",
"handwrittenServicesPreserved", "generatedConsumerRaceTests", "fixedRemoteModuleChecksum"],
"sourceHashes": {name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest() for name in
("scripts/verifyErrorInheritanceRelease.py", "cmd/go-omni/apigen/errorInheritance_test.go", "cmd/go-omni/apigen/errorCatalog_test.go",
"examples/contracts/errorInheritance.api",
"cmd/go-omni/apigen/testdata/errorInheritance/consumer_test.go",
"cmd/go-omni/apigen/testdata/errorInheritance/service.go",
"cmd/go-omni/apigen/testdata/errorInheritance/emptyService.go",
"cmd/go-omni/apigen/testdata/errorInheritance/bounded.go")},
}
args.report.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
print(f"PASS remote App error catalog inheritance {args.version}; temporary files removed", flush=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,59 @@
#!/usr/bin/env python3
"""Test template replay against an isolated, bounded local Redis container."""
import argparse
import datetime
import json
import os
from pathlib import Path
import subprocess
import time
ROOT = Path(__file__).resolve().parents[1]
def run(args, **kwargs):
return subprocess.run(args, check=True, capture_output=True, text=True, **kwargs).stdout.strip()
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--report", type=Path, default=ROOT / "performance/errorParamsRedis.json")
args = parser.parse_args()
started = time.monotonic()
container = run(["docker", "run", "--pull=never", "--rm", "-d", "--cpus", "1", "--memory", "128m",
"--pids-limit", "64", "-p", "127.0.0.1::6379", "redis:7-alpine",
"redis-server", "--save", "", "--appendonly", "no", "--maxmemory", "64mb",
"--maxmemory-policy", "noeviction"])
print("Started isolated Redis (1 CPU, 128 MiB); loopback only", flush=True)
try:
image = run(["docker", "inspect", "--format", "{{.Image}}", container])
address = run(["docker", "port", container, "6379/tcp"])
for _ in range(30):
try:
if run(["docker", "exec", container, "redis-cli", "ping"]) == "PONG":
break
except subprocess.CalledProcessError:
pass
time.sleep(0.1)
else:
raise RuntimeError("Redis did not become ready")
env = dict(os.environ, GO_OMNI_REDIS_TEST_ADDR=address)
output = run(["go", "test", "-race", "-count=1", "-json", "-timeout=120s",
"./middleware/redis"], cwd=ROOT, env=env, timeout=150)
events = [json.loads(line) for line in output.splitlines() if line.startswith("{")]
passed = [e["Test"] for e in events if e.get("Action") == "pass" and "Test" in e]
skipped = [e["Test"] for e in events if e.get("Action") == "skip" and "Test" in e]
assert "TestTemplateErrorRedisReplay" in passed and not skipped, (passed, skipped)
args.report.write_text(json.dumps({
"passed": True, "image": image, "timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"goVersion": run(["go", "version"]), "durationSeconds": round(time.monotonic() - started, 2),
"passedTests": passed, "skippedTests": skipped,
"checks": ["templateReplay", "int64CodePrecisionThroughLua", "catalogChangeKeepsSnapshot",
"noPersistedCauseOrParams", "http200Failure", "dataNull", "oneBusinessExecution"],
"cpuLimit": 1, "memoryMiB": 128, "loopbackOnly": True,
}, ensure_ascii=False, indent=2) + "\n")
print("PASS isolated Redis template replay and shared store regressions", flush=True)
finally:
run(["docker", "rm", "-f", container])
print("Temporary Redis container removed", flush=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""Install the released CLI in a fresh cache, then test its generated consumer.
The fixture tests typed message templates, runtime parameters, HTTP 200 business failures,
OpenAPI examples, fixed errors and regeneration protection. Only local temporary resources
are used; the framework and CLI are fetched from the specified remote version.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
import time
ROOT = Path(__file__).resolve().parents[1]
MODULE = "git.yuntuo.hk/k2safe/go-omni"
def run(args, cwd, env, timeout=300):
result = subprocess.run([str(arg) for arg in args], cwd=cwd, env=env,
capture_output=True, text=True, timeout=timeout)
output = re.sub(r"https://[^/@\s]+:[^/@\s]+@", "https://", result.stdout + result.stderr)
if result.returncode:
raise RuntimeError(f"{args[0]} failed ({result.returncode}):\n{output}")
return output
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--version", required=True)
parser.add_argument("--report", type=Path, default=ROOT / "performance/errorParamsRemote.json")
args = parser.parse_args()
if not re.fullmatch(r"v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?", args.version):
parser.error("a fixed release version is required")
env = {k: v for k, v in os.environ.items() if not k.startswith("GO_OMNI_")}
env.update(GOWORK="off", GOFLAGS="", GOPRIVATE="git.yuntuo.hk", GONOSUMDB="git.yuntuo.hk")
start = time.monotonic()
with tempfile.TemporaryDirectory(prefix="goOmniErrorParamsRelease-") as directory:
temp = Path(directory)
(temp / "bin").mkdir()
env.update(GOBIN=str(temp / "bin"), GOMODCACHE=str(temp / "modcache"))
print(f"Installing CLI {args.version} in an empty module cache", flush=True)
run(["go", "install", MODULE + "/cmd/go-omni@" + args.version], temp, env)
cli = temp / "bin/go-omni"
run([cli, "api", "validate", "--api", ROOT / "examples/contracts/errorParams.api"], temp, env)
env.update(GO_OMNI_SCAFFOLD_INTEGRATION="1", GO_OMNI_RELEASE_CLI=str(cli), GO_OMNI_RELEASE_VERSION=args.version)
print("Testing remote typed error generation, parameter failures and raw responses", flush=True)
output = run(["go", "test", "-race", "-count=1", "-json", "-timeout=180s",
"./cmd/go-omni/apigen", "-run", "^TestErrorParamsConsumer$"], ROOT, env)
events = [json.loads(line) for line in output.splitlines() if line.startswith("{")]
passed = [e["Test"] for e in events if e.get("Action") == "pass" and "Test" in e]
assert passed == ["TestErrorParamsConsumer"], output
evidence = "".join(e.get("Output", "") for e in events)
checksum = re.search(r"checksum (h1:\S+); no local replace", evidence)
assert checksum, "consumer did not verify its remote dependency"
report = {
"passed": True, "frameworkVersion": args.version, "frameworkSum": checksum.group(1),
"freshModuleCache": True, "noLocalReplace": True,
"durationSeconds": round(time.monotonic() - start, 2),
"checks": ["remoteCLIInstall", "errorCatalogValidation", "generatedErrorConstructors",
"http200BusinessFailure", "openAPIConcreteExampleAndParameters", "typedParameterOrder",
"companyAndRemarkMessages", "runtimeAndGeneratedAgreement", "missingAndWrongParameterFallback",
"concurrentIsolation", "fixedErrorCompatibility", "perErrorHTTPStatus",
"causeNotExposed", "rawSuccessAndWrappedFailure",
"earlyMiddlewareCatalog", "handwrittenFilesPreserved", "generatedConsumerRaceTests",
"fixedRemoteModuleChecksum"],
"sourceHashes": {name: hashlib.sha256((ROOT / name).read_bytes()).hexdigest() for name in
("scripts/verifyErrorParamsRelease.py", "cmd/go-omni/apigen/errorParams_test.go", "cmd/go-omni/apigen/errorCatalog_test.go",
"examples/contracts/errorParams.api",
"cmd/go-omni/apigen/testdata/errorParams/consumer_test.go")},
}
args.report.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
print(f"PASS remote error template contract {args.version}; temporary files removed", flush=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,51 @@
package telemetry_test
import (
"context"
"net/http/httptest"
"testing"
omni "git.yuntuo.hk/k2safe/go-omni"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/response"
"git.yuntuo.hk/k2safe/go-omni/telemetry"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func TestCatalogFailureTraceWithHTTP200(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
runtime, err := telemetry.New(telemetry.Config{Service: "fixture", SampleRatio: 1, Exporter: exporter})
if err != nil {
t.Fatal(err)
}
defer runtime.Close()
app := omni.New(omni.WithErrors(apierrors.Catalog{DefaultStatus: 200, Definitions: map[string]apierrors.Definition{"INVALID": {Code: 123, Message: "invalid"}}}), omni.WithResponse(response.Config{CodeType: "int32", SuccessCode: 0, ErrorCode: 9999}))
defer app.Close()
app.Use(runtime.Middleware())
app.GET("/", func(c *omni.Context) error { c.WriteError(apierrors.ByCode("INVALID")); return nil })
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", "/", nil))
if w.Code != 200 {
t.Fatal(w.Code)
}
if err := runtime.Flush(context.Background()); err != nil {
t.Fatal(err)
}
spans := exporter.GetSpans()
if len(spans) != 1 || spans[0].Status.Code != codes.Error {
t.Fatal(spans)
}
seenStatus, seenFailure := false, false
for _, attr := range spans[0].Attributes {
if attr.Key == "http.response.status_code" && attr.Value.AsInt64() == 200 {
seenStatus = true
}
if attr.Key == "omni.request.failed" && attr.Value.AsBool() {
seenFailure = true
}
}
if !seenStatus || !seenFailure {
t.Fatal(spans[0].Attributes)
}
}

View File

@@ -4,7 +4,10 @@ package telemetry
import ( import (
"context" "context"
"errors" "errors"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors" "math"
"net/http"
"time"
"git.yuntuo.hk/k2safe/go-omni/httpx" "git.yuntuo.hk/k2safe/go-omni/httpx"
"git.yuntuo.hk/k2safe/go-omni/logx" "git.yuntuo.hk/k2safe/go-omni/logx"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
@@ -15,9 +18,6 @@ import (
"go.opentelemetry.io/otel/sdk/resource" "go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
"math"
"net/http"
"time"
) )
type Config struct { type Config struct {
@@ -99,15 +99,22 @@ func (r *Runtime) Middleware() httpx.Middleware {
defer func() { defer func() {
status := c.Status() status := c.Status()
if err != nil && !c.Responded() { if err != nil && !c.Responded() {
status = apierrors.From(err).Status status = c.ResolveError(err).Status
} }
p := recover() p := recover()
if p != nil { if p != nil {
status = 500 status = 500
} }
attrs := []attribute.KeyValue{attribute.String("http.request.method", method), attribute.String("http.route", route), attribute.Int("http.response.status_code", status)} attrs := []attribute.KeyValue{attribute.String("http.request.method", method), attribute.String("http.route", route), attribute.Int("http.response.status_code", status)}
failure := c.Failure()
if failure == nil && err != nil {
failure = c.ResolveError(err)
}
if failure != nil {
attrs = append(attrs, attribute.Bool("omni.request.failed", true), attribute.String("omni.error.key", failure.Code))
}
span.SetAttributes(attrs...) span.SetAttributes(attrs...)
if status >= 500 { if status >= 500 || (failure != nil && status < 400) {
span.SetStatus(codes.Error, "request failed") span.SetStatus(codes.Error, "request failed")
} }
r.requests.Add(ctx, 1, metric.WithAttributes(attrs...)) r.requests.Add(ctx, 1, metric.WithAttributes(attrs...))