From 625314d1671af05f7478148cc8f88df7caec8cc8 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 24 Nov 2025 21:11:21 -0500 Subject: [PATCH] Refactor: Add structured error logging to TransactionAICategorizer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add ILogger for proper error logging - Differentiate between HTTP, JSON parsing, and general errors - Log errors with descriptive messages for debugging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- MoneyMap/Services/TransactionAICategorizer.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/MoneyMap/Services/TransactionAICategorizer.cs b/MoneyMap/Services/TransactionAICategorizer.cs index c913106..4e57b0b 100644 --- a/MoneyMap/Services/TransactionAICategorizer.cs +++ b/MoneyMap/Services/TransactionAICategorizer.cs @@ -19,12 +19,18 @@ public class TransactionAICategorizer : ITransactionAICategorizer private readonly HttpClient _httpClient; private readonly MoneyMapContext _db; private readonly IConfiguration _config; + private readonly ILogger _logger; - public TransactionAICategorizer(HttpClient httpClient, MoneyMapContext db, IConfiguration config) + public TransactionAICategorizer( + HttpClient httpClient, + MoneyMapContext db, + IConfiguration config, + ILogger logger) { _httpClient = httpClient; _db = db; _config = config; + _logger = logger; } public async Task ProposeCategorizationAsync(Transaction transaction) @@ -205,8 +211,19 @@ Return ONLY valid JSON, no additional text."; return result; } - catch + catch (HttpRequestException ex) { + _logger.LogError(ex, "OpenAI API request failed: {Message}", ex.Message); + return null; + } + catch (JsonException ex) + { + _logger.LogError(ex, "Failed to parse OpenAI response JSON: {Message}", ex.Message); + return null; + } + catch (Exception ex) + { + _logger.LogError(ex, "Unexpected error calling OpenAI API: {Message}", ex.Message); return null; } }