Files
Error-Code-System/domains/export.go
T
weichenandCursor bd53e9ba79
ci / vet-and-test (push) Successful in 3s
ci / docker-checkout-hostgateway (push) Successful in 3s
Add a shared error-code catalog so failures can be traced by stable codes.
Services can resolve legacy aliases and expose the catalog over CLI and HTTP instead of ad-hoc error strings.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-29 13:43:12 +00:00

36 lines
862 B
Go

package domains
import (
"bytes"
"encoding/json"
"fmt"
"error-code-system/errcode"
)
// ExportDomain 导出单个域的定义 JSON(字典序)。
func ExportDomain(r *errcode.Registry, domain string) ([]byte, error) {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(r.List(domain)); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// DomainOf 返回定义切片中的域;混合域或空切片返回错误。
func DomainOf(defs []errcode.Definition) (string, error) {
if len(defs) == 0 {
return "", fmt.Errorf("domains: empty catalog")
}
domain := defs[0].Code.Domain()
for _, def := range defs[1:] {
if def.Code.Domain() != domain {
return "", fmt.Errorf("domains: mixed domains %s and %s", domain, def.Code.Domain())
}
}
return domain, nil
}