diff --git a/samples/eShopLite/BasketService/BasketService.cs b/samples/eShopLite/BasketService/BasketService.cs index f95c7ff224..0008178502 100644 --- a/samples/eShopLite/BasketService/BasketService.cs +++ b/samples/eShopLite/BasketService/BasketService.cs @@ -23,10 +23,6 @@ public override async Task GetBasketById(BasketRequest r { return MapToCustomerBasketResponse(data); } - else - { - context.Status = new Status(StatusCode.NotFound, $"Basket with id {request.Id} does not exist"); - } return new CustomerBasketResponse(); } @@ -36,14 +32,12 @@ public override async Task GetBasketById(BasketRequest r var customerBasket = MapToCustomerBasket(request); var response = await _repository.UpdateBasketAsync(customerBasket); - if (response != null) + if (response is null) { - return MapToCustomerBasketResponse(response); + throw new RpcException(new Status(StatusCode.NotFound, $"Basket with buyer id {request.BuyerId} does not exist")); } - context.Status = new Status(StatusCode.NotFound, $"Basket with buyer id {request.BuyerId} do not exist"); - - return null; + return MapToCustomerBasketResponse(response); } public override async Task CheckoutBasket(CheckoutCustomerBasketRequest request, ServerCallContext context) @@ -53,8 +47,7 @@ public override async Task CheckoutBasket(Checko if (basket is null) { - context.Status = new Status(StatusCode.NotFound, $"Basket with id {buyerId} does not exist"); - return new(); + throw new RpcException(new Status(StatusCode.NotFound, $"Basket with buyer id {request.BuyerId} does not exist")); } var order = new Order() diff --git a/samples/eShopLite/MyFrontend/Services/BasketServiceClient.cs b/samples/eShopLite/MyFrontend/Services/BasketServiceClient.cs index 1a11bf7663..abb66214d8 100644 --- a/samples/eShopLite/MyFrontend/Services/BasketServiceClient.cs +++ b/samples/eShopLite/MyFrontend/Services/BasketServiceClient.cs @@ -12,16 +12,16 @@ public class BasketServiceClient(Basket.BasketClient client) try { var response = await client.GetBasketByIdAsync(new BasketRequest { Id = buyerId }); - var result = MapToCustomerBasket(response); + var result = !string.IsNullOrEmpty(response.BuyerId) ? MapToCustomerBasket(response) : null; return (result, true); } catch (RpcException ex) when ( - // Basket is not found or service name could not be resolved - ex.StatusCode is StatusCode.NotFound or StatusCode.Unavailable || + // Service name could not be resolved + ex.StatusCode is StatusCode.Unavailable || // Polly resilience timed out after retries (ex.StatusCode is StatusCode.Internal && ex.Status.DebugException is TimeoutRejectedException)) { - return (null, ex.StatusCode == StatusCode.NotFound); + return (null, false); } }