mirror of
https://github.com/octoleo/restic.git
synced 2025-02-02 11:58:26 +00:00
rest: rework error reporting and report too short files
This commit is contained in:
parent
eaa3f81d6b
commit
b4895ebd76
@ -30,6 +30,20 @@ type Backend struct {
|
|||||||
layout.Layout
|
layout.Layout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// restError is returned whenever the server returns a non-successful HTTP status.
|
||||||
|
type restError struct {
|
||||||
|
backend.Handle
|
||||||
|
StatusCode int
|
||||||
|
Status string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *restError) Error() string {
|
||||||
|
if e.StatusCode == http.StatusNotFound && e.Handle.Type.String() != "invalid" {
|
||||||
|
return fmt.Sprintf("%v does not exist", e.Handle)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("unexpected HTTP response (%v): %v", e.StatusCode, e.Status)
|
||||||
|
}
|
||||||
|
|
||||||
func NewFactory() location.Factory {
|
func NewFactory() location.Factory {
|
||||||
return location.NewHTTPBackendFactory("rest", ParseConfig, StripPassword, Create, Open)
|
return location.NewHTTPBackendFactory("rest", ParseConfig, StripPassword, Create, Open)
|
||||||
}
|
}
|
||||||
@ -96,7 +110,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("server response unexpected: %v (%v)", resp.Status, resp.StatusCode)
|
return nil, &restError{backend.Handle{}, resp.StatusCode, resp.Status}
|
||||||
}
|
}
|
||||||
|
|
||||||
return be, nil
|
return be, nil
|
||||||
@ -150,26 +164,31 @@ func (b *Backend) Save(ctx context.Context, h backend.Handle, rd backend.RewindR
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return errors.Errorf("server response unexpected: %v (%v)", resp.Status, resp.StatusCode)
|
return &restError{h, resp.StatusCode, resp.Status}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// notExistError is returned whenever the requested file does not exist on the
|
|
||||||
// server.
|
|
||||||
type notExistError struct {
|
|
||||||
backend.Handle
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *notExistError) Error() string {
|
|
||||||
return fmt.Sprintf("%v does not exist", e.Handle)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsNotExist returns true if the error was caused by a non-existing file.
|
// IsNotExist returns true if the error was caused by a non-existing file.
|
||||||
func (b *Backend) IsNotExist(err error) bool {
|
func (b *Backend) IsNotExist(err error) bool {
|
||||||
var e *notExistError
|
var e *restError
|
||||||
return errors.As(err, &e)
|
return errors.As(err, &e) && e.StatusCode == http.StatusNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Backend) IsPermanentError(err error) bool {
|
||||||
|
if b.IsNotExist(err) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var rerr *restError
|
||||||
|
if errors.As(err, &rerr) {
|
||||||
|
if rerr.StatusCode == http.StatusRequestedRangeNotSatisfiable || rerr.StatusCode == http.StatusUnauthorized || rerr.StatusCode == http.StatusForbidden {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load runs fn with a reader that yields the contents of the file at h at the
|
// Load runs fn with a reader that yields the contents of the file at h at the
|
||||||
@ -221,14 +240,13 @@ func (b *Backend) openReader(ctx context.Context, h backend.Handle, length int,
|
|||||||
return nil, errors.Wrap(err, "client.Do")
|
return nil, errors.Wrap(err, "client.Do")
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusNotFound {
|
|
||||||
_ = drainAndClose(resp)
|
|
||||||
return nil, ¬ExistError{h}
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
|
||||||
_ = drainAndClose(resp)
|
_ = drainAndClose(resp)
|
||||||
return nil, errors.Errorf("unexpected HTTP response (%v): %v", resp.StatusCode, resp.Status)
|
return nil, &restError{h, resp.StatusCode, resp.Status}
|
||||||
|
}
|
||||||
|
|
||||||
|
if length > 0 && resp.ContentLength != int64(length) {
|
||||||
|
return nil, &restError{h, http.StatusRequestedRangeNotSatisfiable, "partial out of bounds read"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp.Body, nil
|
return resp.Body, nil
|
||||||
@ -251,12 +269,8 @@ func (b *Backend) Stat(ctx context.Context, h backend.Handle) (backend.FileInfo,
|
|||||||
return backend.FileInfo{}, err
|
return backend.FileInfo{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusNotFound {
|
|
||||||
return backend.FileInfo{}, ¬ExistError{h}
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return backend.FileInfo{}, errors.Errorf("unexpected HTTP response (%v): %v", resp.StatusCode, resp.Status)
|
return backend.FileInfo{}, &restError{h, resp.StatusCode, resp.Status}
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.ContentLength < 0 {
|
if resp.ContentLength < 0 {
|
||||||
@ -288,12 +302,8 @@ func (b *Backend) Remove(ctx context.Context, h backend.Handle) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusNotFound {
|
|
||||||
return ¬ExistError{h}
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return errors.Errorf("blob not removed, server response: %v (%v)", resp.Status, resp.StatusCode)
|
return &restError{h, resp.StatusCode, resp.Status}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -330,7 +340,7 @@ func (b *Backend) List(ctx context.Context, t backend.FileType, fn func(backend.
|
|||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
_ = drainAndClose(resp)
|
_ = drainAndClose(resp)
|
||||||
return errors.Errorf("List failed, server response: %v (%v)", resp.Status, resp.StatusCode)
|
return &restError{backend.Handle{Type: t}, resp.StatusCode, resp.Status}
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Header.Get("Content-Type") == ContentTypeV2 {
|
if resp.Header.Get("Content-Type") == ContentTypeV2 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user