From 47b00c0b8b20042b6df055c070d0cb52820b6543 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 14:48:58 -0800 Subject: [PATCH] fix(mobile): keep error names in Add Members failure text (#9834) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary When adding members fails, the Add Members sheet shows each member's error. The text came from `error.toString().replaceFirst('Exception: ', '')`, which removes the first `Exception: ` anywhere in the text, not just a leading one. So a relay ACK timeout read "TimeoutEvent not acknowledged within …" instead of "TimeoutException: Event not acknowledged within …". Now a plain `Exception: ` prefix is removed only where it starts the text or follows a wrapping error's `: `. A relay rejection still shows as "restricted: …", and an add lost when the app goes to the background reads "Connection lost before the relay acknowledged the event: App moved to background" without a nested "Exception: ". Named types such as `TimeoutException` keep their whole name. ## Validation - New row in `channel_management_provider_test.dart`: `addMembers` with one member whose add times out, one the relay rejects and one lost when the app goes to the background. It checks each text in the `AddMembersException`. No screenshots: the change only affects the error text after a failed add, which can't be triggered on demand against a real relay. Full-app run on an iOS simulator (signed in, healthy relay): cold start and session restore, channel list, opening channels and a DM with history scrolling, channel details and the members list, Add Members (picker, search, cancel), Settings, and background then foreground. All passed with no new errors in the device log. --------- Signed-off-by: Tom Brow Co-authored-by: studio --- .../channels/channel_management_provider.dart | 20 ++++++- .../channel_management_provider_test.dart | 54 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/mobile/lib/features/channels/channel_management_provider.dart b/mobile/lib/features/channels/channel_management_provider.dart index 8e4398ee82..718cf0eab0 100644 --- a/mobile/lib/features/channels/channel_management_provider.dart +++ b/mobile/lib/features/channels/channel_management_provider.dart @@ -24,8 +24,24 @@ export 'channel_metadata_updates.dart'; part 'channel_huddle_actions.dart'; part 'channel_management_actions.dart'; -String _relayErrorMessage(Object error) => - error.toString().replaceFirst('Exception: ', ''); +/// The text of [error] without the `Exception: ` a plain [Exception] adds. +/// A relay rejection keeps its message verbatim, and a lost acknowledgement +/// drops the prefix only from its cause. Named types keep their whole name: +/// `TimeoutException: ...` stays as is. +String _relayErrorMessage(Object error) { + if (error is RelayEventRejectedException) return error.message; + final text = error.toString(); + final cause = error is RelayAckLostError ? error.cause : null; + if (cause != null) { + final causeText = cause.toString(); + if (text.endsWith(causeText)) { + return text.substring(0, text.length - causeText.length) + + _relayErrorMessage(cause); + } + } + const prefix = 'Exception: '; + return text.startsWith(prefix) ? text.substring(prefix.length) : text; +} /// Raised when one or more kind:9000 adds were rejected, keyed by pubkey. /// diff --git a/mobile/test/features/channels/channel_management_provider_test.dart b/mobile/test/features/channels/channel_management_provider_test.dart index 6598681c22..1827149be5 100644 --- a/mobile/test/features/channels/channel_management_provider_test.dart +++ b/mobile/test/features/channels/channel_management_provider_test.dart @@ -489,6 +489,51 @@ void main() { }, ); + test('add-members failures keep each error name but drop a bare ' + 'Exception prefix', () async { + final keys = nostr.Keys.generate(); + final timedOut = 'a' * 64; + final rejected = 'b' * 64; + final lost = 'c' * 64; + final session = _RecordingPublishRelaySession() + ..publishErrors.addAll({ + timedOut: RelayAckTimeoutError('Event e not acknowledged'), + rejected: const RelayEventRejectedException( + 'restricted: Exception: sponsor approval required', + ), + lost: RelayAckLostError(Exception('App moved to background')), + }); + final actionsProvider = Provider((ref) { + return ChannelActions( + ref: ref, + session: session, + signedEventRelay: SignedEventRelay(session: session, nsec: keys.nsec), + currentPubkey: keys.public, + ); + }); + final container = ProviderContainer(retry: (_, _) => null); + addTearDown(container.dispose); + + await expectLater( + container + .read(actionsProvider) + .addMembers( + channelId: _channelId, + pubkeys: [timedOut, rejected, lost], + role: 'member', + ), + throwsA( + isA().having((e) => e.failures, 'failures', { + timedOut: 'TimeoutException: Event e not acknowledged', + rejected: 'restricted: Exception: sponsor approval required', + lost: + 'Connection lost before the relay acknowledged the event: ' + 'App moved to background', + }), + ), + ); + }); + test('reactions tag the reacted-to message author', () async { final keys = nostr.Keys.generate(); final session = _RecordingPublishRelaySession(); @@ -945,6 +990,9 @@ const _memberPubkey = class _RecordingPublishRelaySession extends RelaySessionNotifier { final publishedEvents = []; final queryFilters = []; + + /// Errors to throw instead of publishing, keyed by the event's `p` tag. + final publishErrors = {}; int historyQueryCount = 0; @override @@ -956,6 +1004,12 @@ class _RecordingPublishRelaySession extends RelaySessionNotifier { Duration timeout = const Duration(seconds: 8), Future Function()? mayPublish, }) async { + final target = event.tags + .where((tag) => tag.length > 1 && tag[0] == 'p') + .map((tag) => tag[1]) + .firstOrNull; + final error = publishErrors[target]; + if (error != null) throw error; publishedEvents.add(event); return event; }