onedrive: fix waitForJob to parse errors correctly #1224

This commit is contained in:
Nick Craig-Wood 2017-03-12 12:00:10 +00:00
parent 194a8f56e1
commit 0faf82702b

View File

@ -4,6 +4,7 @@ package onedrive
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
@ -548,30 +549,38 @@ func (f *Fs) waitForJob(location string, o *Object) error {
Method: "GET",
Path: location,
Absolute: true,
IgnoreStatus: true, // Ignore the http status response since it seems to return valid info on 500 errors
}
var resp *http.Response
var err error
var body []byte
err = f.pacer.Call(func() (bool, error) {
resp, err = f.srv.Call(&opts)
return shouldRetry(resp, err)
if err != nil {
return fs.ShouldRetry(err), err
}
body, err = rest.ReadBody(resp)
return fs.ShouldRetry(err), err
})
if err != nil {
return err
}
if resp.StatusCode == 202 {
// Try to decode the body first as an api.AsyncOperationStatus
var status api.AsyncOperationStatus
err = rest.DecodeJSON(resp, &status)
err = json.Unmarshal(body, &status)
if err != nil {
return err
return errors.Wrapf(err, "async status result not JSON: %q", body)
}
// See if we decoded anything...
if !(status.Operation == "" && status.PercentageComplete == 0 && status.Status == "") {
if status.Status == "failed" || status.Status == "deleteFailed" {
return errors.Errorf("async operation %q returned %q", status.Operation, status.Status)
return errors.Errorf("%s: async operation %q returned %q", o.remote, status.Operation, status.Status)
}
} else {
} else if resp.StatusCode == 200 {
var info api.Item
err = rest.DecodeJSON(resp, &info)
err = json.Unmarshal(body, &info)
if err != nil {
return err
return errors.Wrapf(err, "async item result not JSON: %q", body)
}
return o.setMetaData(&info)
}