feat: enhance error handling and parsing logic in importFile method for improved response management
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -61,7 +61,7 @@ class ImportRepository {
|
||||
final response = await http.Response.fromStream(streamed);
|
||||
developer.log('Received response with status: ${response.statusCode}', name: 'ImportRepository');
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
developer.log('Error response: ${response.body}', name: 'ImportRepository', error: 'HTTP Error');
|
||||
throw ApiException(
|
||||
type: _mapStatusCodeToErrorType(response.statusCode),
|
||||
@@ -71,19 +71,27 @@ class ImportRepository {
|
||||
}
|
||||
|
||||
final parsed = _parseResponse(response);
|
||||
developer.log('Response parsed, keys: ${parsed is Map ? parsed.keys.toList() : "list"}', name: 'ImportRepository');
|
||||
|
||||
// Check if the response is a ReceiptImportResult
|
||||
if (parsed is Map<String, dynamic>) {
|
||||
// Structured receipt items from /receipt-import
|
||||
if (parsed.containsKey('items')) {
|
||||
final items = (parsed['items'] as List?)?.map((e) => ParsedReceiptItem.fromJson(e)).toList();
|
||||
if (items != null) {
|
||||
developer.log('Successfully parsed ${items.length} items', name: 'ImportRepository');
|
||||
return items;
|
||||
}
|
||||
} else if (parsed.containsKey('markdown')) {
|
||||
// Handle the case where the response is a QuickImportResult
|
||||
developer.log('Successfully parsed markdown', name: 'ImportRepository');
|
||||
return [ParsedReceiptItem(rawName: parsed['markdown'], quantity: 1)];
|
||||
}
|
||||
// Markdown-based receipt from /quick-import fallback — parse lines into items
|
||||
if (parsed.containsKey('markdown')) {
|
||||
developer.log('Got markdown receipt, parsing lines into items', name: 'ImportRepository');
|
||||
final lines = (parsed['markdown'] as String).split('\n');
|
||||
final items = lines
|
||||
.where((l) => l.trim().isNotEmpty && !l.startsWith('#') && !l.startsWith('=') && !l.startsWith('-'))
|
||||
.map((l) => ParsedReceiptItem(rawName: l.trim()))
|
||||
.toList();
|
||||
developer.log('Extracted ${items.length} lines as items', name: 'ImportRepository');
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user