Refactor: Add structured error logging to TransactionAICategorizer

- 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 <noreply@anthropic.com>
This commit is contained in:
2025-11-24 21:11:21 -05:00
parent d9e883a9f3
commit 625314d167

View File

@@ -19,12 +19,18 @@ public class TransactionAICategorizer : ITransactionAICategorizer
private readonly HttpClient _httpClient;
private readonly MoneyMapContext _db;
private readonly IConfiguration _config;
private readonly ILogger<TransactionAICategorizer> _logger;
public TransactionAICategorizer(HttpClient httpClient, MoneyMapContext db, IConfiguration config)
public TransactionAICategorizer(
HttpClient httpClient,
MoneyMapContext db,
IConfiguration config,
ILogger<TransactionAICategorizer> logger)
{
_httpClient = httpClient;
_db = db;
_config = config;
_logger = logger;
}
public async Task<AICategoryProposal?> 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;
}
}