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

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