Archived
1
0

Handle unexpected HTTP error codes

This commit is contained in:
Arnaud Ysmal 2014-07-13 12:13:36 +02:00
parent 8f46d993e2
commit 811d4db755

View File

@ -280,7 +280,7 @@ func getResponse(r *http.Response) ([]byte, error) {
return nil, newErrorf(r.StatusCode, "wrong parameter") return nil, newErrorf(r.StatusCode, "wrong parameter")
} }
} }
return nil, newErrorf(r.StatusCode, "unexpected HTTP status code %d") return nil, newErrorf(r.StatusCode, "unexpected HTTP status code %d", r.StatusCode)
} }
// CommitChunkedUpload ends the chunked upload by giving a name to the UploadID. // CommitChunkedUpload ends the chunked upload by giving a name to the UploadID.
@ -454,16 +454,19 @@ func (db *Dropbox) Thumbnails(src, format, size string) (io.ReadCloser, int64, *
if response, err = db.Session.Client().Get(rawurl); err != nil { if response, err = db.Session.Client().Get(rawurl); err != nil {
return nil, 0, nil, err return nil, 0, nil, err
} }
if response.StatusCode == http.StatusOK {
json.Unmarshal([]byte(response.Header.Get("x-dropbox-metadata")), &entry)
return response.Body, response.ContentLength, &entry, err
}
response.Body.Close()
switch response.StatusCode { switch response.StatusCode {
case http.StatusNotFound: case http.StatusNotFound:
response.Body.Close()
return nil, 0, nil, os.ErrNotExist return nil, 0, nil, os.ErrNotExist
case http.StatusUnsupportedMediaType: case http.StatusUnsupportedMediaType:
response.Body.Close()
return nil, 0, nil, newErrorf(response.StatusCode, "the image located at '%s' cannot be converted to a thumbnail", src) return nil, 0, nil, newErrorf(response.StatusCode, "the image located at '%s' cannot be converted to a thumbnail", src)
default:
return nil, 0, nil, newErrorf(response.StatusCode, "unexpected HTTP status code %d", response.StatusCode)
} }
json.Unmarshal([]byte(response.Header.Get("x-dropbox-metadata")), &entry)
return response.Body, response.ContentLength, &entry, err
} }
// ThumbnailsToFile downloads the file located in the src path on the Dropbox to the dst file on the local disk. // ThumbnailsToFile downloads the file located in the src path on the Dropbox to the dst file on the local disk.
@ -516,11 +519,16 @@ func (db *Dropbox) Download(src, rev string, offset int) (io.ReadCloser, int64,
if response, err = db.Session.Client().Do(request); err != nil { if response, err = db.Session.Client().Do(request); err != nil {
return nil, 0, err return nil, 0, err
} }
if response.StatusCode == http.StatusNotFound { if response.StatusCode == http.StatusOK {
response.Body.Close() return response.Body, response.ContentLength, err
return nil, 0, os.ErrNotExist }
response.Body.Close()
switch response.StatusCode {
case http.StatusNotFound:
return nil, 0, os.ErrNotExist
default:
return nil, 0, newErrorf(response.StatusCode, "unexpected HTTP status code %d", response.StatusCode)
} }
return response.Body, response.ContentLength, err
} }
// DownloadToFileResume resumes the download of the file located in the src path on the Dropbox to the dst file on the local disk. // DownloadToFileResume resumes the download of the file located in the src path on the Dropbox to the dst file on the local disk.