Skip to content

ESPL-21: 404 on missing resources, 409 on conflict, real monitoring metrics - #8

Closed
TyperBody wants to merge 1 commit into
feat/project-review-and-pr-qafrom
agent/sxy/e371b29c7b9a
Closed

TyperBody wants to merge 1 commit into
feat/project-review-and-pr-qafrom
agent/sxy/e371b29c7b9a

Conversation

@TyperBody

@TyperBody TyperBody commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Closes ESPL-21

Base: feat/project-review-and-pr-qa(PR #3)。不改 M5。

清 M10 / M11 / M12 / M13:假成功、错误码语义、恒为 0 的监控指标,以及钉住这些行为的状态码测试。

改前 → 改后(本机起真实服务实测,非 mock)

改前跑基线提交 a0d5148 的二进制,改后跑本分支,同一套 curl:

请求 改前 改后
DELETE /api/v1/routes/nope 200 {"message":"route deleted"} 404 {"error":"resource not found"}
DELETE /api/v1/platforms/nope 200 404
DELETE /api/v1/instances/nope 200 404
DELETE /api/v1/adapters/nope 200 404
POST /api/v1/messages/nope/ack 200 {"message":"message acknowledged"} 404
PUT /api/v1/instances/nope 400 {"error":"record not found"} 404 {"error":"resource not found"}
PUT /api/v1/routes/nope 400 {"error":"record not found"} 404
PUT /api/v1/platforms/nope 400 {"error":"record not found"} 404
POST /api/v1/adapters/nope/start 400 {"error":"record not found"} 404
POST /api/v1/platforms(重复 code) 400 platform code already exists 409
POST /api/v1/auth/register(重复用户) 400 username already exists 409
内部错误(表被删)走 GET 404(被误判) 500
GET /api/v1/instances 的监控字段 字段根本不存在 last_heartbeat / message_count / error_count 有真实值

监控页实测:连上 /ws/adapter?instance_id=… 发 3 条消息 + 1 帧 error 后

{'status': 'running', 'last_heartbeat': '2026-11-07T18:09:15.654825009+08:00',
 'message_count': 3, 'error_count': 1}

改前同样的库/动作下三个指标恒为 0、status 恒为 stopped。

改动要点

  • M11:repository 的 Delete / UpdateStatus 判 RowsAffected,为 0 返回 gorm.ErrRecordNotFound(10 个 repository 方法,含 route/platform/instance/adapter/message/user)。
  • M12:internal/service/errors.go 新增 ErrNotFound / ErrConflict / ErrInvalid / ErrUnauthorized 哨兵;internal/handler/errors.go 的 respondError 单点映射 404/409/401/400,其余 500。所有 handler 的 err.Error() + 写死状态码改成走它,record not found 不再作为 400 文案回吐。重名(platform code、username)走 409;已在运行再 start 走 409。
  • M10:models.AdapterInstance 补 last_heartbeat / message_count / error_count;迁移 migrations/003_instance_metrics.sql(IF NOT EXISTS + 回滚注释)与 AutoMigrate 双重覆盖,migrations/README.md 补索引。WebSocket 热路径维护三个值:连上/任意帧刷新心跳并置 running,入站消息 +1,桥报错/落库失败 +1;error 帧不再当消息入库,两类计数不重叠。
  • M13:internal/handler/errors_test.go 表驱动 httptest,覆盖 404 全资源面、409 两类、400 坏 body、500 内部错误,以及“删除真实资源仍 200”的正向用例。
  • 文档:api-reference.md 状态码表补规则(DELETE 无行必须 404、重名 409、已在运行 409、404 不回数据库原文),实例表示补三个指标字段。

测试先红后绿

新测试在基线 a0d5148 上跑(只拷入测试文件):

--- FAIL: TestDeleteUnknownResourceIs404/route      DELETE /routes/does-not-exist = 200, want 404
--- FAIL: TestAckUnknownMessageIs404                ack unknown message = 200, want 404
--- FAIL: TestNotFoundMapsTo404/update_instance     PUT /instances/nope = 400, want 404 (body "record not found")
--- FAIL: TestDuplicateIs409/duplicate_platform_code POST /platforms = 400, want 409
--- FAIL: TestInternalErrorIs500                    broken query = 404, want 500

本分支全绿。

本地校验

命令 结果
CGO_ENABLED=1 make test 8 包全 ok
golangci-lint run 0 issues.
gofmt -l . 空
npx tsc --noEmit(改了前端) 通过
npm run lint 0 error / 65 warning(预算内)

约束:未引入新依赖;全程离线(只在本机起服务打 curl/WS,不碰上游平台);CGO_ENABLED=1。

范围外

  • M5(平台写路由)已在主干,未动。
  • 监控页把 message_count 当窗口速率算的 “messages per minute” 仍是累计均值,只在变量名上写明,未改成时间窗统计。
  • 已安装的 gh 仍不可用,PR 由 API 创建。

…ng metrics

M11 假成功:DELETE /routes/<不存在>、/platforms、/instances、/adapters 和
POST /messages/<不存在>/ack 都回 200,因为 repository 的 Delete/UpdateStatus
不看 RowsAffected。改成返回 gorm.ErrRecordNotFound,handler 统一映射 404。

M12 错误码语义:新增 service 层哨兵错误(ErrNotFound/ErrConflict/ErrInvalid/
ErrUnauthorized)和 handler.respondError 单点映射(404/409/400/401/500,
其余仍 500)。Get/Update/Delete 的 not-found 分支不再回 400,重名
(platform code、username)走 409,`record not found` 原文不再当 400 文案。

M10 恒为 0 的监控页:AdapterInstance 补 last_heartbeat / message_count /
error_count,迁移 003 与 AutoMigrate 双重覆盖;心跳、入站消息、失败投递在
WebSocket 热路径上维护,状态翻 running,指标不再恒为 0。

M13 测试:internal/handler 新增 httptest 表驱动用例,覆盖 404/409 两端与
500/400 边界;在基线提交上先红后绿。

文档:api-reference.md 的状态码表补规则并补实例指标字段。

Co-authored-by: multica-agent <github@multica.ai>
@TyperBody
TyperBody force-pushed the agent/sxy/e371b29c7b9a branch from 17547cd to 44c9029 Compare November 7, 2026 14:16
@TyperBody TyperBody closed this Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant