/// The reason text carried by a failed request, without runtime diagnostics. /// /// The gateway fails pending requests with `StateError('Connection closed')`, /// whose `toString()` is `Bad state: Connection closed`. The browser shows the /// reason itself, never a Dart diagnostic, so those prefixes are stripped while /// deliberate protocol/refusal messages stay untouched. [fallback] is the /// honest sentence for a failure that carries no text at all. String describeErrorReason(Object error, {required String fallback}) { final raw = switch (error) { StateError(:final message) => message.toString(), _ => '$error', }; var message = raw.trim(); for (final prefix in _runtimePrefixes) { if (message.startsWith(prefix)) { message = message.substring(prefix.length).trim(); } } return message.isEmpty ? fallback : message; } /// Dart runtime prefixes that never belong in operator-facing copy. const _runtimePrefixes = ['Bad state:', 'Exception:'];