fix(mobile): keep error names in Add Members failure text (#9834)

## 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 <id> not
acknowledged within …" instead of "TimeoutException: Event <id> 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 <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
This commit is contained in:
Tom Brow
2026-11-20 14:48:58 -08:00
committed by GitHub
parent 58695fe376
commit 47b00c0b8b
2 changed files with 72 additions and 2 deletions

View File

@@ -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.
///

View File

@@ -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<ChannelActions>((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<AddMembersException>().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 = <NostrEvent>[];
final queryFilters = <NostrFilter>[];
/// Errors to throw instead of publishing, keyed by the event's `p` tag.
final publishErrors = <String, Object>{};
int historyQueryCount = 0;
@override
@@ -956,6 +1004,12 @@ class _RecordingPublishRelaySession extends RelaySessionNotifier {
Duration timeout = const Duration(seconds: 8),
Future<bool> 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;
}