Merge pull request #646 from duglin/revertErrorsJSON
Add back in the "errors" wrapper in the Errors serialization
This commit is contained in:
commit
26b7fb7aaf
3 changed files with 18 additions and 12 deletions
|
@ -172,7 +172,9 @@ type jsonError struct {
|
||||||
// MarshalJSON converts slice of error, ErrorCode or Error into a
|
// MarshalJSON converts slice of error, ErrorCode or Error into a
|
||||||
// slice of Error - then serializes
|
// slice of Error - then serializes
|
||||||
func (errs Errors) MarshalJSON() ([]byte, error) {
|
func (errs Errors) MarshalJSON() ([]byte, error) {
|
||||||
var tmpErrs []jsonError
|
var tmpErrs struct {
|
||||||
|
Errors []jsonError `json:"errors,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
for _, daErr := range errs {
|
for _, daErr := range errs {
|
||||||
var err Error
|
var err Error
|
||||||
|
@ -187,7 +189,7 @@ func (errs Errors) MarshalJSON() ([]byte, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpErrs = append(tmpErrs, jsonError{
|
tmpErrs.Errors = append(tmpErrs.Errors, jsonError{
|
||||||
Code: err.Code,
|
Code: err.Code,
|
||||||
Message: err.Message(),
|
Message: err.Message(),
|
||||||
Detail: err.Detail,
|
Detail: err.Detail,
|
||||||
|
@ -200,14 +202,16 @@ func (errs Errors) MarshalJSON() ([]byte, error) {
|
||||||
// UnmarshalJSON deserializes []Error and then converts it into slice of
|
// UnmarshalJSON deserializes []Error and then converts it into slice of
|
||||||
// Error or ErrorCode
|
// Error or ErrorCode
|
||||||
func (errs *Errors) UnmarshalJSON(data []byte) error {
|
func (errs *Errors) UnmarshalJSON(data []byte) error {
|
||||||
var tmpErrs []jsonError
|
var tmpErrs struct {
|
||||||
|
Errors []jsonError
|
||||||
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(data, &tmpErrs); err != nil {
|
if err := json.Unmarshal(data, &tmpErrs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var newErrs Errors
|
var newErrs Errors
|
||||||
for _, daErr := range tmpErrs {
|
for _, daErr := range tmpErrs.Errors {
|
||||||
if daErr.Detail == nil {
|
if daErr.Detail == nil {
|
||||||
// Error's w/o details get converted to ErrorCode
|
// Error's w/o details get converted to ErrorCode
|
||||||
newErrs = append(newErrs, daErr.Code)
|
newErrs = append(newErrs, daErr.Code)
|
||||||
|
|
|
@ -89,7 +89,7 @@ func TestErrorsManagement(t *testing.T) {
|
||||||
t.Fatalf("error marashaling errors: %v", err)
|
t.Fatalf("error marashaling errors: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedJSON := "[{\"code\":\"TEST1\",\"message\":\"test error 1\"},{\"code\":\"TEST2\",\"message\":\"test error 2\",\"detail\":{\"digest\":\"sometestblobsumdoesntmatter\"}}]"
|
expectedJSON := "{\"errors\":[{\"code\":\"TEST1\",\"message\":\"test error 1\"},{\"code\":\"TEST2\",\"message\":\"test error 2\",\"detail\":{\"digest\":\"sometestblobsumdoesntmatter\"}}]}"
|
||||||
|
|
||||||
if string(p) != expectedJSON {
|
if string(p) != expectedJSON {
|
||||||
t.Fatalf("unexpected json:\ngot:\n%q\n\nexpected:\n%q", string(p), expectedJSON)
|
t.Fatalf("unexpected json:\ngot:\n%q\n\nexpected:\n%q", string(p), expectedJSON)
|
||||||
|
@ -107,7 +107,7 @@ func TestErrorsManagement(t *testing.T) {
|
||||||
|
|
||||||
// Test again with a single value this time
|
// Test again with a single value this time
|
||||||
errs = Errors{ErrorCodeUnknown}
|
errs = Errors{ErrorCodeUnknown}
|
||||||
expectedJSON = "[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\"}]"
|
expectedJSON = "{\"errors\":[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\"}]}"
|
||||||
p, err = json.Marshal(errs)
|
p, err = json.Marshal(errs)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -86,12 +86,14 @@ func TestUploadReadFrom(t *testing.T) {
|
||||||
Response: testutil.Response{
|
Response: testutil.Response{
|
||||||
StatusCode: http.StatusBadRequest,
|
StatusCode: http.StatusBadRequest,
|
||||||
Body: []byte(`
|
Body: []byte(`
|
||||||
[
|
{ "errors":
|
||||||
{
|
[
|
||||||
"code": "BLOB_UPLOAD_INVALID",
|
{
|
||||||
"detail": "more detail"
|
"code": "BLOB_UPLOAD_INVALID",
|
||||||
}
|
"detail": "more detail"
|
||||||
] `),
|
}
|
||||||
|
]
|
||||||
|
} `),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Test 400 invalid json
|
// Test 400 invalid json
|
||||||
|
|
Loading…
Reference in a new issue