Skip to content

Commit 47b00c0

Browse files
browstudio
andauthored
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>
1 parent 58695fe commit 47b00c0

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎mobile/lib/features/channels/channel_management_provider.dart‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,24 @@ export 'channel_metadata_updates.dart';
2424
part 'channel_huddle_actions.dart';
2525
part 'channel_management_actions.dart';
2626

27-
String _relayErrorMessage(Object error) =>
28-
error.toString().replaceFirst('Exception: ', '');
27+
/// The text of [error] without the `Exception: ` a plain [Exception] adds.
28+
/// A relay rejection keeps its message verbatim, and a lost acknowledgement
29+
/// drops the prefix only from its cause. Named types keep their whole name:
30+
/// `TimeoutException: ...` stays as is.
31+
String _relayErrorMessage(Object error) {
32+
if (error is RelayEventRejectedException) return error.message;
33+
final text = error.toString();
34+
final cause = error is RelayAckLostError ? error.cause : null;
35+
if (cause != null) {
36+
final causeText = cause.toString();
37+
if (text.endsWith(causeText)) {
38+
return text.substring(0, text.length - causeText.length) +
39+
_relayErrorMessage(cause);
40+
}
41+
}
42+
const prefix = 'Exception: ';
43+
return text.startsWith(prefix) ? text.substring(prefix.length) : text;
44+
}
2945

3046
/// Raised when one or more kind:9000 adds were rejected, keyed by pubkey.
3147
///

‎mobile/test/features/channels/channel_management_provider_test.dart‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,51 @@ void main() {
489489
},
490490
);
491491

492+
test('add-members failures keep each error name but drop a bare '
493+
'Exception prefix', () async {
494+
final keys = nostr.Keys.generate();
495+
final timedOut = 'a' * 64;
496+
final rejected = 'b' * 64;
497+
final lost = 'c' * 64;
498+
final session = _RecordingPublishRelaySession()
499+
..publishErrors.addAll({
500+
timedOut: RelayAckTimeoutError('Event e not acknowledged'),
501+
rejected: const RelayEventRejectedException(
502+
'restricted: Exception: sponsor approval required',
503+
),
504+
lost: RelayAckLostError(Exception('App moved to background')),
505+
});
506+
final actionsProvider = Provider<ChannelActions>((ref) {
507+
return ChannelActions(
508+
ref: ref,
509+
session: session,
510+
signedEventRelay: SignedEventRelay(session: session, nsec: keys.nsec),
511+
currentPubkey: keys.public,
512+
);
513+
});
514+
final container = ProviderContainer(retry: (_, _) => null);
515+
addTearDown(container.dispose);
516+
517+
await expectLater(
518+
container
519+
.read(actionsProvider)
520+
.addMembers(
521+
channelId: _channelId,
522+
pubkeys: [timedOut, rejected, lost],
523+
role: 'member',
524+
),
525+
throwsA(
526+
isA<AddMembersException>().having((e) => e.failures, 'failures', {
527+
timedOut: 'TimeoutException: Event e not acknowledged',
528+
rejected: 'restricted: Exception: sponsor approval required',
529+
lost:
530+
'Connection lost before the relay acknowledged the event: '
531+
'App moved to background',
532+
}),
533+
),
534+
);
535+
});
536+
492537
test('reactions tag the reacted-to message author', () async {
493538
final keys = nostr.Keys.generate();
494539
final session = _RecordingPublishRelaySession();
@@ -945,6 +990,9 @@ const _memberPubkey =
945990
class _RecordingPublishRelaySession extends RelaySessionNotifier {
946991
final publishedEvents = <NostrEvent>[];
947992
final queryFilters = <NostrFilter>[];
993+
994+
/// Errors to throw instead of publishing, keyed by the event's `p` tag.
995+
final publishErrors = <String, Object>{};
948996
int historyQueryCount = 0;
949997

950998
@override
@@ -956,6 +1004,12 @@ class _RecordingPublishRelaySession extends RelaySessionNotifier {
9561004
Duration timeout = const Duration(seconds: 8),
9571005
Future<bool> Function()? mayPublish,
9581006
}) async {
1007+
final target = event.tags
1008+
.where((tag) => tag.length > 1 && tag[0] == 'p')
1009+
.map((tag) => tag[1])
1010+
.firstOrNull;
1011+
final error = publishErrors[target];
1012+
if (error != null) throw error;
9591013
publishedEvents.add(event);
9601014
return event;
9611015
}

0 commit comments

Comments
 (0)