Skip to content

Fix missing error handling in HttpClient.SendAsync call in RedisCartStore.cs #55

@scott-clare1

Description

@scott-clare1

Issue Description

The cartservice is throwing errors in the logs related to HTTP client operations. When examining the code in src/cartservice/src/cartstore/RedisCartStore.cs, I found that the CallExternalApiAsync() method is missing proper error handling and is not awaited properly when called.

Problem Details

In the RedisCartStore.cs file, line 83-102, the CallExternalApiAsync() method is defined but has these issues:

  1. The method is marked as async but it's called without awaiting it in the AddItemAsync method (line 79).
  2. When making a request to the external API, there's no try-catch block to handle potential exceptions that may occur during network calls.
  3. The method doesn't return a Task that the caller can await.

This can cause several problems:

  • Unhandled exceptions from the HTTP request
  • Fire-and-forget pattern with no error handling
  • Potential race conditions if the main method completes before the API call completes

Proposed Solution

  1. Modify the CallExternalApiAsync() method to include proper error handling
  2. Ensure the method is properly awaited when called
  3. Add logging for better troubleshooting
private async Task CallExternalApiAsync()
{
    try
    {
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri(mcpClientEndpoint)
        };

        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", clientEndpointBearerToken);

        var response = await httpClient.SendAsync(request);

        var responseBody = await response.Content.ReadAsStringAsync();

        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine($"External API call failed: {response.StatusCode}, Body: {responseBody}");
        }
        else
        {
            Console.WriteLine($"External API call succeeded: {responseBody}");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception during external API call: {ex.Message}");
        // Log but don't throw to prevent disrupting the main flow
    }
}

And in the AddItemAsync method, change:

await NotifySlackAsync(productId);
CallExternalApiAsync(); // Not awaited!

To:

await NotifySlackAsync(productId);
await CallExternalApiAsync(); // Properly awaited

Expected Outcome

This fix should prevent unhandled exceptions from the HTTP client calls and ensure that all async operations are properly awaited, which should resolve the errors seen in the logs and improve the reliability of the cartservice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions