Skip to content

Commit

Permalink
Drop github.com/pkg/errors and fix error_code detection (#1951)
Browse files Browse the repository at this point in the history
Also fixes a bunch of lint issues and hopefully improves error messages for certain errors.

Co-authored-by: Mihail Stoykov <[email protected]>
  • Loading branch information
na-- and mstoykov authored Apr 6, 2021
1 parent 70248fe commit e5d4d82
Show file tree
Hide file tree
Showing 48 changed files with 332 additions and 702 deletions.
7 changes: 4 additions & 3 deletions api/v1/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
package v1

import (
"fmt"

"github.com/manyminds/api2go/jsonapi"
"github.com/pkg/errors"

"github.com/loadimpact/k6/lib"
)
Expand Down Expand Up @@ -135,7 +136,7 @@ func (g *Group) SetToManyReferenceIDs(name string, ids []string) error {
g.GroupIDs = ids
return nil
default:
return errors.New("Unknown to many relation: " + name)
return fmt.Errorf("unknown to many relation: %s", name)
}
}

Expand All @@ -146,7 +147,7 @@ func (g *Group) SetToOneReferenceID(name, id string) error {
g.ParentID = id
return nil
default:
return errors.New("Unknown to one relation: " + name)
return fmt.Errorf("unknown to one relation: %s", name)
}
}

Expand Down
4 changes: 1 addition & 3 deletions cloudapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"net/http"
"strconv"

"github.com/pkg/errors"

"github.com/loadimpact/k6/lib"
)

Expand Down Expand Up @@ -80,7 +78,7 @@ func (c *Client) CreateTestRun(testRun *TestRun) (*CreateTestRunResponse, error)
}

if ctrr.ReferenceID == "" {
return nil, errors.Errorf("Failed to get a reference ID")
return nil, fmt.Errorf("failed to get a reference ID")
}

return &ctrr, nil
Expand Down
5 changes: 2 additions & 3 deletions cloudapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"net/http"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -205,8 +204,8 @@ func checkResponse(r *http.Response) error {
if r.StatusCode == http.StatusForbidden {
return ErrNotAuthorized
}
return errors.Errorf(
"Unexpected HTTP error from %s: %d %s",
return fmt.Errorf(
"unexpected HTTP error from %s: %d %s",
r.Request.URL,
r.StatusCode,
http.StatusText(r.StatusCode),
Expand Down
3 changes: 1 addition & 2 deletions cloudapi/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
package cloudapi

import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/pkg/errors"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
Expand All @@ -33,7 +34,6 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down
2 changes: 1 addition & 1 deletion cmd/login_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ package cmd

import (
"encoding/json"
"errors"
"os"
"syscall"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down
12 changes: 6 additions & 6 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
package cmd

import (
"errors"
"fmt"
"strings"
"time"

"github.com/pkg/errors"
"github.com/spf13/pflag"
"gopkg.in/guregu/null.v3"

Expand Down Expand Up @@ -140,7 +140,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
for i, s := range stageStrings {
var stage lib.Stage
if err := stage.UnmarshalText([]byte(s)); err != nil {
return opts, errors.Wrapf(err, "stage %d", i)
return opts, fmt.Errorf("error for stage %d: %w", i, err)
}
if !stage.Duration.Valid {
return opts, fmt.Errorf("stage %d doesn't have a specified duration", i)
Expand Down Expand Up @@ -190,7 +190,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
for _, s := range blacklistIPStrings {
net, parseErr := lib.ParseCIDR(s)
if parseErr != nil {
return opts, errors.Wrap(parseErr, "blacklist-ip")
return opts, fmt.Errorf("error parsing blacklist-ip '%s': %w", s, parseErr)
}
opts.BlacklistIPs = append(opts.BlacklistIPs, net)
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
}
if summaryTimeUnit != "" {
if summaryTimeUnit != "s" && summaryTimeUnit != "ms" && summaryTimeUnit != "us" {
return opts, errors.New("invalid summary time unit. Use: 's', 'ms' or 'us'")
return opts, fmt.Errorf("invalid summary time unit '%s', use 's', 'ms' or 'us'", summaryTimeUnit)
}
opts.SummaryTimeUnit = null.StringFrom(summaryTimeUnit)
}
Expand All @@ -246,10 +246,10 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {

if len(runTags) > 0 {
parsedRunTags := make(map[string]string, len(runTags))
for i, s := range runTags {
for _, s := range runTags {
name, value, err := parseTagNameValue(s)
if err != nil {
return opts, errors.Wrapf(err, "tag %d", i)
return opts, fmt.Errorf("error parsing tag '%s': %w", s, err)
}
parsedRunTags[name] = value
}
Expand Down
21 changes: 10 additions & 11 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -35,7 +36,6 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -335,20 +335,19 @@ a commandline interface for interacting with it.`,
}

func getExitCodeFromEngine(err error) ExitCode {
switch e := errors.Cause(err).(type) {
case lib.TimeoutError:
switch e.Place() {
var terr lib.TimeoutError
if errors.As(err, &terr) {
switch terr.Place() {
case consts.SetupFn:
return ExitCode{error: err, Code: setupTimeoutErrorCode, Hint: e.Hint()}
return ExitCode{error: err, Code: setupTimeoutErrorCode, Hint: terr.Hint()}
case consts.TeardownFn:
return ExitCode{error: err, Code: teardownTimeoutErrorCode, Hint: e.Hint()}
return ExitCode{error: err, Code: teardownTimeoutErrorCode, Hint: terr.Hint()}
default:
return ExitCode{error: err, Code: genericTimeoutErrorCode}
}
default:
//nolint:golint
return ExitCode{error: errors.New("Engine error"), Code: genericEngineErrorCode, Hint: err.Error()}
}

return ExitCode{error: errors.New("engine error"), Code: genericEngineErrorCode, Hint: err.Error()}
}

func reportUsage(execScheduler *local.ExecutionScheduler) error {
Expand Down Expand Up @@ -419,10 +418,10 @@ func newRunner(
case typeJS:
return js.NewFromArchive(logger, arc, rtOpts)
default:
return nil, errors.Errorf("archive requests unsupported runner: %s", arc.Type)
return nil, fmt.Errorf("archive requests unsupported runner: %s", arc.Type)
}
default:
return nil, errors.Errorf("unknown -t/--type: %s", typ)
return nil, fmt.Errorf("unknown -t/--type: %s", typ)
}
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/spf13/pflag"
"gopkg.in/guregu/null.v3"

Expand Down Expand Up @@ -144,7 +143,7 @@ func getRuntimeOptions(flags *pflag.FlagSet, environment map[string]string) (lib
k, v := parseEnvKeyValue(kv)
// Allow only alphanumeric ASCII variable names for now
if !userEnvVarName.MatchString(k) {
return opts, errors.Errorf("Invalid environment variable name '%s'", k)
return opts, fmt.Errorf("invalid environment variable name '%s'", k)
}
opts.Env[k] = v
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ package cmd

import (
"context"
"errors"

"github.com/pkg/errors"
"github.com/spf13/cobra"

v1 "github.com/loadimpact/k6/api/v1"
Expand Down
13 changes: 8 additions & 5 deletions converter/har/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"sort"
"strings"

"github.com/pkg/errors"
"github.com/tidwall/pretty"

"github.com/loadimpact/k6/lib"
Expand Down Expand Up @@ -64,15 +64,15 @@ func Convert(h HAR, options lib.Options, minSleep, maxSleep uint, enableChecks b
w := bufio.NewWriter(&b)

if returnOnFailedCheck && !enableChecks {
return "", errors.Errorf("return on failed check requires --enable-status-code-checks")
return "", fmt.Errorf("return on failed check requires --enable-status-code-checks")
}

if correlate && !nobatch {
return "", errors.Errorf("correlation requires --no-batch")
return "", fmt.Errorf("correlation requires --no-batch")
}

if h.Log == nil {
return "", errors.Errorf("invalid HAR file supplied, the 'log' property is missing")
return "", fmt.Errorf("invalid HAR file supplied, the 'log' property is missing")
}

if enableChecks {
Expand Down Expand Up @@ -192,7 +192,10 @@ func Convert(h HAR, options lib.Options, minSleep, maxSleep uint, enableChecks b

if correlate && recordedRedirectURL != "" {
if recordedRedirectURL != e.Request.URL {
return "", errors.Errorf("The har file contained a redirect but the next request did not match that redirect. Possibly a misbehaving client or concurrent requests?")
return "", errors.New( //nolint:stylecheck
"The har file contained a redirect but the next request did not match that redirect. " +
"Possibly a misbehaving client or concurrent requests?",
)
}
fprintf(w, "redirectUrl")
recordedRedirectURL = ""
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/mitchellh/mapstructure v1.1.2
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
github.com/pkg/errors v0.8.1
github.com/pmezard/go-difflib v1.0.0
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e
github.com/sirupsen/logrus v1.8.1
Expand Down
5 changes: 3 additions & 2 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"runtime"

"github.com/dop251/goja"
"github.com/dop251/goja/parser"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"gopkg.in/guregu/null.v3"
Expand Down Expand Up @@ -111,7 +112,7 @@ func NewBundle(
// NewBundleFromArchive creates a new bundle from an lib.Archive.
func NewBundleFromArchive(logger logrus.FieldLogger, arc *lib.Archive, rtOpts lib.RuntimeOptions) (*Bundle, error) {
if arc.Type != "js" {
return nil, errors.Errorf("expected bundle type 'js', got '%s'", arc.Type)
return nil, fmt.Errorf("expected bundle type 'js', got '%s'", arc.Type)
}

if !rtOpts.CompatibilityMode.Valid {
Expand Down
4 changes: 2 additions & 2 deletions js/common/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ package common

import (
"context"
"fmt"
"reflect"
"strings"

"github.com/dop251/goja"
"github.com/pkg/errors"
"github.com/serenize/snaker"
)

Expand Down Expand Up @@ -172,7 +172,7 @@ func Bind(rt *goja.Runtime, v interface{}, ctxPtr *context.Context) map[string]i
reservedArgs := 0
if wantsContext {
if ctxPtr == nil || *ctxPtr == nil {
Throw(rt, errors.Errorf("%s() can only be called from within default()", name))
Throw(rt, fmt.Errorf("%s() can only be called from within default()", name))
}
args[0] = reflect.ValueOf(*ctxPtr)
reservedArgs++
Expand Down
4 changes: 2 additions & 2 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ package js

import (
"context"
"errors"
"fmt"
"net/url"
"path/filepath"
"strings"

"github.com/dop251/goja"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"

Expand Down Expand Up @@ -142,7 +142,7 @@ func (i *InitContext) Require(arg string) goja.Value {
func (i *InitContext) requireModule(name string) (goja.Value, error) {
mod := modules.Get(name)
if mod == nil {
return nil, errors.Errorf("unknown module: %s", name)
return nil, fmt.Errorf("unknown module: %s", name)
}
return i.runtime.ToValue(common.Bind(i.runtime, mod, i.ctxPtr)), nil
}
Expand Down
5 changes: 2 additions & 3 deletions js/modules/k6/crypto/x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"time"

"github.com/pkg/errors"

"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/js/internal/modules"
)
Expand Down Expand Up @@ -145,7 +144,7 @@ func parseCertificate(encoded []byte) (*x509.Certificate, error) {
}
parsed, err := x509.ParseCertificate(decoded.Bytes)
if err != nil {
err = errors.Wrap(err, "failed to parse certificate")
err = fmt.Errorf("failed to parse certificate: %w", err)
return nil, err
}
return parsed, nil
Expand Down
Loading

0 comments on commit e5d4d82

Please sign in to comment.