From 94b1b40193a2d618171faafa45dd479a1682f4ed Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 16 Feb 2023 11:54:01 -0800 Subject: [PATCH 1/2] compose: Show error alert instead of toast for file-upload failure When, next, we start giving more specific information on these failures, we'll prefer this persistent format to the transient toast, so the user has time to read the whole thing. --- src/compose/ComposeBox.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compose/ComposeBox.js b/src/compose/ComposeBox.js index fe7d13e1f3f..22bb10c5c8f 100644 --- a/src/compose/ComposeBox.js +++ b/src/compose/ComposeBox.js @@ -33,7 +33,7 @@ import { TranslationContext } from '../boot/TranslationProvider'; import { draftUpdate, sendTypingStart, sendTypingStop } from '../actions'; import Touchable from '../common/Touchable'; import Input from '../common/Input'; -import { showToast, showErrorAlert } from '../utils/info'; +import { showErrorAlert } from '../utils/info'; import { IconDone, IconSend } from '../common/Icons'; import { isConversationNarrow, @@ -340,7 +340,7 @@ const ComposeBox: React$AbstractComponent = forwardRef( try { response = await api.uploadFile(auth, attachments[i].url, fileName); } catch { - showToast(_('Failed to upload file: {fileName}', { fileName })); + showErrorAlert(_('Failed to upload file: {fileName}', { fileName })); setMessageInputValue(state => state.value.replace( placeholder, From 9fb6a5030fc0e58f881890fe9f6ef7dd17ce6eda Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 16 Feb 2023 12:22:41 -0800 Subject: [PATCH 2/2] ComposeBox: Show more information on file-upload errors Related: #5660 --- src/compose/ComposeBox.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/compose/ComposeBox.js b/src/compose/ComposeBox.js index 22bb10c5c8f..da6d86014c3 100644 --- a/src/compose/ComposeBox.js +++ b/src/compose/ComposeBox.js @@ -73,6 +73,7 @@ import { tryFetch } from '../message/fetchActions'; import { getMessageUrl } from '../utils/internalLinks'; import * as logging from '../utils/logging'; import type { Attachment } from './ComposeMenu'; +import { ApiError, RequestError } from '../api/apiErrors'; /* eslint-disable no-shadow */ @@ -339,8 +340,27 @@ const ComposeBox: React$AbstractComponent = forwardRef( let response = null; try { response = await api.uploadFile(auth, attachments[i].url, fileName); - } catch { - showErrorAlert(_('Failed to upload file: {fileName}', { fileName })); + } catch (errorIllTyped) { + const error: mixed = errorIllTyped; // https://github.com/facebook/flow/issues/2470 + + if (!(error instanceof Error)) { + logging.error('ComposeBox: Unexpected non-error thrown'); + } + + let msg = undefined; + if ( + error instanceof RequestError + && error.httpStatus === 413 // 413 Payload Too Large: + // https://github.com/zulip/zulip-mobile/issues/5148#issuecomment-1092140960 + ) { + msg = _('The server said the file is too large.'); + } else if (error instanceof ApiError) { + msg = _('The server said:\n\n{errorMessage}', { errorMessage: error.message }); + } else if (error instanceof Error && error.message.length > 0) { + msg = error.message; + } + showErrorAlert(_('Failed to upload file: {fileName}', { fileName }), msg); + setMessageInputValue(state => state.value.replace( placeholder,