Files
buzz/mobile/test/shared/relay/relay_call_error_test.dart
Tom Brow 0966f3fad6 feat(mobile): tag escaped relay errors with their call and reason class (#9821)
Makes relay errors that nothing catches readable in diagnostic reports.
Today an unhandled relay rejection or timeout arrives with its text
removed and a stack that stops at the async error handler, so a report
can't say which call failed or why.

## Changes

- **Typed call and reason:** relay errors now carry the call (`publish`,
`query`, `history`, `subscribe`) and a fixed reason class taken from the
relay's reason prefix (`invalid`, `rate-limited`, `restricted`,
`auth-required`, `blocked`, `duplicate`, `pow`, `unsupported`, `error`,
`timeout`, or `other`). Types:
  - `RelayEventRejectedException` implements the new `RelayCallError`;
- new `RelayTimeoutException` extends `TimeoutException`, and
`RelayAckTimeoutError` now extends it;
- new `RelayClosedException` replaces the plain `Exception` for a closed
history or live subscription, with the same `toString`.
- new `RelayQueryException` (a `RelayException` that is also a
`RelayCallError`) for a non-2xx `/query` answer, with its reason from
the status alone;
- a one-shot signed event the relay refuses now fails with a typed
rejection, with the same text.
Existing `TimeoutException` checks and message helpers behave as before.
- **Report tags:** before the filter runs, a report whose error is a
`RelayCallError` gets `relay.call` and `relay.reason` tags. The relay's
own text is never sent. These reports are also grouped by call and
reason on top of the default grouping, so a rate limit and an invalid
request from the same code become separate issues.
- **Call-site stack:** pending publishes, one-shot signed events, and
history and live subscriptions keep the stack where they were created,
and errors completed later from timers or socket callbacks use it. A
report's stack now reaches the calling code.
- **Spec:** one bullet on the tags, the grouping and the stack.

## Validation

New tests:
- a table of reason prefixes to classes;
- error types, fields and the existing message helpers;
- relay session cases: a rejected publish's call, reason and calling
stack, the timeout types, and closed subscriptions;
- reports: tags, the grouping key, no relay text, and non-relay errors
untouched.
- production refusals through a serialized report: `/query` statuses,
snapshot `/query` versus discovery, and one-shot rejections and timeouts
with the caller's stack.


Full-app run on an iOS simulator (signed in, healthy relay): cold start
and session restore, channel list, opening channels and DMs, history
back to each channel's start, send, react, edit, thread reply, search,
channel details and Add Members, Settings > Send test report, and
background then foreground followed by a send and a delete. All passed
with no new errors in the device log, and no doubled error names in any
logged text. Error paths (timeouts, closed connections, relay
rejections) weren't triggered on the healthy relay; the unit tests above
cover them.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-24 16:29:14 -08:00

134 lines
4.5 KiB
Dart

import 'dart:async';
import 'package:buzz/shared/relay/relay.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('the reason class of a relay message', () {
const cases = {
'invalid: bad signature': RelayReason.invalid,
'rate-limited: quota exceeded; retry in 4s': RelayReason.rateLimited,
'restricted: access revoked': RelayReason.restricted,
'auth-required: verification failed': RelayReason.authRequired,
'blocked: community policy': RelayReason.blocked,
'duplicate: already have this event': RelayReason.duplicate,
'pow: insufficient work': RelayReason.pow,
'unsupported: filter extension': RelayReason.unsupported,
'error: query timed out': RelayReason.error,
'INVALID: shouted': RelayReason.invalid,
' invalid : padded': RelayReason.invalid,
'invalid:': RelayReason.invalid,
'timeout: not a relay prefix': RelayReason.other,
'other: not a relay prefix': RelayReason.other,
'teapot: unknown prefix': RelayReason.other,
'no prefix at all': RelayReason.other,
'a sentence, then invalid: later': RelayReason.other,
'': RelayReason.other,
};
for (final entry in cases.entries) {
test('"${entry.key}" is ${entry.value.name}', () {
expect(RelayReason.fromMessage(entry.key), entry.value);
});
}
test('the wording that leaves the device is the relay prefix', () {
expect(
{for (final reason in RelayReason.values) reason.tag},
{
'invalid',
'rate-limited',
'restricted',
'auth-required',
'blocked',
'duplicate',
'pow',
'unsupported',
'error',
'timeout',
'other',
},
);
});
});
group('the reason class of a /query status', () {
const cases = {
429: RelayReason.rateLimited,
401: RelayReason.authRequired,
403: RelayReason.restricted,
500: RelayReason.error,
503: RelayReason.error,
599: RelayReason.error,
400: RelayReason.other,
404: RelayReason.other,
302: RelayReason.other,
600: RelayReason.other,
};
for (final entry in cases.entries) {
test('${entry.key} is ${entry.value.name}', () {
expect(RelayReason.fromHttpStatus(entry.key), entry.value);
});
}
});
group('what a relay error says about itself', () {
test('a refused query is a relay exception that names the query', () {
final error = RelayQueryException(429, '{"error":"slow down"}');
expect(error, isA<RelayException>());
expect(error.call, RelayCall.query);
expect(error.reason, RelayReason.rateLimited);
expect(error.statusCode, 429);
expect(error.toString(), 'RelayException(429): {"error":"slow down"}');
});
test('a rejected publish names the publish and the relay prefix', () {
const error = RelayEventRejectedException('blocked: secret reason');
expect(error.call, RelayCall.publish);
expect(error.reason, RelayReason.blocked);
expect(error.toString(), 'Exception: blocked: secret reason');
});
test('a closed subscription names its call and keeps its text', () {
final error = RelayClosedException(
RelayCall.history,
'error: query timed out',
);
expect(error.call, RelayCall.history);
expect(error.reason, RelayReason.error);
expect(error.toString(), 'Exception: error: query timed out');
expect(isRelayDeadlineError(error), isTrue);
expect(relayErrorMessage(error), 'Query timed out');
});
test('an access refusal is still recognized as one', () {
final error = RelayClosedException(
RelayCall.subscribe,
'restricted: not a member',
);
expect(isRelayAccessDenied(error), isTrue);
});
test('a relay timeout is a timeout of its call', () {
final error = RelayTimeoutException(RelayCall.query, 'slow');
expect(error, isA<TimeoutException>());
expect(error.call, RelayCall.query);
expect(error.reason, RelayReason.timeout);
});
test('an unacknowledged publish is still a timeout, of the publish', () {
final error = RelayAckTimeoutError('no OK', const Duration(seconds: 8));
expect(error, isA<TimeoutException>());
expect(error, isA<RelayTimeoutException>());
expect(error.call, RelayCall.publish);
expect(error.reason, RelayReason.timeout);
expect(error.duration, const Duration(seconds: 8));
});
});
}