Skip to content

Commit 17b80f9

Browse files
committed
Fix error handling corrected, non positive quantity rejeected, http client's address resolved
1 parent 4bd74c9 commit 17b80f9

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

challenge-14/submissions/PopovMarko/solution-template.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ type UserServiceClient struct {
456456
func NewUserServiceClient(conn *grpc.ClientConn) UserService {
457457
// Extract address from connection for HTTP calls
458458
// In a real gRPC implementation, this would use the connection directly
459-
return &UserServiceClient{baseURL: "http://localhost:50051"}
459+
return &UserServiceClient{baseURL: fmt.Sprintf("http://%s", conn.Target())}
460460
}
461461

462462
// GetUser returns User or error not found
@@ -490,6 +490,9 @@ func (c *UserServiceClient) ValidateUser(ctx context.Context, userID int64) (boo
490490
return false, err
491491
}
492492
resp, err := http.DefaultClient.Do(req)
493+
if err != nil {
494+
return false, err
495+
}
493496
defer resp.Body.Close()
494497

495498
if resp.StatusCode == http.StatusNotFound {
@@ -510,7 +513,7 @@ type ProductServiceClient struct {
510513
}
511514

512515
func NewProductServiceClient(conn *grpc.ClientConn) ProductService {
513-
return &ProductServiceClient{conn: conn, baseURL: "http://localhost:50052"}
516+
return &ProductServiceClient{conn: conn, baseURL: fmt.Sprintf("http://%s", conn.Target())}
514517
}
515518

516519
// GetProduct returns product or error if not found
@@ -537,6 +540,9 @@ func (c *ProductServiceClient) GetProduct(ctx context.Context, productID int64)
537540

538541
// CheckInventory returns bool var for products availability
539542
func (c *ProductServiceClient) CheckInventory(ctx context.Context, productID int64, quantity int32) (bool, error) {
543+
if quantity <= 0 {
544+
return false, status.Error(codes.InvalidArgument, "Quantity can not be negative")
545+
}
540546
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/product/inventorycheck?id=%d&qty=%d", c.baseURL, productID, quantity), nil)
541547
if err != nil {
542548
return false, err

0 commit comments

Comments
 (0)