From 4db38cde492651c6aaa8940fe2f3b1683288889c Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Mon, 20 May 2024 16:48:54 -0500 Subject: [PATCH] Use std library for errors --- analytics/base.go | 6 +++--- dates/format.go | 4 ++-- dates/parse.go | 5 ++--- dbutil/bulk.go | 9 +++++---- dbutil/errors_test.go | 9 ++++----- dbutil/scan.go | 8 ++++---- go.mod | 1 - go.sum | 2 -- httpx/access.go | 6 +++--- httpx/http.go | 2 +- httpx/mock.go | 2 +- httpx/recorder.go | 5 ++--- i18n/language.go | 6 +++--- storage/s3.go | 7 +++---- urns/phone.go | 2 +- 15 files changed, 34 insertions(+), 40 deletions(-) diff --git a/analytics/base.go b/analytics/base.go index a02f4e0..44acab5 100644 --- a/analytics/base.go +++ b/analytics/base.go @@ -1,6 +1,6 @@ package analytics -import "github.com/pkg/errors" +import "fmt" // Backend is the interface for backends type Backend interface { @@ -21,7 +21,7 @@ func RegisterBackend(b Backend) { func Start() error { for _, b := range backends { if err := b.Start(); err != nil { - return errors.Wrapf(err, "error starting %s analytics backend", b.Name()) + return fmt.Errorf("error starting %s analytics backend: %w", b.Name(), err) } } return nil @@ -38,7 +38,7 @@ func Gauge(name string, value float64) { func Stop() error { for _, b := range backends { if err := b.Stop(); err != nil { - return errors.Wrapf(err, "error stopping %s analytics backend", b.Name()) + return fmt.Errorf("error stopping %s analytics backend: %w", b.Name(), err) } } return nil diff --git a/dates/format.go b/dates/format.go index 2299d11..586a7b7 100644 --- a/dates/format.go +++ b/dates/format.go @@ -2,11 +2,11 @@ package dates import ( "bytes" + "fmt" "strings" "time" "github.com/nyaruka/gocommon/i18n" - "github.com/pkg/errors" ) // Custom date/time formatting using layout strings like YYYY-MM-DD @@ -236,7 +236,7 @@ func visitLayout(layout string, type_ LayoutType, mode LayoutMode, callback func if exists && type_.Includes(layoutSeq.seqType) && (mode != ParsingMode || layoutSeq.parseable) { mapped = layoutSeq.mapped } else { - return errors.Errorf("'%s' is not valid in a %s %s layout", seq, type_, mode) + return fmt.Errorf("'%s' is not valid in a %s %s layout", seq, type_, mode) } } diff --git a/dates/parse.go b/dates/parse.go index ad34377..5d26682 100644 --- a/dates/parse.go +++ b/dates/parse.go @@ -1,9 +1,8 @@ package dates import ( + "fmt" "time" - - "github.com/pkg/errors" ) // ZeroDateTime is our uninitialized datetime value @@ -67,7 +66,7 @@ func parseError(err error) error { } } - return errors.Errorf("cannot parse '%s' as '%s'", typed.ValueElem, origLayoutSeq) + return fmt.Errorf("cannot parse '%s' as '%s'", typed.ValueElem, origLayoutSeq) default: return err } diff --git a/dbutil/bulk.go b/dbutil/bulk.go index 52e0c9c..0d1b9f6 100644 --- a/dbutil/bulk.go +++ b/dbutil/bulk.go @@ -2,10 +2,11 @@ package dbutil import ( "context" + "errors" + "fmt" "strings" "github.com/jmoiron/sqlx" - "github.com/pkg/errors" ) // BulkQueryer is the DB/TX functionality needed for these bulk operations @@ -32,13 +33,13 @@ func BulkSQL[T any](db BulkQueryer, sql string, structs []T) (string, []any, err for i, value := range structs { valueSQL, valueArgs, err := sqlx.Named(sql, value) if err != nil { - return "", nil, errors.Wrapf(err, "error converting bulk insert args") + return "", nil, fmt.Errorf("error converting bulk insert args: %w", err) } args = append(args, valueArgs...) argValues := extractValues(valueSQL) if argValues == "" { - return "", nil, errors.Errorf("error extracting VALUES from sql: %s", valueSQL) + return "", nil, fmt.Errorf("error extracting VALUES from sql: %s", valueSQL) } // append to our global values, adding comma if necessary @@ -50,7 +51,7 @@ func BulkSQL[T any](db BulkQueryer, sql string, structs []T) (string, []any, err valuesSQL := extractValues(sql) if valuesSQL == "" { - return "", nil, errors.Errorf("error extracting VALUES from sql: %s", sql) + return "", nil, fmt.Errorf("error extracting VALUES from sql: %s", sql) } return db.Rebind(strings.Replace(sql, valuesSQL, values.String(), -1)), args, nil diff --git a/dbutil/errors_test.go b/dbutil/errors_test.go index f96d3ab..f55df4e 100644 --- a/dbutil/errors_test.go +++ b/dbutil/errors_test.go @@ -1,13 +1,12 @@ package dbutil_test import ( + "errors" "fmt" "testing" "github.com/lib/pq" "github.com/nyaruka/gocommon/dbutil" - - "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -15,7 +14,7 @@ func TestIsUniqueViolation(t *testing.T) { var err error = &pq.Error{Code: pq.ErrorCode("23505")} assert.True(t, dbutil.IsUniqueViolation(err)) - assert.True(t, dbutil.IsUniqueViolation(errors.Wrap(err, "wrapped"))) + assert.True(t, dbutil.IsUniqueViolation(fmt.Errorf("wrapped: %w", err))) assert.False(t, dbutil.IsUniqueViolation(errors.New("boom"))) } @@ -39,12 +38,12 @@ func TestQueryError(t *testing.T) { assert.Equal(t, err, pqerr) // can unwrap a wrapped error to find the first query error - wrapped := errors.Wrap(errors.Wrap(qerr, "error doing this"), "error doing that") + wrapped := fmt.Errorf("error doing that: %w", fmt.Errorf("error doing this: %w", qerr)) unwrapped := dbutil.AsQueryError(wrapped) assert.Equal(t, qerr, unwrapped) // nil if error was never a query error - wrapped = errors.Wrap(errors.New("error doing this"), "error doing that") + wrapped = fmt.Errorf("error doing that: %w", errors.New("error doing this")) assert.Nil(t, dbutil.AsQueryError(wrapped)) query, params := unwrapped.Query() diff --git a/dbutil/scan.go b/dbutil/scan.go index e83b326..3683a97 100644 --- a/dbutil/scan.go +++ b/dbutil/scan.go @@ -3,9 +3,9 @@ package dbutil import ( "database/sql" "encoding/json" + "fmt" "github.com/go-playground/validator/v10" - "github.com/pkg/errors" ) var validate = validator.New() @@ -15,12 +15,12 @@ func ScanJSON(rows *sql.Rows, destination any) error { var raw json.RawMessage err := rows.Scan(&raw) if err != nil { - return errors.Wrap(err, "error scanning row JSON") + return fmt.Errorf("error scanning row JSON: %w", err) } err = json.Unmarshal(raw, destination) if err != nil { - return errors.Wrap(err, "error unmarshalling row JSON") + return fmt.Errorf("error unmarshalling row JSON: %w", err) } return nil @@ -34,7 +34,7 @@ func ScanAndValidateJSON(rows *sql.Rows, destination any) error { err := validate.Struct(destination) if err != nil { - return errors.Wrapf(err, "error validating unmarsalled JSON") + return fmt.Errorf("error validating unmarsalled JSON: %w", err) } return nil diff --git a/go.mod b/go.mod index decccd0..a6e9244 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,6 @@ require ( github.com/nyaruka/librato v1.1.1 github.com/nyaruka/null/v2 v2.0.3 github.com/nyaruka/phonenumbers v1.3.5 - github.com/pkg/errors v0.9.1 github.com/shopspring/decimal v1.4.0 github.com/stretchr/testify v1.9.0 golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 diff --git a/go.sum b/go.sum index 5830e79..596da8a 100644 --- a/go.sum +++ b/go.sum @@ -45,8 +45,6 @@ github.com/nyaruka/null/v2 v2.0.3 h1:rdmMRQyVzrOF3Jff/gpU/7BDR9mQX0lcLl4yImsA3kw github.com/nyaruka/null/v2 v2.0.3/go.mod h1:OCVeCkCXwrg5/qE6RU0c1oUVZBy+ZDrT+xYg1XSaIWA= github.com/nyaruka/phonenumbers v1.3.5 h1:WZLbQn61j2E1OFnvpUTYbK/6hViUgl6tppJ55/E2iQM= github.com/nyaruka/phonenumbers v1.3.5/go.mod h1:Ut+eFwikULbmCenH6InMKL9csUNLyxHuBLyfkpum11s= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k= diff --git a/httpx/access.go b/httpx/access.go index ec62e12..36f2f8d 100644 --- a/httpx/access.go +++ b/httpx/access.go @@ -1,12 +1,12 @@ package httpx import ( + "fmt" "net" "net/http" "strings" "time" - "github.com/pkg/errors" "golang.org/x/net/context" ) @@ -63,13 +63,13 @@ func ParseNetworks(addrs ...string) ([]net.IP, []*net.IPNet, error) { if strings.Contains(addr, "/") { _, ipNet, err := net.ParseCIDR(addr) if err != nil { - return nil, nil, errors.Errorf("couldn't parse '%s' as an IP network", addr) + return nil, nil, fmt.Errorf("couldn't parse '%s' as an IP network", addr) } ipNets = append(ipNets, ipNet) } else { ip := net.ParseIP(addr) if ip == nil { - return nil, nil, errors.Errorf("couldn't parse '%s' as an IP address", addr) + return nil, nil, fmt.Errorf("couldn't parse '%s' as an IP address", addr) } ips = append(ips, ip) } diff --git a/httpx/http.go b/httpx/http.go index d2d9512..376e369 100644 --- a/httpx/http.go +++ b/httpx/http.go @@ -3,6 +3,7 @@ package httpx import ( "bytes" "encoding/base64" + "errors" "fmt" "io" "net/http" @@ -13,7 +14,6 @@ import ( "github.com/gabriel-vasile/mimetype" "github.com/nyaruka/gocommon/dates" - "github.com/pkg/errors" ) // ErrResponseSize is returned when response size exceeds provided limit diff --git a/httpx/mock.go b/httpx/mock.go index 8a5fe12..c794ef0 100644 --- a/httpx/mock.go +++ b/httpx/mock.go @@ -3,13 +3,13 @@ package httpx import ( "bytes" "encoding/json" + "errors" "fmt" "io" "net/http" "github.com/nyaruka/gocommon/jsonx" "github.com/nyaruka/gocommon/stringsx" - "github.com/pkg/errors" "golang.org/x/exp/maps" ) diff --git a/httpx/recorder.go b/httpx/recorder.go index 3eb3ff3..a950d2f 100644 --- a/httpx/recorder.go +++ b/httpx/recorder.go @@ -10,7 +10,6 @@ import ( "github.com/go-chi/chi/v5/middleware" "github.com/nyaruka/gocommon/dates" - "github.com/pkg/errors" ) // Recorder is a utility for creating traces of HTTP requests being handled @@ -31,7 +30,7 @@ func NewRecorder(r *http.Request, w http.ResponseWriter, reconstruct bool) (*Rec requestTrace, err := httputil.DumpRequest(or, true) if err != nil { - return nil, errors.Wrap(err, "error dumping request") + return nil, fmt.Errorf("error dumping request: %w", err) } // if we cloned the request above, DumpRequest will have drained the body and saved a copy on the reconstructed @@ -66,7 +65,7 @@ func (r *Recorder) End() error { // and parse as response object response, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(responseTrace.Bytes())), r.Trace.Request) if err != nil { - return errors.Wrap(err, "error reading response trace") + return fmt.Errorf("error reading response trace: %w", err) } r.Trace.Response = response diff --git a/i18n/language.go b/i18n/language.go index 2572296..516ea8a 100644 --- a/i18n/language.go +++ b/i18n/language.go @@ -2,9 +2,9 @@ package i18n import ( "database/sql/driver" + "fmt" "github.com/nyaruka/null/v2" - "github.com/pkg/errors" "golang.org/x/text/language" ) @@ -32,12 +32,12 @@ var NilLanguage = Language("") // ParseLanguage returns a new Language for the passed in language string, or an error if not found func ParseLanguage(lang string) (Language, error) { if len(lang) != 3 { - return NilLanguage, errors.Errorf("iso-639-3 codes must be 3 characters, got: %s", lang) + return NilLanguage, fmt.Errorf("iso-639-3 codes must be 3 characters, got: %s", lang) } base, err := language.ParseBase(lang) if err != nil { - return NilLanguage, errors.Errorf("unrecognized language code: %s", lang) + return NilLanguage, fmt.Errorf("unrecognized language code: %s", lang) } return Language(base.ISO3()), nil diff --git a/storage/s3.go b/storage/s3.go index 28ad340..b5a1a04 100644 --- a/storage/s3.go +++ b/storage/s3.go @@ -13,7 +13,6 @@ import ( "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" - "github.com/pkg/errors" ) var s3BucketURL = "https://%s.s3.%s.amazonaws.com/%s" @@ -88,12 +87,12 @@ func (s *s3Storage) Get(ctx context.Context, path string) (string, []byte, error Key: aws.String(path), }) if err != nil { - return "", nil, errors.Wrapf(err, "error getting S3 object") + return "", nil, fmt.Errorf("error getting S3 object: %w", err) } body, err := io.ReadAll(out.Body) if err != nil { - return "", nil, errors.Wrapf(err, "error reading S3 object") + return "", nil, fmt.Errorf("error reading S3 object: %w", err) } return aws.StringValue(out.ContentType), body, nil @@ -109,7 +108,7 @@ func (s *s3Storage) Put(ctx context.Context, path string, contentType string, bo ACL: aws.String(s.acl), }) if err != nil { - return "", errors.Wrapf(err, "error putting S3 object") + return "", fmt.Errorf("error putting S3 object: %w", err) } return s.url(path), nil diff --git a/urns/phone.go b/urns/phone.go index 51a6dc0..58a7962 100644 --- a/urns/phone.go +++ b/urns/phone.go @@ -1,13 +1,13 @@ package urns import ( + "errors" "regexp" "strconv" "strings" "github.com/nyaruka/gocommon/i18n" "github.com/nyaruka/phonenumbers" - "github.com/pkg/errors" ) var nonTelCharsRegex = regexp.MustCompile(`[^0-9A-Za-z]`)