feat: include platformCode in error responses (v0.10.0)
- 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>
This commit is contained in:
13
response.go
13
response.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user