Files
buzz/mobile/lib/shared/diagnostics/diagnostic_startup.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

162 lines
6.4 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:uuid/uuid.dart';
import '../http/app_http_client.dart';
import 'diagnostic_filter.dart';
import 'diagnostic_reporter.dart';
import 'relay_error_tags.dart';
import 'sentry_diagnostic_reporter.dart';
/// The build's reporting destination, a Sentry DSN supplied at build time.
/// Builds made from this repository alone have none and report nothing.
const reportingDestination = String.fromEnvironment('BUZZ_SENTRY_DSN');
/// Runs [app] with the reporter this build calls for.
///
/// Without a [destination] the SDK is never initialized: a reporting library
/// given an empty destination can still install its hooks (ADR 0007). With
/// one, the SDK's own handlers capture native crashes, Flutter framework
/// errors and uncaught asynchronous Dart errors from then on. A destination
/// the SDK cannot parse is treated as none, so a configuration mistake never
/// stops the app from starting.
Future<void> runWithDiagnostics({
required String destination,
required FutureOr<void> Function(DiagnosticReporter reporter) app,
}) async {
if (!_isUsable(destination)) return app(const NoopDiagnosticReporter());
final installId = await diagnosticInstallId();
await SentryFlutter.init(
(options) => configureDiagnostics(options, destination),
appRunner: () {
// A scope tag also reaches the native SDKs, so native crashes and
// hangs carry it too. Counts carry scope attributes, not tags.
if (installId != null) {
Sentry.configureScope(
(scope) => scope
..setTag(DiagnosticFilter.installIdTag, installId)
..setAttributes({
DiagnosticFilter.installIdTag: SentryAttribute.string(installId),
}),
);
}
final reporter = SentryDiagnosticReporter();
// The shared HTTP client is made outside Riverpod; it counts to the same
// reporter, so its counts follow the member like everything else.
appHttpDiagnostics = reporter;
return app(reporter);
},
);
}
const _installIdFile = 'diagnostics_install_id';
/// A random identifier for this installation, so reports from one device can
/// be matched up across sign-ins and sign-outs. Created on first use and kept
/// in a file that device backups and transfers leave behind, so a phone
/// restored from another gets its own: on Android in the files directory,
/// excluded by the backup rules, and on iOS in an Application Support folder
/// the app marks as excluded from backup. Null if storage fails, since
/// reporting must never stop the app from starting.
Future<String?> diagnosticInstallId({
Future<Directory> Function()? directory,
}) async {
try {
final dir = await (directory ?? _installIdDirectory)();
final file = File('${dir.path}/$_installIdFile');
if (await file.exists()) {
// A failed read goes to the catch below and keeps the file: replacing it
// after a passing error would split this device's reports.
final bytes = await file.readAsBytes();
try {
final saved = utf8.decode(bytes).trim();
if (saved.isNotEmpty) return saved;
} on FormatException {
// Not text: a damaged file is replaced below, not a reason to send
// reports without an install ID.
}
}
final created = const Uuid().v4();
await dir.create(recursive: true);
await file.writeAsString(created, flush: true);
return created;
} on Object catch (error) {
debugPrint('[Diagnostics] no install ID: ${error.runtimeType}');
return null;
}
}
Future<Directory> _installIdDirectory() async {
if (!Platform.isIOS) return getApplicationSupportDirectory();
final path = await const MethodChannel(
'buzz/diagnostics',
).invokeMethod<String>('installIdDirectory');
if (path == null) throw StateError('no install ID directory');
return Directory(path);
}
bool _isUsable(String destination) {
if (destination.isEmpty) return false;
try {
final dsn = Dsn.parse(destination);
// The native SDKs also need an HTTP(S) host and a public key, which the
// Dart parser lets through; they would fail after Dart installs its
// handlers. The scheme is checked on the raw text because Dart lowercases
// it and Cocoa does not.
if (!(destination.startsWith('https://') ||
destination.startsWith('http://')) ||
(dsn.uri?.host ?? '').isEmpty ||
dsn.publicKey.isEmpty ||
dsn.projectId.isEmpty) {
throw const FormatException();
}
return true;
} on Object catch (error) {
// Dsn.parse throws ArgumentError or FormatException; the message would
// repeat the destination, so only the type is printed.
debugPrint(
'[Diagnostics] reporting off: unusable destination '
'(${error.runtimeType})',
);
return false;
}
}
/// Applies the reporting settings from `docs/mobile-diagnostic-reporting.md`.
void configureDiagnostics(SentryFlutterOptions options, String destination) {
options
..dsn = destination
..sendDefaultPii = false
..attachScreenshot = false
..attachViewHierarchy = false
// Breadcrumbs come only from the app's fixed list and the SDK's
// foreground/background entries; native ones can carry addresses.
..enableAutoNativeBreadcrumbs = false
// On iOS this is Cocoa's own network breadcrumbs, which keep each
// request's query and fragment and reach native crash reports, which
// never pass through the filter below.
..recordHttpBreadcrumbs = false
// Otherwise the SDK replaces debugPrint in non-debug builds with one that
// records a breadcrumb and prints nothing.
..enablePrintBreadcrumbs = false
..enableUserInteractionBreadcrumbs = false
..enableAppLifecycleBreadcrumbs = true
// Main-thread hangs, at the SDK's default thresholds. Dart runs on the
// main thread, so blocking Dart work is what these catch.
..enableAppHangTracking = true
..anrEnabled = true
..enableWatchdogTerminationTracking = true
..beforeSend = ((event, hint) =>
DiagnosticFilter.filterEvent(RelayErrorTags.apply(event)))
..beforeBreadcrumb = ((crumb, hint) =>
DiagnosticFilter.filterBreadcrumb(crumb))
..beforeSendMetric = ((metric, hint) =>
DiagnosticFilter.filterMetric(metric));
}