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

Remove github.com/pkg/errors in favor of Go core errors package #334

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions product/interfaces/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ func (vc *View) Get(c context.Context, r *web.Request) web.Result {

// catch error
if err != nil {
switch errors.Unwrap(err).(type) {
case domain.ProductNotFound:
return vc.Responder.NotFound(err)
if unwrappedErr := errors.Unwrap(err); unwrappedErr != nil {
err = unwrappedErr
}

default:
return vc.Responder.ServerError(err)
if _, ok := err.(domain.ProductNotFound); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about errors.Is(err, domain.ProductNotFound{})?

return vc.Responder.NotFound(err)
}

return vc.Responder.ServerError(err)
}

var viewData productViewData
Expand Down
37 changes: 24 additions & 13 deletions product/interfaces/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -74,6 +75,9 @@ func (mps *MockProductService) Get(_ context.Context, marketplacecode string) (d
if marketplacecode == "not_found" {
return nil, domain.ProductNotFound{MarketplaceCode: "not_found"}
}
if marketplacecode == "not_found_wrapped" {
return nil, fmt.Errorf("wrap it: %w", domain.ProductNotFound{MarketplaceCode: "not_found"})
}
if marketplacecode == "simple" {
return domain.SimpleProduct{
BasicProductData: domain.BasicProductData{Title: "My Product Title", MarketPlaceCode: marketplacecode},
Expand Down Expand Up @@ -127,22 +131,29 @@ func TestViewController_GetNotFound(t *testing.T) {
marketPlaceCode: "not_found",
expectedStatus: http.StatusNotFound,
},
{
name: "not found wrapped error",
marketPlaceCode: "not_found_wrapped",
expectedStatus: http.StatusNotFound,
},
}
for _, tt := range tests {
vc := getController()

// call with correct name parameter and expect Rendering
ctx := context.Background()
r := web.CreateRequest(&http.Request{}, nil)
r.Request().URL = &url.URL{}
r.Params = web.RequestParams{
"marketplacecode": tt.marketPlaceCode,
"name": tt.marketPlaceCode,
}
t.Run(tt.name, func(t *testing.T) {
vc := getController()

// call with correct name parameter and expect Rendering
ctx := context.Background()
r := web.CreateRequest(&http.Request{}, nil)
r.Request().URL = &url.URL{}
r.Params = web.RequestParams{
"marketplacecode": tt.marketPlaceCode,
"name": tt.marketPlaceCode,
}

result := vc.Get(ctx, r)
require.IsType(t, &web.ServerErrorResponse{}, result)
assert.Equal(t, int(tt.expectedStatus), int(result.(*web.ServerErrorResponse).Response.Status))
result := vc.Get(ctx, r)
require.IsType(t, &web.ServerErrorResponse{}, result)
assert.Equal(t, int(tt.expectedStatus), int(result.(*web.ServerErrorResponse).Response.Status))
})
}
}

Expand Down