Skip to content
Merged
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
16 changes: 15 additions & 1 deletion src/WhatsApp/WhatsAppClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -15,6 +16,15 @@ namespace Devlooped.WhatsApp;
/// <param name="logger">A logger for messages.</param>
public class WhatsAppClient(IHttpClientFactory httpFactory, IOptions<MetaOptions> options, ILogger<WhatsAppClient> logger) : IWhatsAppClient
{
static readonly JsonSerializerOptions jsonOptions = new(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition =
System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault |
System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true,
};

readonly MetaOptions options = options.Value;

/// <summary>
Expand Down Expand Up @@ -55,7 +65,11 @@ public HttpClient CreateHttp(string numberId)

if (!result.IsSuccessStatusCode)
{
var error = JsonNode.Parse(await result.Content.ReadAsStringAsync())?.ToJsonString(new() { WriteIndented = true });
var error = JsonSerializer.Serialize(new
{
payload,
error = JsonNode.Parse(await result.Content.ReadAsStringAsync())
Copy link

Copilot AI Jun 13, 2025

Choose a reason for hiding this comment

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

Parsing the HTTP response content without handling parse errors could throw an exception if the content isn’t valid JSON and mask the original failure. Consider wrapping the parse call in a try/catch and falling back to the raw string on parse errors.

Copilot uses AI. Check for mistakes.
}, jsonOptions);
logger.LogError("Failed to send WhatsApp message from {From}: {Error}", numberId, error);
throw new HttpRequestException(error, null, result.StatusCode);
}
Expand Down