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; } }