fix(chat): read DioException status from response #102
DioException has no statusCode getter, so formatting Ollama chat errors threw NoSuchMethodError. Read it from response.statusCode instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
abdulmominsakib committed
Sep 28, 2026 at 09:44 UTC
69378ad194b8e48c1338cc7a38bd09ab1802c03a
2 files changed
+29
-1
lib/features/chat/data/chat_error_formatter.dart
+11
-1
@@ -34,7 +34,8 @@ class ChatErrorFormatter {
34
final errorType = _readErrorType(error);
35
36
if (errorType != null) {
37
- statusCode = errorType.statusCode;
37
+ // DioException exposes the status only via `response.statusCode`.
38
+ statusCode = _readStatusCode(errorType);
39
final data = errorType.response?.data;
40
if (data != null) {
41
if (readBody != null) {
@@ -276,6 +277,15 @@ class ChatErrorFormatter {
277
return null;
278
}
279
280
+ static int? _readStatusCode(dynamic error) {
281
+ try {
282
+ final code = (error as dynamic).response?.statusCode;
283
+ return code is int ? code : null;
284
+ } catch (_) {
285
+ return null;
286
+ }
287
+ }
288
+
289
static String? _dioTypeLabel(dynamic error) {
290
try {
291
final dyn = error as dynamic;
test/features/chat/data/chat_error_formatter_test.dart
+18
@@ -1,3 +1,4 @@
1
+import 'package:dio/dio.dart';
2
import 'package:flutter_test/flutter_test.dart';
3
import 'package:localmind/features/chat/data/chat_api_error.dart';
4
import 'package:localmind/features/chat/data/chat_error_formatter.dart';
@@ -52,5 +53,22 @@ void main() {
53
final parsed = ChatApiError.tryParse(formatted)!;
54
expect(parsed.message.toLowerCase(), contains('ollama'));
55
});
56
+
57
+ test(
58
+ 'formatDioException reads status from DioException response (#102)',
59
+ () async {
60
+ final error = DioException(
61
+ requestOptions: RequestOptions(path: '/api/chat'),
62
+ response: Response(
63
+ requestOptions: RequestOptions(path: '/api/chat'),
64
+ statusCode: 429,
65
+ ),
66
+ type: DioExceptionType.badResponse,
67
+ );
68
+ final formatted = await ChatErrorFormatter.formatDioException(error);
69
+ final parsed = ChatApiError.tryParse(formatted)!;
70
+ expect(parsed.type, 'rate_limited');
71
+ },
72
+ );
73
});
74
}