1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-28 14:13:28 +00:00

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 ( import (
"bytes" "bytes"
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
@@ -548,30 +549,38 @@ func (f *Fs) waitForJob(location string, o *Object) error {
Method: "GET", Method: "GET",
Path: location, Path: location,
Absolute: true, Absolute: true,
IgnoreStatus: true, // Ignore the http status response since it seems to return valid info on 500 errors
} }
var resp *http.Response var resp *http.Response
var err error var err error
var body []byte
err = f.pacer.Call(func() (bool, error) { err = f.pacer.Call(func() (bool, error) {
resp, err = f.srv.Call(&opts) 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 { if err != nil {
return err return err
} }
if resp.StatusCode == 202 { // Try to decode the body first as an api.AsyncOperationStatus
var status api.AsyncOperationStatus var status api.AsyncOperationStatus
err = rest.DecodeJSON(resp, &status) err = json.Unmarshal(body, &status)
if err != nil { 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" { 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 var info api.Item
err = rest.DecodeJSON(resp, &info) err = json.Unmarshal(body, &info)
if err != nil { if err != nil {
return err return errors.Wrapf(err, "async item result not JSON: %q", body)
} }
return o.setMetaData(&info) return o.setMetaData(&info)
} }