From 365de1b215b9d263551f3d51f522534fd8b58d23 Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Thu, 18 Jun 2015 18:00:26 -0700 Subject: [PATCH] Add back in the "errors" wrapper in the Errors serialization See: https://github.com/docker/distribution/pull/548/files#r32794064 Signed-off-by: Doug Davis --- docs/api/errcode/errors.go | 12 ++++++++---- docs/api/errcode/errors_test.go | 4 ++-- docs/client/blob_writer_test.go | 14 ++++++++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/docs/api/errcode/errors.go b/docs/api/errcode/errors.go index cf186cfb..a68aaad5 100644 --- a/docs/api/errcode/errors.go +++ b/docs/api/errcode/errors.go @@ -172,7 +172,9 @@ type jsonError struct { // MarshalJSON converts slice of error, ErrorCode or Error into a // slice of Error - then serializes func (errs Errors) MarshalJSON() ([]byte, error) { - var tmpErrs []jsonError + var tmpErrs struct { + Errors []jsonError `json:"errors,omitempty"` + } for _, daErr := range errs { 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, Message: err.Message(), Detail: err.Detail, @@ -200,14 +202,16 @@ func (errs Errors) MarshalJSON() ([]byte, error) { // UnmarshalJSON deserializes []Error and then converts it into slice of // Error or ErrorCode func (errs *Errors) UnmarshalJSON(data []byte) error { - var tmpErrs []jsonError + var tmpErrs struct { + Errors []jsonError + } if err := json.Unmarshal(data, &tmpErrs); err != nil { return err } var newErrs Errors - for _, daErr := range tmpErrs { + for _, daErr := range tmpErrs.Errors { if daErr.Detail == nil { // Error's w/o details get converted to ErrorCode newErrs = append(newErrs, daErr.Code) diff --git a/docs/api/errcode/errors_test.go b/docs/api/errcode/errors_test.go index d89c0253..684e263a 100644 --- a/docs/api/errcode/errors_test.go +++ b/docs/api/errcode/errors_test.go @@ -89,7 +89,7 @@ func TestErrorsManagement(t *testing.T) { 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 { 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 errs = Errors{ErrorCodeUnknown} - expectedJSON = "[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\"}]" + expectedJSON = "{\"errors\":[{\"code\":\"UNKNOWN\",\"message\":\"unknown error\"}]}" p, err = json.Marshal(errs) if err != nil { diff --git a/docs/client/blob_writer_test.go b/docs/client/blob_writer_test.go index eeb9f53d..8436ca9a 100644 --- a/docs/client/blob_writer_test.go +++ b/docs/client/blob_writer_test.go @@ -86,12 +86,14 @@ func TestUploadReadFrom(t *testing.T) { Response: testutil.Response{ StatusCode: http.StatusBadRequest, Body: []byte(` - [ - { - "code": "BLOB_UPLOAD_INVALID", - "detail": "more detail" - } - ] `), + { "errors": + [ + { + "code": "BLOB_UPLOAD_INVALID", + "detail": "more detail" + } + ] + } `), }, }, // Test 400 invalid json