Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mobile/lib/controllers/chat/active_chat_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ class ActiveChatController extends ChangeNotifier {
);
return null;
} catch (e) {
debugPrint("Edit message failed: $e");
return ApiService.errorMessage(e, fallback: "Couldn't edit the message.");
}
}
Expand Down
5 changes: 5 additions & 0 deletions mobile/lib/services/api_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class ApiService {
final message = data['error'] as String;
return message[0].toUpperCase() + message.substring(1);
}
// A bare 404/405 (not our JSON envelope) means the route doesn't exist,
// i.e. the server is older than the app.
if ((status == 404 || status == 405) && data is! Map) {
return 'This server doesn\'t support that yet. It may need updating.';
}
switch (error.type) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.receiveTimeout:
Expand Down
39 changes: 39 additions & 0 deletions mobile/test/api_error_message_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mobile/services/api_services.dart';

DioException _error(int status, dynamic data) {
final options = RequestOptions(path: '/messages/1');
return DioException(
requestOptions: options,
type: DioExceptionType.badResponse,
response: Response(requestOptions: options, statusCode: status, data: data),
);
}

void main() {
group('ApiService.errorMessage', () {
test('prefers the server error field', () {
final message = ApiService.errorMessage(
_error(404, {'success': false, 'error': 'message not found'}),
);
expect(message, 'Message not found');
});

test('explains a missing route on an older server', () {
final message = ApiService.errorMessage(
_error(404, '404 page not found'),
fallback: "Couldn't edit the message.",
);
expect(message, contains('may need updating'));
});

test('falls back for other failures', () {
final message = ApiService.errorMessage(
_error(500, 'boom'),
fallback: "Couldn't edit the message.",
);
expect(message, "Couldn't edit the message.");
});
});
}
Loading