From 0281651f2bf705621ebe777a6d7275886a202e83 Mon Sep 17 00:00:00 2001 From: Suman Biswas Date: Sat, 7 Nov 2026 19:14:17 +0530 Subject: [PATCH] fix(mob): explain why editing a message failed When the server is older than the app, PUT /api/messages/{id} returns a plain-text 404, which ApiService.errorMessage turned into the generic "Couldn't edit the message." Treat a bare 404/405 as a missing route and say the server may need updating, and log the underlying error. Closes #160 --- .../chat/active_chat_controller.dart | 1 + mobile/lib/services/api_services.dart | 5 +++ mobile/test/api_error_message_test.dart | 39 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 mobile/test/api_error_message_test.dart diff --git a/mobile/lib/controllers/chat/active_chat_controller.dart b/mobile/lib/controllers/chat/active_chat_controller.dart index 5de488d..a2b47c8 100644 --- a/mobile/lib/controllers/chat/active_chat_controller.dart +++ b/mobile/lib/controllers/chat/active_chat_controller.dart @@ -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."); } } diff --git a/mobile/lib/services/api_services.dart b/mobile/lib/services/api_services.dart index 4515d40..b148e58 100644 --- a/mobile/lib/services/api_services.dart +++ b/mobile/lib/services/api_services.dart @@ -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: diff --git a/mobile/test/api_error_message_test.dart b/mobile/test/api_error_message_test.dart new file mode 100644 index 0000000..2ee9c6f --- /dev/null +++ b/mobile/test/api_error_message_test.dart @@ -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."); + }); + }); +}