- case http.StatusForbidden:
- resp.Body.Close()
- return nil, ArvadosErrorForbidden
- case http.StatusNotFound:
- resp.Body.Close()
- return nil, ArvadosErrorNotFound
- default:
- resp.Body.Close()
- if resp.StatusCode >= 400 && resp.StatusCode <= 499 {
- return nil, ArvadosErrorBadRequest
- } else {
- return nil, ArvadosErrorServerError
+ }
+
+ defer resp.Body.Close()
+ errorText := fmt.Sprintf("API response: %s", resp.Status)
+
+ // If the response body has {"errors":["reason1","reason2"]}
+ // then return those reasons.
+ var errInfo = Dict{}
+ if err := json.NewDecoder(resp.Body).Decode(&errInfo); err == nil {
+ if errorList, ok := errInfo["errors"]; ok {
+ var errorStrings []string
+ if errArray, ok := errorList.([]interface{}); ok {
+ for _, errItem := range errArray {
+ // We expect an array of strings here.
+ // Non-strings will be passed along
+ // JSON-encoded.
+ if s, ok := errItem.(string); ok {
+ errorStrings = append(errorStrings, s)
+ } else if j, err := json.Marshal(errItem); err == nil {
+ errorStrings = append(errorStrings, string(j))
+ }
+ }
+ errorText = strings.Join(errorStrings, "; ")
+ }