Files
buzz/mobile/lib/shared/hosted/bounded_http_response.dart
Diem Nguyen c18bed1f46 fix(mobile): name the cause of account-unavailable sign-in errors (#9628)
## Problem

Organization sign-in can fail with `Couldn't sign in
(account-unavailable)`. That code covers several different failures, so
a screenshot can't tell them apart:

- the account server can't be reached at all (DNS, connection, or TLS
failure, or a timeout), for example when a required VPN profile isn't
applied to the app;
- a redirect, since the client doesn't follow redirects, so an
access-proxy login page lands here;
- a 5xx from the server.

## Change

- `BuilderlabAccountClient` records why a request failed as
`unavailable`, in `lastUnavailableCause`, and logs it. It follows the
existing `lastRejectedRequest` pattern. The cause is one of:
- a transport category: `unreachable`, `insecure`, `timedOut` (reusing
`transportFailure`), or `network` as the fallback;
  - the status code, such as `http302`.
- `OrganizationSignIn` saves the cause at the moment the exchange fails,
so later account requests can't change it. It adds the cause to the
failure code, for example `account-unavailable-http302` or
`account-unavailable-unreachable`. Each new attempt clears it.

- The organization sign-in page now ends its failure message with advice
for that cause instead of "Try again.":
- unreachable, timeout, TLS failure, redirect: "Check your network or
VPN connection, then try again."
  - a 5xx: "The sign-in service is unavailable. Try again later."
- The in-app banner keeps "Try again.": its capsule clips longer
messages at default text size, and the existing banner layout test
caught that.
- Unexpected statuses are classified as soon as the headers arrive, so a
slow or oversized error body can't hide the status.

| Before | After |
|---|---|
|
![Before](https://github.com/user-attachments/assets/17a2a3ac-a488-46b3-9995-1bd95a529f60)
|
![After](https://github.com/user-attachments/assets/3813d1e9-7dc4-4726-9f40-ace57ed12d7a)
|

The log line contains only the host, route, and cause. It has no
headers, credentials, or response bodies.

## Validation

- A new journey test in `organization_sign_in_test.dart` covers an
exchange that fails with a 302, a 503, and a client exception, then a
successful retry. With the cause capture removed, the test fails
(`Expected: 'account-unavailable-http302'`, `Actual:
'account-unavailable'`).
- The same test checks the advice for each cause. The internal-build
landing tests pass with `--dart-define=BUZZ_DISTRIBUTION=internal`.
- The screenshots come from the real `PairingPage` rendered in a scratch
widget test with a 390×844 surface, cropped. The scratch test isn't
committed.
- `just mobile-check` passes, and the full `flutter test` run passes:
+5018, 71 skipped.
- Not yet done: a device run that reproduces an unreachable account
server and captures the new message on screen.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Diem Nguyen <diem@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:34:49 -08:00

63 lines
1.9 KiB
Dart

import 'dart:async';
import 'package:http/http.dart' as http;
/// One deadline covers headers and the bounded body. Abort only this request,
/// leaving the shared client usable for sign-out and subsequent attempts.
/// [onHeaders] sees the response as soon as its headers arrive, before the
/// body is read, even if reading the body then fails.
Future<http.Response> boundedHttpResponse(
http.Client client,
http.Request source, {
int maxBytes = 65536,
void Function(http.BaseResponse headers)? onHeaders,
}) async {
final abort = Completer<void>();
final request =
http.AbortableRequest(
source.method,
source.url,
abortTrigger: abort.future,
)
..followRedirects = false
..headers.addAll(source.headers)
..bodyBytes = source.bodyBytes;
StreamIterator<List<int>>? body;
Future<http.Response> read() async {
final response = await client.send(request);
try {
onHeaders?.call(response);
} catch (_) {
// Some clients hold the connection until the body is listened to and
// cancelled; an abort alone leaves it open.
unawaited(response.stream.listen(null).cancel());
rethrow;
}
final iterator = body = StreamIterator(response.stream);
if (abort.isCompleted) {
await iterator.cancel();
throw http.RequestAbortedException();
}
final bytes = <int>[];
while (await iterator.moveNext()) {
if (bytes.length + iterator.current.length > maxBytes) {
throw const FormatException('Response too large');
}
bytes.addAll(iterator.current);
}
return http.Response.bytes(
bytes,
response.statusCode,
headers: response.headers,
);
}
try {
return await read().timeout(const Duration(seconds: 30));
} finally {
abort.complete();
// Do not let transport cleanup hold the account's serial queue hostage.
unawaited(body?.cancel());
}
}