feat: add project-owned error catalogs and HTTP 200 failure contracts

This commit is contained in:
2026-09-24 12:40:05 +08:00
parent bb2caefbb4
commit ebfd279f9a
42 changed files with 1897 additions and 120 deletions

View File

@@ -73,10 +73,10 @@ func AccessLog(options ...AccessLogOption) httpx.Middleware {
return func(ctx *httpx.Context) error {
started := time.Now()
err := next(ctx)
status := ctx.Status()
if err != nil && !ctx.Responded() {
status = apierrors.From(err).Status
ctx.WriteError(err)
}
status := ctx.Status()
duration := time.Since(started)
slow := settings.slowThreshold > 0 && duration >= settings.slowThreshold
attrs := []any{
@@ -87,6 +87,16 @@ func AccessLog(options ...AccessLogOption) httpx.Middleware {
"durationMs", duration.Seconds() * 1000,
"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)
if failure.BusinessCode != nil {
attrs = append(attrs, "businessCode", failure.BusinessCode)
}
}
if slow {
attrs = append(attrs, "slow", true)
}