Skip to content

Commit

Permalink
chore: Replace github.com/pkg/errors with Go core error package (#593)
Browse files Browse the repository at this point in the history
* chore: Replace github.com/pkg/errors with Go core error package

* chore: switch to non dynamic errors that already exist
  • Loading branch information
carstendietrich authored Aug 6, 2024
1 parent a8576c7 commit 7864b84
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cart/application/cartCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package application
import (
"context"
"encoding/gob"
"errors"
"fmt"
"strings"
"time"
Expand All @@ -13,7 +14,6 @@ import (
"flamingo.me/flamingo/v3/core/auth"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"github.com/pkg/errors"
)

//go:generate go run github.com/vektra/mockery/[email protected] --name CartCache --case snake
Expand Down
2 changes: 1 addition & 1 deletion cart/application/cartReceiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package application_test

import (
"context"
"errors"
"fmt"
"reflect"
"testing"
Expand All @@ -10,7 +11,6 @@ import (
authMock "flamingo.me/flamingo/v3/core/auth/mock"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down
4 changes: 2 additions & 2 deletions cart/application/cartService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package application
import (
"context"
"encoding/gob"
"errors"
"fmt"

"go.opencensus.io/trace"

"flamingo.me/flamingo/v3/core/auth"
"github.com/pkg/errors"

"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
Expand Down Expand Up @@ -582,7 +582,7 @@ func (cs *CartService) DeleteItem(ctx context.Context, session *web.Session, ite
if err != nil {
cs.handleCartNotFound(session, err)
if !errors.Is(err, context.Canceled) {
cs.logger.WithContext(ctx).WithField(flamingo.LogKeySubCategory, "DeleteItem").Error(errors.Wrap(err, "Trying to delete SKU :"+item.MarketplaceCode))
cs.logger.WithContext(ctx).WithField(flamingo.LogKeySubCategory, "DeleteItem").Error(fmt.Errorf("trying to delete SKU %q: %w", item.MarketplaceCode, err))
}
return err
}
Expand Down
9 changes: 4 additions & 5 deletions cart/domain/cart/cart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package cart
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"math/big"

"github.com/pkg/errors"

"flamingo.me/flamingo/v3/framework/web"

"flamingo.me/flamingo-commerce/v3/price/domain"
Expand Down Expand Up @@ -276,7 +275,7 @@ func (c Cart) GetDeliveryByItemID(itemID string) (*Delivery, error) {
}
}

return nil, errors.Errorf("delivery not found for %q", itemID)
return nil, fmt.Errorf("%w for %q", ErrDeliveryCodeNotFound, itemID)
}

// GetByItemID gets an item by its id
Expand All @@ -289,7 +288,7 @@ func (c Cart) GetByItemID(itemID string) (*Item, error) {
}
}

return nil, errors.Errorf("itemId %q in cart does not exist", itemID)
return nil, fmt.Errorf("%w for itemID %q", ErrItemNotFound, itemID)
}

// GetTotalQty for the product in the cart
Expand All @@ -315,7 +314,7 @@ func (c Cart) GetByExternalReference(ref string) (*Item, error) {
}
}

return nil, errors.Errorf("uitemID %v in cart not existing", ref)
return nil, fmt.Errorf("%w for external reference %q", ErrItemNotFound, ref)
}

// ItemCount returns amount of cart items in the current cart
Expand Down
12 changes: 5 additions & 7 deletions cart/domain/cart/cartServicePorts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ package cart
import (
"context"
"encoding/json"
"errors"

productDomain "flamingo.me/flamingo-commerce/v3/product/domain"
"flamingo.me/flamingo/v3/core/auth"
"flamingo.me/flamingo/v3/framework/flamingo"

"github.com/pkg/errors"

productDomain "flamingo.me/flamingo-commerce/v3/product/domain"
)

type (
Expand Down Expand Up @@ -120,11 +118,11 @@ type (

var (
// ErrCartNotFound is used if a cart was not found
ErrCartNotFound = errors.New("Cart not found")
ErrCartNotFound = errors.New("cart not found")
// ErrItemNotFound is used if a item on cart was not found
ErrItemNotFound = errors.New("Item not found")
ErrItemNotFound = errors.New("item not found")
// ErrDeliveryCodeNotFound is used if a delivery was not found
ErrDeliveryCodeNotFound = errors.New("Delivery not found")
ErrDeliveryCodeNotFound = errors.New("delivery not found")
)

// CreateDeliveryInfoUpdateCommand - factory to get the update command based on the given deliveryInfos (which might come from cart)
Expand Down
4 changes: 2 additions & 2 deletions cart/infrastructure/defaultCartBehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package infrastructure

import (
"context"
"errors"
"fmt"
"math/big"
"math/rand"
Expand All @@ -10,7 +11,6 @@ import (
"go.opencensus.io/trace"

"flamingo.me/flamingo/v3/framework/flamingo"
"github.com/pkg/errors"

domaincart "flamingo.me/flamingo-commerce/v3/cart/domain/cart"
"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"
Expand Down Expand Up @@ -552,7 +552,7 @@ func (cob *DefaultCartBehaviour) CleanDelivery(ctx context.Context, cart *domain

// create delivery if it does not yet exist
if !newCart.HasDeliveryForCode(deliveryCode) {
return nil, nil, errors.Errorf("DefaultCartBehaviour: delivery %s not found", deliveryCode)
return nil, nil, fmt.Errorf("DefaultCartBehaviour: %w with code %q", domaincart.ErrDeliveryCodeNotFound, deliveryCode)
}

var position int
Expand Down
3 changes: 2 additions & 1 deletion cart/infrastructure/defaultCartBehaviour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func TestDefaultCartBehaviour_CleanDelivery(t *testing.T) {
assert.NoError(t, err)

got, _, err := cob.CleanDelivery(context.Background(), cart, "dev-3")
assert.EqualError(t, err, "DefaultCartBehaviour: delivery dev-3 not found")
assert.ErrorIs(t, err, domaincart.ErrDeliveryCodeNotFound)
assert.ErrorContains(t, err, "dev-3")
assert.Nil(t, got)
})
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/leekchan/accounting v0.3.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
github.com/stvp/tempredis v0.0.0-20231107154819-8a695b693b9c
Expand Down Expand Up @@ -123,6 +122,7 @@ require (
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/openzipkin/zipkin-go v0.4.3 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
Expand Down
4 changes: 3 additions & 1 deletion product/domain/productService.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (

// SearchService is a typed search for products
SearchService interface {
//Search returns Products based on given Filters
// Search returns Products based on given Filters
Search(ctx context.Context, filter ...searchDomain.Filter) (*SearchResult, error)
// SearchBy returns Products prefiltered by the given attribute (also based on additional given Filters)
// e.g. SearchBy(ctx,"brandCode","apple")
Expand All @@ -37,6 +37,8 @@ type (
}
)

var _ error = ProductNotFound{}

// Error implements the error interface
func (err ProductNotFound) Error() string {
return fmt.Sprintf("Product with Marketplace Code %q Not Found", err.MarketplaceCode)
Expand Down
18 changes: 8 additions & 10 deletions product/interfaces/controller/apicontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package controller

import (
"context"
"errors"

"flamingo.me/flamingo/v3/framework/web"
"github.com/pkg/errors"

"flamingo.me/flamingo-commerce/v3/product/application"
"flamingo.me/flamingo-commerce/v3/product/domain"
Expand All @@ -28,7 +28,7 @@ type (
resultError struct {
Message string
Code string
} //@name productResultError
} // @name productResultError
)

// Inject dependencies
Expand All @@ -53,19 +53,17 @@ func (c *APIController) Inject(responder *web.Responder,
func (c *APIController) Get(ctx context.Context, r *web.Request) web.Result {
product, err := c.productService.Get(ctx, r.Params["marketplacecode"])
if err != nil {
switch errors.Cause(err).(type) {
case domain.ProductNotFound:
if errors.As(err, &domain.ProductNotFound{}) {
return c.responder.Data(APIResult{
Success: false,
Error: &resultError{Code: "404", Message: err.Error()},
})

default:
return c.responder.Data(APIResult{
Success: false,
Error: &resultError{Code: "500", Message: err.Error()},
})
}

return c.responder.Data(APIResult{
Success: false,
Error: &resultError{Code: "500", Message: err.Error()},
})
}

return c.responder.Data(APIResult{
Expand Down
10 changes: 4 additions & 6 deletions product/interfaces/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package controller

import (
"context"
"errors"
"net/url"

"flamingo.me/flamingo/v3/framework/web"
"github.com/pkg/errors"
"golang.org/x/text/cases"
"golang.org/x/text/language"

Expand Down Expand Up @@ -189,13 +189,11 @@ func (vc *View) Get(c context.Context, r *web.Request) web.Result {

// catch error
if err != nil {
switch errors.Cause(err).(type) {
case domain.ProductNotFound:
if errors.As(err, &domain.ProductNotFound{}) {
return vc.Responder.NotFound(err)

default:
return vc.Responder.ServerError(err)
}

return vc.Responder.ServerError(err)
}

var viewData productViewData
Expand Down
4 changes: 2 additions & 2 deletions w3cdatalayer/application/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"fmt"
"net/url"
"regexp"
"strconv"
Expand All @@ -14,7 +15,6 @@ import (
"flamingo.me/flamingo/v3/core/auth"
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"github.com/pkg/errors"
"go.opencensus.io/tag"

"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"
Expand Down Expand Up @@ -128,7 +128,7 @@ func (s Factory) BuildForCurrentRequest(ctx context.Context, request *web.Reques

baseURL, err := s.router.Absolute(request, request.Request().URL.Path, nil)
if err != nil {
s.logger.Warn(errors.Wrap(err, "cannot build absolute url"))
s.logger.Warn(fmt.Errorf("cannot build absolute url: %w", err))
baseURL = new(url.URL)
}
layer.Page = &domain.Page{
Expand Down
2 changes: 1 addition & 1 deletion w3cdatalayer/application/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package application

import (
"context"
"errors"
"fmt"

"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"
Expand All @@ -11,7 +12,6 @@ import (
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/web"
"flamingo.me/pugtemplate/pugjs"
"github.com/pkg/errors"
)

type (
Expand Down

0 comments on commit 7864b84

Please sign in to comment.