Files
buzz/mobile/lib/shared/relay/relay_call_error.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

109 lines
3.0 KiB
Dart

import 'dart:async';
/// The relay call an error came from.
enum RelayCall {
/// An EVENT sent over the socket and acknowledged with OK.
publish,
/// A one-shot query over the relay's HTTP bridge.
query,
/// A one-shot REQ over the socket, read until EOSE.
history,
/// A live REQ, up to its first EOSE.
subscribe,
}
/// The reason class of a failed relay call: the relay's own reason prefix, or
/// a timeout. Never the reason text, which is free-form and can quote what
/// the app sent.
enum RelayReason {
invalid('invalid'),
rateLimited('rate-limited'),
restricted('restricted'),
authRequired('auth-required'),
blocked('blocked'),
duplicate('duplicate'),
pow('pow'),
unsupported('unsupported'),
error('error'),
/// The call timed out here, before the relay answered.
timeout('timeout'),
/// A reason with no known prefix.
other('other');
const RelayReason(this.tag);
/// The wording that leaves the device.
final String tag;
static final _prefixes = {
for (final reason in values)
if (reason != timeout && reason != other) reason.tag: reason,
};
/// The class of a relay `OK` or `CLOSED` [message]: its prefix, up to the
/// first colon, when that is one of the relay's.
static RelayReason fromMessage(String message) {
final colon = message.indexOf(':');
if (colon < 0) return other;
return _prefixes[message.substring(0, colon).trim().toLowerCase()] ?? other;
}
/// The class of an HTTP error [status] from the relay's `/query`. Its body
/// is not read: the status alone says rate limit, refusal or server error.
static RelayReason fromHttpStatus(int status) => switch (status) {
429 => rateLimited,
401 => authRequired,
403 => restricted,
>= 500 && < 600 => error,
_ => other,
};
}
/// An error from a relay call that says which call and why.
///
/// Diagnostic reports of these carry [call] and [reason] as tags, because the
/// error's own text is removed before a report leaves the device.
abstract interface class RelayCallError implements Exception {
RelayCall get call;
RelayReason get reason;
}
/// A relay call timed out before the relay answered.
class RelayTimeoutException extends TimeoutException implements RelayCallError {
RelayTimeoutException(this.call, String message, [Duration? duration])
: super(message, duration);
@override
final RelayCall call;
@override
RelayReason get reason => RelayReason.timeout;
/// The text without the duration, which [message] already states. Screens
/// show this text.
@override
String toString() => 'TimeoutException: $message';
}
/// The relay closed a history or live subscription with [message].
class RelayClosedException implements RelayCallError {
RelayClosedException(this.call, this.message);
@override
final RelayCall call;
/// The relay's `CLOSED` reason. Callers read its text.
final String message;
@override
RelayReason get reason => RelayReason.fromMessage(message);
@override
String toString() => 'Exception: $message';
}