fix: inherit App error catalogs on generated routes without local errors

This commit is contained in:
2026-09-24 15:42:03 +08:00
parent bca2ef92cc
commit a2a07a39ea
21 changed files with 570 additions and 54 deletions

View File

@@ -80,3 +80,22 @@ func TestCatalogWrapperPreservesScope(t *testing.T) {
}
}
}
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")
}
}