mirror of
https://github.com/block/buzz.git
synced 2026-11-27 19:52:31 +00:00
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>
134 lines
4.1 KiB
Dart
134 lines
4.1 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:buzz/shared/relay/relay_http_query_client.dart';
|
|
import 'package:buzz/shared/relay/relay_query_response.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
|
|
void main() {
|
|
test('the default transport honors http.runWithClient', () async {
|
|
final requests = <http.Request>[];
|
|
final response = await http.runWithClient(
|
|
() => RelayHttpQueryClient().post(
|
|
Uri.parse('https://relay.example/query'),
|
|
headers: const {'Content-Type': 'application/json'},
|
|
body: const [],
|
|
timeout: const Duration(seconds: 5),
|
|
),
|
|
() => MockClient((request) async {
|
|
requests.add(request);
|
|
return http.Response('[]', 200);
|
|
}),
|
|
);
|
|
|
|
expect(response.body, '[]');
|
|
expect(requests.single.headers['Accept-Encoding'], 'gzip');
|
|
});
|
|
|
|
test('the default transport leaves a gzip body for the decoder', () async {
|
|
final compressed = gzip.encode(utf8.encode('[]'));
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
addTearDown(() => server.close(force: true));
|
|
server.listen((request) async {
|
|
request.response
|
|
..headers.set(HttpHeaders.contentEncodingHeader, 'gzip')
|
|
..headers.contentType = ContentType.json
|
|
..add(compressed);
|
|
await request.response.close();
|
|
});
|
|
|
|
final client = RelayHttpQueryClient();
|
|
addTearDown(client.close);
|
|
final response = await client.post(
|
|
Uri.parse('http://127.0.0.1:${server.port}/query'),
|
|
headers: const {'Content-Type': 'application/json'},
|
|
body: const [],
|
|
timeout: const Duration(seconds: 5),
|
|
);
|
|
|
|
expect(response.bodyBytes, compressed);
|
|
expect(
|
|
relayResponseText(response.bodyBytes, response.headers['content-type']),
|
|
'[]',
|
|
);
|
|
});
|
|
|
|
test('a timed-out request is aborted, not left running', () async {
|
|
final aborted = Completer<void>();
|
|
final client = RelayHttpQueryClient(
|
|
client: _HangingClient(onAbort: aborted.complete),
|
|
);
|
|
|
|
await expectLater(
|
|
client.post(
|
|
Uri.parse('https://relay.example/query'),
|
|
headers: const {},
|
|
body: const [],
|
|
timeout: const Duration(milliseconds: 10),
|
|
),
|
|
throwsA(isA<TimeoutException>()),
|
|
);
|
|
await aborted.future.timeout(const Duration(seconds: 5));
|
|
});
|
|
|
|
test('a timed-out bounded request is aborted, not left running', () async {
|
|
final aborted = Completer<void>();
|
|
final client = RelayHttpQueryClient(
|
|
client: _HangingClient(onAbort: aborted.complete),
|
|
);
|
|
|
|
await expectLater(
|
|
client.sendBounded(
|
|
http.Request('GET', Uri.parse('https://relay.example/')),
|
|
maxBytes: 1024,
|
|
timeout: const Duration(milliseconds: 10),
|
|
call: null,
|
|
),
|
|
throwsA(isA<TimeoutException>()),
|
|
);
|
|
await aborted.future.timeout(const Duration(seconds: 5));
|
|
});
|
|
|
|
test('an oversized bounded response is aborted before it is read', () async {
|
|
Future<void>? aborted;
|
|
final client = RelayHttpQueryClient(
|
|
client: MockClient.streaming((request, _) async {
|
|
aborted = (request as http.Abortable).abortTrigger;
|
|
return http.StreamedResponse(
|
|
StreamController<List<int>>().stream,
|
|
200,
|
|
contentLength: 4096,
|
|
);
|
|
}),
|
|
);
|
|
|
|
await expectLater(
|
|
client.sendBounded(
|
|
http.Request('GET', Uri.parse('https://relay.example/')),
|
|
maxBytes: 1024,
|
|
timeout: const Duration(seconds: 5),
|
|
call: null,
|
|
),
|
|
throwsA(isA<FormatException>()),
|
|
);
|
|
await aborted!.timeout(const Duration(seconds: 1));
|
|
});
|
|
}
|
|
|
|
/// Never responds; reports when the request's abort trigger fires.
|
|
class _HangingClient extends http.BaseClient {
|
|
_HangingClient({required this.onAbort});
|
|
|
|
final void Function() onAbort;
|
|
|
|
@override
|
|
Future<http.StreamedResponse> send(http.BaseRequest request) {
|
|
final trigger = (request as http.Abortable).abortTrigger;
|
|
trigger?.then((_) => onAbort());
|
|
return Completer<http.StreamedResponse>().future;
|
|
}
|
|
}
|