Say why an app update check found nothing

The check dropped every per-server failure and showed one generic
message. It now lists each machine with its reason (sign in first, a
feed key this app does not trust, a timeout), and an APK signed with
another certificate than the installed app is named plainly with how
to install it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 19:50:50 +00:00
parent 1535d20ca8
commit 71719b5707
3 changed files with 159 additions and 5 deletions
+67 -4
View File
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';
import '../../core/servers/server_catalog.dart';
@@ -171,6 +172,13 @@ Future<AppUpdate> newestServerUpdate(
Future<AppUpdate> Function(Uri, Map<String, String> Function(Uri))? read,
bool Function(SavedServer server, Uri uri)? stillTrusted,
}) async {
// Why each machine offered nothing, so a failed check says what to fix.
final problems = <String>[];
void note(SavedServer server, Object error) {
final line = '${server.name}: ${updateProblem(error)}';
if (!problems.contains(line)) problems.add(line);
}
final results = await Future.wait(
servers.map((server) async {
Map<String, String> Function(Uri) headers;
@@ -179,7 +187,8 @@ Future<AppUpdate> newestServerUpdate(
id: server.id,
endpoints: server.endpoints.map(Uri.parse),
);
} catch (_) {
} catch (error) {
note(server, error);
return null;
}
final snapshotHeaders = headers;
@@ -201,7 +210,8 @@ Future<AppUpdate> newestServerUpdate(
headers,
).timeout(const Duration(seconds: 15));
return update.fromServer(server.name, headers);
} catch (_) {
} catch (error) {
note(server, error);
return null;
}
}),
@@ -214,13 +224,39 @@ Future<AppUpdate> newestServerUpdate(
final available = results.whereType<AppUpdate>().toList()
..sort((a, b) => b.versionCode.compareTo(a.versionCode));
if (available.isEmpty) {
throw const FormatException(
'No compatible update could be read. Check your saved servers and sign-ins.',
throw FormatException(
problems.isEmpty
? 'No saved server offers an update.'
: 'No compatible update could be read:\n${problems.join('\n')}',
);
}
return available.first;
}
/// One failed update check in words: the reason the check itself gave
/// (sign in first, an untrusted feed key, another signing certificate), else
/// the failure's kind.
String updateProblem(Object error) => switch (error) {
FormatException(:final message) => message,
StateError(:final message) => message,
TimeoutException() => 'the server did not answer in time',
_ => _ioProblem(error),
};
/// A network failure's own words ("HttpException: Sign in first …, uri = …"
/// becomes "Sign in first …"). Portable: no dart:io types are named here.
String _ioProblem(Object error) {
var text = '$error';
final kind = RegExp(r'^[A-Za-z]*(Exception|Error): ').firstMatch(text);
if (kind != null) text = text.substring(kind.end);
final uri = text.indexOf(', uri = ');
if (uri >= 0) text = text.substring(0, uri);
if (error.runtimeType.toString() == 'SocketException') {
return 'could not connect ($text)';
}
return text.trim().isEmpty ? 'the check failed' : text.trim();
}
/// Retained offers must stop trusting an alias if its profile is edited/removed.
bool updateSourceStillTrusted(
Iterable<SavedServer> current,
@@ -233,3 +269,30 @@ bool updateSourceStillTrusted(
(endpoint) => loginUriFor(Uri.parse(endpoint)) == loginUriFor(uri),
),
);
/// Why a readable, trusted feed offers this app nothing. An Android build
/// signed with another certificate than the installed app cannot install over
/// it, so that is said plainly; anything else is an incompatible build.
String incompatibleUpdateReason(
String body, {
required String platform,
String? installedSignature,
}) {
try {
final json = jsonDecode(body);
final signer = json is Map ? json['signingCertificateSha256'] : null;
if (platform == 'android' &&
signer is String &&
installedSignature != null &&
signer.toLowerCase() !=
installedSignature.replaceAll(':', '').trim().toLowerCase()) {
return 'the offered APK is signed with a different certificate than '
'this app, so Android cannot install it over this one; install '
'it from the server\'s /updates/pipelines.apk after uninstalling '
'this app';
}
} on FormatException {
return 'the update feed could not be read';
}
return 'no compatible build is offered for this device';
}
+9 -1
View File
@@ -127,7 +127,15 @@ Future<AppUpdate> fetchLatestScoped(
signed: trusted.isNotEmpty,
);
if (update == null) {
throw const FormatException('No compatible build is offered.');
throw FormatException(
incompatibleUpdateReason(
utf8.decode(chunks),
platform: platform,
installedSignature: platform == 'android'
? (await PackageInfo.fromPlatform()).buildSignature
: null,
),
);
}
return update;
} finally {
@@ -0,0 +1,83 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/server_catalog.dart';
import 'package:pipelines_app/features/updates/app_updates.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
test('a failed check names each machine and why it offered nothing', () async {
SharedPreferences.setMockInitialValues({});
final tank = SavedServer.validated(
id: 'tank',
name: 'tank',
endpoints: ['wss://tank.test/connect'],
);
final mac = SavedServer.validated(
id: 'mac',
name: 'Mac',
endpoints: ['wss://mac.test/connect'],
);
await expectLater(
newestServerUpdate(
[tank, mac],
read: (base, _) async => base.host == 'tank.test'
? throw const FormatException(
'The update feed is signed by a key this app does not trust.',
)
: throw TimeoutException('slow'),
),
throwsA(
isA<FormatException>().having(
(e) => e.message,
'message',
allOf(
contains(
'tank: The update feed is signed by a key this app does not trust.',
),
contains('Mac: the server did not answer in time'),
),
),
),
);
});
test('an HTTP failure keeps its own words without the exception noise', () {
expect(
updateProblem(_FakeHttpException('Sign in first (Settings → Sign in).')),
'Sign in first (Settings → Sign in).',
);
});
test('another Android signing certificate is said plainly', () {
final body = jsonEncode({
'versionCode': 2,
'apk': 'pipelines.apk',
'signingCertificateSha256': 'a' * 64,
});
expect(
incompatibleUpdateReason(
body,
platform: 'android',
installedSignature: 'B' * 64,
),
contains('different certificate'),
);
expect(
incompatibleUpdateReason(
body,
platform: 'android',
installedSignature: 'A' * 64,
),
'no compatible build is offered for this device',
);
});
}
class _FakeHttpException implements Exception {
_FakeHttpException(this.message);
final String message;
@override
String toString() => 'HttpException: $message, uri = https://tank.test/x';
}