Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-2929 Replace all uses of errutil.WrapErrorf with fmt.Errorf #1354

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions internal/credproviders/imds_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"time"

"go.mongodb.org/mongo-driver/internal/aws/credentials"
"go.mongodb.org/mongo-driver/internal/errutil"
)

const (
Expand Down Expand Up @@ -47,7 +46,7 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va
v := credentials.Value{ProviderName: AzureProviderName}
req, err := http.NewRequest(http.MethodGet, azureURI, nil)
if err != nil {
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials")
return v, fmt.Errorf("unable to retrieve Azure credentials: %w", err)
}
q := make(url.Values)
q.Set("api-version", "2018-02-01")
Expand All @@ -58,15 +57,15 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va

resp, err := a.httpClient.Do(req.WithContext(ctx))
if err != nil {
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials")
return v, fmt.Errorf("unable to retrieve Azure credentials: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: error reading response body")
return v, fmt.Errorf("unable to retrieve Azure credentials: error reading response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body)
return v, fmt.Errorf("unable to retrieve Azure credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body)
}
var tokenResponse struct {
AccessToken string `json:"access_token"`
Expand All @@ -75,7 +74,7 @@ func (a *AzureProvider) RetrieveWithContext(ctx context.Context) (credentials.Va
// Attempt to read body as JSON
err = json.Unmarshal(body, &tokenResponse)
if err != nil {
return v, errutil.WrapErrorf(err, "unable to retrieve Azure credentials: error reading body JSON. Response body: %s", body)
return v, fmt.Errorf("unable to retrieve Azure credentials: error reading body JSON: %w (response body: %s)", err, body)
}
if tokenResponse.AccessToken == "" {
return v, fmt.Errorf("unable to retrieve Azure credentials: got unexpected empty accessToken from Azure Metadata Server. Response body: %s", body)
Expand Down
75 changes: 0 additions & 75 deletions internal/errutil/errutil.go

This file was deleted.

3 changes: 2 additions & 1 deletion internal/integtest/integtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package integtest

import (
"context"
"errors"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -202,7 +203,7 @@ func AddServerlessAuthCredentials(uri string) (string, error) {
} else if strings.HasPrefix(uri, "mongodb://") {
scheme = "mongodb://"
} else {
return "", fmt.Errorf("scheme must be \"mongodb\" or \"mongodb+srv\"")
return "", errors.New(`scheme must be "mongodb" or "mongodb+srv"`)
}

uri = scheme + user + ":" + password + "@" + uri[len(scheme):]
Expand Down
2 changes: 1 addition & 1 deletion mongo/integration/mtest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func addServerlessAuthCredentials(uri string) (string, error) {
} else if strings.HasPrefix(uri, "mongodb://") {
scheme = "mongodb://"
} else {
return "", fmt.Errorf("scheme must be \"mongodb\" or \"mongodb+srv\"")
return "", errors.New(`scheme must be "mongodb" or "mongodb+srv"`)
}

uri = scheme + user + ":" + password + "@" + uri[len(scheme):]
Expand Down
41 changes: 18 additions & 23 deletions mongo/options/clientoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/errutil"
"go.mongodb.org/mongo-driver/internal/httputil"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
Expand All @@ -41,9 +40,9 @@ var tClientOptions = reflect.TypeOf(&ClientOptions{})
func TestClientOptions(t *testing.T) {
t.Run("ApplyURI/doesn't overwrite previous errors", func(t *testing.T) {
uri := "not-mongo-db-uri://"
want := errutil.WrapErrorf(
errors.New(`scheme must be "mongodb" or "mongodb+srv"`), "error parsing uri",
)
want := fmt.Errorf(
"error parsing uri: %w",
errors.New(`scheme must be "mongodb" or "mongodb+srv"`))
co := Client().ApplyURI(uri).ApplyURI("mongodb://localhost/")
got := co.Validate()
if !cmp.Equal(got, want, cmp.Comparer(compareErrors)) {
Expand Down Expand Up @@ -209,9 +208,9 @@ func TestClientOptions(t *testing.T) {
"ParseError",
"not-mongo-db-uri://",
&ClientOptions{
err: errutil.WrapErrorf(
errors.New(`scheme must be "mongodb" or "mongodb+srv"`), "error parsing uri",
),
err: fmt.Errorf(
"error parsing uri: %w",
errors.New(`scheme must be "mongodb" or "mongodb+srv"`)),
HTTPClient: httputil.DefaultHTTPClient,
},
},
Expand Down Expand Up @@ -285,10 +284,9 @@ func TestClientOptions(t *testing.T) {
"Unescaped slash in username",
"mongodb:///:pwd@localhost",
&ClientOptions{
err: errutil.WrapErrorf(
errors.New("unescaped slash in username"),
"error parsing uri",
),
err: fmt.Errorf(
"error parsing uri: %w",
errors.New("unescaped slash in username")),
HTTPClient: httputil.DefaultHTTPClient,
},
},
Expand Down Expand Up @@ -472,33 +470,30 @@ func TestClientOptions(t *testing.T) {
"TLS only tlsCertificateFile",
"mongodb://localhost/?tlsCertificateFile=testdata/nopass/cert.pem",
&ClientOptions{
err: errutil.WrapErrorf(
errors.New("the tlsPrivateKeyFile URI option must be provided if the tlsCertificateFile option is specified"),
"error validating uri",
),
err: fmt.Errorf(
"error validating uri: %w",
errors.New("the tlsPrivateKeyFile URI option must be provided if the tlsCertificateFile option is specified")),
HTTPClient: httputil.DefaultHTTPClient,
},
},
{
"TLS only tlsPrivateKeyFile",
"mongodb://localhost/?tlsPrivateKeyFile=testdata/nopass/key.pem",
&ClientOptions{
err: errutil.WrapErrorf(
errors.New("the tlsCertificateFile URI option must be provided if the tlsPrivateKeyFile option is specified"),
"error validating uri",
),
err: fmt.Errorf(
"error validating uri: %w",
errors.New("the tlsCertificateFile URI option must be provided if the tlsPrivateKeyFile option is specified")),
HTTPClient: httputil.DefaultHTTPClient,
},
},
{
"TLS tlsCertificateFile and tlsPrivateKeyFile and tlsCertificateKeyFile",
"mongodb://localhost/?tlsCertificateFile=testdata/nopass/cert.pem&tlsPrivateKeyFile=testdata/nopass/key.pem&tlsCertificateKeyFile=testdata/nopass/certificate.pem",
&ClientOptions{
err: errutil.WrapErrorf(
err: fmt.Errorf(
"error validating uri: %w",
errors.New("the sslClientCertificateKeyFile/tlsCertificateKeyFile URI option cannot be provided "+
"along with tlsCertificateFile or tlsPrivateKeyFile"),
"error validating uri",
),
"along with tlsCertificateFile or tlsPrivateKeyFile")),
HTTPClient: httputil.DefaultHTTPClient,
},
},
Expand Down
17 changes: 11 additions & 6 deletions x/mongo/driver/auth/creds/gcpcreds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"net/http"
"os"

"go.mongodb.org/mongo-driver/internal/errutil"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

Expand All @@ -37,28 +36,34 @@ func (p GCPCredentialProvider) GetCredentialsDoc(ctx context.Context) (bsoncore.
url := fmt.Sprintf("http://%s/computeMetadata/v1/instance/service-accounts/default/token", metadataHost)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errutil.WrapErrorf(err, "unable to retrieve GCP credentials")
return nil, fmt.Errorf("unable to retrieve GCP credentials: %w", err)
}
req.Header.Set("Metadata-Flavor", "Google")
resp, err := p.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, errutil.WrapErrorf(err, "unable to retrieve GCP credentials")
return nil, fmt.Errorf("unable to retrieve GCP credentials: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errutil.WrapErrorf(err, "unable to retrieve GCP credentials: error reading response body")
return nil, fmt.Errorf("unable to retrieve GCP credentials: error reading response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, errutil.WrapErrorf(err, "unable to retrieve GCP credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s", resp.StatusCode, body)
return nil, fmt.Errorf(
"unable to retrieve GCP credentials: expected StatusCode 200, got StatusCode: %v. Response body: %s",
resp.StatusCode,
body)
}
var tokenResponse struct {
AccessToken string `json:"access_token"`
}
// Attempt to read body as JSON
err = json.Unmarshal(body, &tokenResponse)
if err != nil {
return nil, errutil.WrapErrorf(err, "unable to retrieve GCP credentials: error reading body JSON. Response body: %s", body)
return nil, fmt.Errorf(
"unable to retrieve GCP credentials: error reading body JSON: %w (response body: %s)",
err,
body)
}
if tokenResponse.AccessToken == "" {
return nil, fmt.Errorf("unable to retrieve GCP credentials: got unexpected empty accessToken from GCP Metadata Server. Response body: %s", body)
Expand Down
25 changes: 12 additions & 13 deletions x/mongo/driver/connstring/connstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"strings"
"time"

"go.mongodb.org/mongo-driver/internal/errutil"
"go.mongodb.org/mongo-driver/internal/randutil"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"go.mongodb.org/mongo-driver/x/mongo/driver/dns"
Expand Down Expand Up @@ -58,11 +57,11 @@ func ParseAndValidate(s string) (ConnString, error) {
p := parser{dnsResolver: dns.DefaultResolver}
err := p.parse(s)
if err != nil {
return p.ConnString, errutil.WrapErrorf(err, "error parsing uri")
return p.ConnString, fmt.Errorf("error parsing uri: %w", err)
}
err = p.ConnString.Validate()
if err != nil {
return p.ConnString, errutil.WrapErrorf(err, "error validating uri")
return p.ConnString, fmt.Errorf("error validating uri: %w", err)
}
return p.ConnString, nil
}
Expand All @@ -74,7 +73,7 @@ func Parse(s string) (ConnString, error) {
p := parser{dnsResolver: dns.DefaultResolver}
err := p.parse(s)
if err != nil {
err = errutil.WrapErrorf(err, "error parsing uri")
err = fmt.Errorf("error parsing uri: %w", err)
}
return p.ConnString, err
}
Expand Down Expand Up @@ -240,7 +239,7 @@ func (p *parser) parse(original string) error {
// remove the scheme
uri = uri[len(SchemeMongoDB)+3:]
} else {
return fmt.Errorf("scheme must be \"mongodb\" or \"mongodb+srv\"")
return errors.New(`scheme must be "mongodb" or "mongodb+srv"`)
}

if idx := strings.Index(uri, "@"); idx != -1 {
Expand All @@ -262,7 +261,7 @@ func (p *parser) parse(original string) error {
}
p.Username, err = url.PathUnescape(username)
if err != nil {
return errutil.WrapErrorf(err, "invalid username")
return fmt.Errorf("invalid username: %w", err)
}
p.UsernameSet = true

Expand All @@ -275,7 +274,7 @@ func (p *parser) parse(original string) error {
}
p.Password, err = url.PathUnescape(password)
if err != nil {
return errutil.WrapErrorf(err, "invalid password")
return fmt.Errorf("invalid password: %w", err)
}
}

Expand Down Expand Up @@ -352,7 +351,7 @@ func (p *parser) parse(original string) error {
for _, host := range parsedHosts {
err = p.addHost(host)
if err != nil {
return errutil.WrapErrorf(err, "invalid host %q", host)
return fmt.Errorf("invalid host %q: %w", host, err)
}
}
if len(p.Hosts) == 0 {
Expand Down Expand Up @@ -597,7 +596,7 @@ func (p *parser) addHost(host string) error {
}
host, err := url.QueryUnescape(host)
if err != nil {
return errutil.WrapErrorf(err, "invalid host %q", host)
return fmt.Errorf("invalid host %q: %w", host, err)
}

_, port, err := net.SplitHostPort(host)
Expand All @@ -612,7 +611,7 @@ func (p *parser) addHost(host string) error {
if port != "" {
d, err := strconv.Atoi(port)
if err != nil {
return errutil.WrapErrorf(err, "port must be an integer")
return fmt.Errorf("port must be an integer: %w", err)
}
if d <= 0 || d >= 65536 {
return fmt.Errorf("port must be in the range [1, 65535]")
Expand All @@ -630,12 +629,12 @@ func (p *parser) addOption(pair string) error {

key, err := url.QueryUnescape(kv[0])
if err != nil {
return errutil.WrapErrorf(err, "invalid option key %q", kv[0])
return fmt.Errorf("invalid option key %q: %w", kv[0], err)
}

value, err := url.QueryUnescape(kv[1])
if err != nil {
return errutil.WrapErrorf(err, "invalid option value %q", kv[1])
return fmt.Errorf("invalid option value %q: %w", kv[1], err)
}

lowerKey := strings.ToLower(key)
Expand Down Expand Up @@ -1051,7 +1050,7 @@ func extractDatabaseFromURI(uri string) (extractedDatabase, error) {

escapedDatabase, err := url.QueryUnescape(database)
if err != nil {
return extractedDatabase{}, errutil.WrapErrorf(err, "invalid database %q", database)
return extractedDatabase{}, fmt.Errorf("invalid database %q: %w", database, err)
}

uri = uri[len(database):]
Expand Down
Loading
Loading