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:
@@ -240,6 +240,28 @@ func TestError_NilError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_PlatformCode_IncludedWhenSet(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
Error(rec, xerrors.New(xerrors.ErrNotFound, "employee not found").
|
||||
WithPlatformCode("EMPLOYEE_NOT_FOUND"))
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Errorf("want 404, got %d", rec.Code)
|
||||
}
|
||||
m := decodeMap(t, rec)
|
||||
if m["platformCode"] != "EMPLOYEE_NOT_FOUND" {
|
||||
t.Errorf("platformCode: want EMPLOYEE_NOT_FOUND, got %v", m["platformCode"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_PlatformCode_OmittedWhenNotSet(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
Error(rec, xerrors.New(xerrors.ErrInternal, "unexpected error"))
|
||||
m := decodeMap(t, rec)
|
||||
if _, ok := m["platformCode"]; ok {
|
||||
t.Errorf("platformCode should be absent for errors without a platform code, got %v", m["platformCode"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorCodeToStatus_AllCodes(t *testing.T) {
|
||||
cases := []struct {
|
||||
code xerrors.Code
|
||||
|
||||
Reference in New Issue
Block a user