From 2ae8d8e54dcf25ce760206b0f6fc16923b4375b5 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Mon, 8 Jul 2024 16:57:22 -0700 Subject: [PATCH] outboxActions: Show dialog if message send fails with informative ApiError This is our minimal support for #5870 in this legacy codebase. Supporting it properly with a client-side check of the setting is more effort than we can spare here, because it requires implementing the group-based permissions system. Probably more error handling is called for in general (like for network or server issues), but #3881 ("Sending outbox messages is fraught with issues") is complicated and it's probably best to leave it be. Fixes: #5870 --- src/outbox/outboxActions.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/outbox/outboxActions.js b/src/outbox/outboxActions.js index ce7720c5553..b516301843b 100644 --- a/src/outbox/outboxActions.js +++ b/src/outbox/outboxActions.js @@ -3,6 +3,8 @@ import parseMarkdown from 'zulip-markdown-parser'; import invariant from 'invariant'; +import { ApiError } from '../api/apiErrors'; +import { showErrorAlert } from '../utils/info'; import * as logging from '../utils/logging'; import type { PerAccountState, @@ -79,14 +81,30 @@ const trySendMessages = (dispatch, getState): boolean => { // CSV, then a literal. To avoid misparsing, always use JSON. : JSON.stringify([streamNameOfStreamMessage(item)]); - await api.sendMessage(auth, { - type: item.type, - to, - subject: item.subject, - content: item.markdownContent, - localId: item.timestamp, - eventQueueId: state.session.eventQueueId ?? undefined, - }); + try { + await api.sendMessage(auth, { + type: item.type, + to, + subject: item.subject, + content: item.markdownContent, + localId: item.timestamp, + eventQueueId: state.session.eventQueueId ?? undefined, + }); + } catch (errorIllTyped) { + const error: mixed = errorIllTyped; // https://github.com/facebook/flow/issues/2470 + + if (error instanceof ApiError && error.message.length > 0) { + showErrorAlert( + // TODO(i18n) nontrivial to plumb through GetText; + // skip for now in this legacy codebase + 'Failed to send message', + // E.g., "You do not have permission to send direct messages to this recipient." + `The server at ${auth.realm.toString()} said:\n\n${error.message}`, + ); + dispatch(deleteOutboxMessage(item.id)); + return; + } + } dispatch(messageSendComplete(item.timestamp)); }); return true;