feat: include platformCode in error responses (v0.10.0) (#1)

- Error(w, err) now extracts PlatformCode() from *xerrors.Err and
  includes "platformCode" in the JSON body when set; omitted otherwise
- errorBody updated to accept platformCode as an explicit parameter
- Upgraded code.nochebuena.dev/go/xerrors dependency to v0.10.0
- Added two new tests: platformCode included when set, omitted when absent

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Reviewed-on: #1
Reviewed-by: Rene Nochebuena <rene@noreply.nochebuena.dev>
Co-authored-by: Claude Code <claude@nochebuena.dev>
Co-committed-by: Claude Code <claude@nochebuena.dev>
This commit was merged in pull request #1.
This commit is contained in:
2026-03-25 16:57:49 -06:00
committed by NOCHEBUENADEV
parent 285293a75b
commit 8d34a0c715
5 changed files with 47 additions and 6 deletions

View File

@@ -22,28 +22,31 @@ func NoContent(w http.ResponseWriter) {
}
// Error maps err to the appropriate HTTP status code and writes a JSON error body.
// Understands *xerrors.Err — extracts Code and Message; fields are included if present.
// Understands *xerrors.Err — extracts Code, PlatformCode, and Message.
// Falls back to 500 for unknown errors.
func Error(w http.ResponseWriter, err error) {
if err == nil {
JSON(w, http.StatusInternalServerError, errorBody("INTERNAL", "internal server error", nil))
JSON(w, http.StatusInternalServerError, errorBody("INTERNAL", "", "internal server error", nil))
return
}
var xe *xerrors.Err
if errors.As(err, &xe) {
status := errorCodeToStatus(xe.Code())
body := errorBody(string(xe.Code()), xe.Message(), xe.Fields())
body := errorBody(string(xe.Code()), xe.PlatformCode(), xe.Message(), xe.Fields())
JSON(w, status, body)
return
}
JSON(w, http.StatusInternalServerError, errorBody("INTERNAL", "internal server error", nil))
JSON(w, http.StatusInternalServerError, errorBody("INTERNAL", "", "internal server error", nil))
}
func errorBody(code, message string, fields map[string]any) map[string]any {
func errorBody(code, platformCode, message string, fields map[string]any) map[string]any {
m := map[string]any{
"code": code,
"message": message,
}
if platformCode != "" {
m["platformCode"] = platformCode
}
for k, v := range fields {
m[k] = v
}