Remove github.com/pkg/errors and replace with std library version

This is possible now that we no longer support go1.12 and brings
rclone into line with standard practices in the Go world.

This also removes errors.New and errors.Errorf from lib/errors and
prefers the stdlib errors package over lib/errors.
This commit is contained in:
Nick Craig-Wood
2021-11-04 10:12:57 +00:00
parent 97328e5755
commit e43b5ce5e5
233 changed files with 1673 additions and 1695 deletions

View File

@@ -8,6 +8,8 @@ import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
@@ -15,7 +17,6 @@ import (
"net/url"
"sync"
"github.com/pkg/errors"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/readers"
)
@@ -51,9 +52,9 @@ func ReadBody(resp *http.Response) (result []byte, err error) {
func defaultErrorHandler(resp *http.Response) (err error) {
body, err := ReadBody(resp)
if err != nil {
return errors.Wrap(err, "error reading error out of body")
return fmt.Errorf("error reading error out of body: %w", err)
}
return errors.Errorf("HTTP error %v (%v) returned body: %q", resp.StatusCode, resp.Status, body)
return fmt.Errorf("HTTP error %v (%v) returned body: %q", resp.StatusCode, resp.Status, body)
}
// SetErrorHandler sets the handler to decode an error response when
@@ -272,7 +273,7 @@ func (api *Client) Call(ctx context.Context, opts *Opts) (resp *http.Response, e
err = api.signer(req)
api.mu.RLock()
if err != nil {
return nil, errors.Wrap(err, "signer failed")
return nil, fmt.Errorf("signer failed: %w", err)
}
}
api.mu.RUnlock()
@@ -286,7 +287,7 @@ func (api *Client) Call(ctx context.Context, opts *Opts) (resp *http.Response, e
err = api.errorHandler(resp)
if err.Error() == "" {
// replace empty errors with something
err = errors.Errorf("http error %d: %v", resp.StatusCode, resp.Status)
err = fmt.Errorf("http error %d: %v", resp.StatusCode, resp.Status)
}
return resp, err
}
@@ -364,7 +365,7 @@ func MultipartUpload(ctx context.Context, in io.Reader, params url.Values, conte
for _, val := range vals {
err = writer.WriteField(key, val)
if err != nil {
_ = bodyWriter.CloseWithError(errors.Wrap(err, "create metadata part"))
_ = bodyWriter.CloseWithError(fmt.Errorf("create metadata part: %w", err))
return
}
}
@@ -373,20 +374,20 @@ func MultipartUpload(ctx context.Context, in io.Reader, params url.Values, conte
if in != nil {
part, err := writer.CreateFormFile(contentName, fileName)
if err != nil {
_ = bodyWriter.CloseWithError(errors.Wrap(err, "failed to create form file"))
_ = bodyWriter.CloseWithError(fmt.Errorf("failed to create form file: %w", err))
return
}
_, err = io.Copy(part, in)
if err != nil {
_ = bodyWriter.CloseWithError(errors.Wrap(err, "failed to copy data"))
_ = bodyWriter.CloseWithError(fmt.Errorf("failed to copy data: %w", err))
return
}
}
err = writer.Close()
if err != nil {
_ = bodyWriter.CloseWithError(errors.Wrap(err, "failed to close form"))
_ = bodyWriter.CloseWithError(fmt.Errorf("failed to close form: %w", err))
return
}

View File

@@ -1,9 +1,8 @@
package rest
import (
"fmt"
"net/url"
"github.com/pkg/errors"
)
// URLJoin joins a URL and a path returning a new URL
@@ -12,7 +11,7 @@ import (
func URLJoin(base *url.URL, path string) (*url.URL, error) {
rel, err := url.Parse(path)
if err != nil {
return nil, errors.Wrapf(err, "Error parsing %q as URL", path)
return nil, fmt.Errorf("Error parsing %q as URL: %w", path, err)
}
return base.ResolveReference(rel), nil
}