diff --git a/post-processor/vagrant-cloud/client.go b/post-processor/vagrant-cloud/client.go index ec71b97e9..4fbd37454 100644 --- a/post-processor/vagrant-cloud/client.go +++ b/post-processor/vagrant-cloud/client.go @@ -26,14 +26,22 @@ type VagrantCloudClient struct { } type VagrantCloudErrors struct { - Errors map[string][]string `json:"errors"` + Errors []interface{} `json:"errors"` } func (v VagrantCloudErrors) FormatErrors() string { errs := make([]string, 0) - for e := range v.Errors { - msg := fmt.Sprintf("%s %s", e, strings.Join(v.Errors[e], ",")) - errs = append(errs, msg) + for _, err := range v.Errors { + switch e := err.(type) { + case string: + errs = append(errs, e) + case map[string]interface{}: + for k, v := range e { + errs = append(errs, fmt.Sprintf("%s %s", k, v)) + } + default: + errs = append(errs, fmt.Sprintf("%s", err)) + } } return strings.Join(errs, ". ") } diff --git a/post-processor/vagrant-cloud/client_test.go b/post-processor/vagrant-cloud/client_test.go new file mode 100644 index 000000000..18388da4a --- /dev/null +++ b/post-processor/vagrant-cloud/client_test.go @@ -0,0 +1,30 @@ +package vagrantcloud + +import ( + "encoding/json" + "strings" + "testing" +) + +func TestVagranCloudErrors(t *testing.T) { + testCases := []struct { + resp string + expected string + }{ + {`{"Status":"422 Unprocessable Entity", "StatusCode":422, "errors":[]}`, ""}, + {`{"Status":"404 Artifact not found", "StatusCode":404, "errors":["error1", "error2"]}`, "error1. error2"}, + {`{"StatusCode":403, "errors":[{"message":"Bad credentials"}]}`, "message Bad credentials"}, + {`{"StatusCode":500, "errors":[["error in unexpected format"]]}`, "[error in unexpected format]"}, + } + + for _, tc := range testCases { + var cloudErrors VagrantCloudErrors + err := json.NewDecoder(strings.NewReader(tc.resp)).Decode(&cloudErrors) + if err != nil { + t.Errorf("failed to decode error response: %s", err) + } + if got := cloudErrors.FormatErrors(); got != tc.expected { + t.Errorf("failed to get expected response; expected %q, got %q", tc.expected, got) + } + } +}