Skip to content

Commit

Permalink
compose box: On failed message send, show error dialog
Browse files Browse the repository at this point in the history
Fixes: zulip#815
  • Loading branch information
chrisbobbe committed Jul 17, 2024
1 parent 3f65665 commit c56963e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/widgets/compose_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
import 'package:image_picker/image_picker.dart';

import '../api/exception.dart';
import '../api/model/model.dart';
import '../api/route/messages.dart';
import '../model/compose.dart';
Expand Down Expand Up @@ -716,7 +717,7 @@ class _SendButtonState extends State<_SendButton> {
|| widget.contentController.hasValidationErrors.value;
}

void _send() {
void _send() async {
if (_hasValidationErrors) {
final zulipLocalizations = ZulipLocalizations.of(context);
List<String> validationErrorMessages = [
Expand All @@ -735,7 +736,23 @@ class _SendButtonState extends State<_SendButton> {

final store = PerAccountStoreWidget.of(context);
final content = widget.contentController.textNormalized;
store.sendMessage(destination: widget.getDestination(), content: content);

try {
// TODO(#720) put input(s) and send button into a disabled "working on it"
// state (allowing input text to be selected for copying).
await store.sendMessage(destination: widget.getDestination(), content: content);
} on ApiRequestException catch (e) {
if (!mounted) return;
final zulipLocalizations = ZulipLocalizations.of(context);
final message = switch (e) {
ZulipApiException() => zulipLocalizations.errorServerMessage(e.message),
_ => e.message,
};
showErrorDialog(context: context,
title: zulipLocalizations.errorMessageNotSent,
message: message);
return;
}

widget.contentController.clear();
}
Expand Down
19 changes: 19 additions & 0 deletions test/widgets/compose_box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import '../example_data.dart' as eg;
import '../flutter_checks.dart';
import '../model/binding.dart';
import '../stdlib_checks.dart';
import 'dialog_checks.dart';

void main() {
TestZulipBinding.ensureInitialized();
Expand Down Expand Up @@ -225,5 +226,23 @@ void main() {
final errorDialogs = tester.widgetList(find.byType(AlertDialog));
check(errorDialogs).isEmpty();
});

testWidgets('ZulipApiException', (tester) async {
await setupAndTapSend(tester, prepareResponse: (message) {
connection.prepare(
httpStatus: 400,
json: {
'result': 'error',
'code': 'BAD_REQUEST',
'msg': 'You do not have permission to initiate direct message conversations.',
});
});
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
await tester.tap(find.byWidget(checkErrorDialog(tester,
expectedTitle: zulipLocalizations.errorMessageNotSent,
expectedMessage: zulipLocalizations.errorServerMessage(
'You do not have permission to initiate direct message conversations.'),
)));
});
});
}

0 comments on commit c56963e

Please sign in to comment.