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

Update gRPC calls to not use NotFound status #361

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 4 additions & 11 deletions samples/eShopLite/BasketService/BasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public override async Task<CustomerBasketResponse> GetBasketById(BasketRequest r
{
return MapToCustomerBasketResponse(data);
}
else
{
context.Status = new Status(StatusCode.NotFound, $"Basket with id {request.Id} does not exist");
}

return new CustomerBasketResponse();
}
Expand All @@ -36,14 +32,12 @@ public override async Task<CustomerBasketResponse> 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<CheckoutCustomerBasketResponse> CheckoutBasket(CheckoutCustomerBasketRequest request, ServerCallContext context)
Expand All @@ -53,8 +47,7 @@ public override async Task<CheckoutCustomerBasketResponse> 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()
Expand Down
8 changes: 4 additions & 4 deletions samples/eShopLite/MyFrontend/Services/BasketServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down