mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 18:43:42 +01:00
Report a rejected upload once per refusal, not once per row
This commit is contained in:
@@ -95,14 +95,14 @@ class DjangoConnector extends PowerSyncBackendConnector {
|
||||
/// Client for the endpoint liveness probes in [fetchCredentials].
|
||||
final http.Client _probeClient;
|
||||
|
||||
/// IDs of CRUD operations that already triggered a user-facing
|
||||
/// rejection dialog this session. Without this gate the same
|
||||
/// permanent-failure op would re-pop the dialog on every sync tick
|
||||
/// (PowerSync keeps re-driving `uploadData` until the transaction
|
||||
/// is completed, and our `transaction.complete()` only fires once
|
||||
/// the loop finishes, so we'd see the dialog at every iteration).
|
||||
/// Resets on app restart.
|
||||
final Set<String> _reportedFailedOps = {};
|
||||
/// Refusals that already triggered a user-facing dialog this session, as
|
||||
/// table, operation and the backend's answer.
|
||||
///
|
||||
/// Keyed by the refusal rather than by the row it happened on: a category
|
||||
/// the backend rejects orphans every measurement pointing at it, and one
|
||||
/// dialog per orphan is thousands of them, each one re-popping as the user
|
||||
/// dismisses the last. Resets on app restart.
|
||||
final Set<String> _reportedRejections = {};
|
||||
|
||||
/// Ceiling for one token fetch. On token expiry the SDK awaits the fetch
|
||||
/// inline in its sync loop, so a request that never answers would freeze
|
||||
@@ -393,20 +393,22 @@ class DjangoConnector extends PowerSyncBackendConnector {
|
||||
return _UploadOutcome.reject;
|
||||
}
|
||||
|
||||
/// Surfaces a permanently refused op via the global error dialog, once per op
|
||||
/// per session (a re-driven transaction would otherwise re-pop it each tick).
|
||||
/// Surfaces a permanently refused op via the global error dialog, once per
|
||||
/// refusal per session, see [_reportedRejections].
|
||||
void _reportRejection(CrudEntry op, http.Response response) {
|
||||
if (!_reportedFailedOps.add(op.id)) {
|
||||
// Already shown for this operation in the current session.
|
||||
return;
|
||||
}
|
||||
|
||||
final exception = WgerHttpException(
|
||||
response,
|
||||
source: ExceptionSource.powersync,
|
||||
context: {'table': op.table, 'op': op.op.name},
|
||||
);
|
||||
final ctx = '${op.op.name} ${op.table}';
|
||||
|
||||
if (!_reportedRejections.add('$ctx|${response.body}')) {
|
||||
// Below the exportable log level: the repeats say nothing the first line
|
||||
// did not, and thousands of them would push everything else out of it
|
||||
logger.fine('Backend rejected $ctx again: $exception');
|
||||
return;
|
||||
}
|
||||
// 200 + {error} is the expected contract (warning); other statuses are
|
||||
// unexpected (severe).
|
||||
if (response.statusCode == 200) {
|
||||
|
||||
@@ -542,6 +542,7 @@ void main() {
|
||||
group('processTransaction', () {
|
||||
late MockApiClient api;
|
||||
late DjangoConnector conn;
|
||||
late List<LogRecord> records;
|
||||
|
||||
// Mirrors PowerSync clearing the op from the local queue: set to true once
|
||||
// transaction.complete() runs.
|
||||
@@ -551,8 +552,14 @@ void main() {
|
||||
api = MockApiClient();
|
||||
conn = DjangoConnector(baseUrl: 'http://example.invalid', apiClient: api);
|
||||
completed = false;
|
||||
records = [];
|
||||
final sub = Logger.root.onRecord.listen(records.add);
|
||||
addTearDown(sub.cancel);
|
||||
});
|
||||
|
||||
Iterable<LogRecord> logLines(Level level, String needle) =>
|
||||
records.where((r) => r.level == level && r.message.contains(needle));
|
||||
|
||||
CrudTransaction txWithAll(List<CrudEntry> entries) => CrudTransaction(
|
||||
transactionId: 1,
|
||||
crud: entries,
|
||||
@@ -665,6 +672,45 @@ void main() {
|
||||
expect(completed, isTrue);
|
||||
});
|
||||
|
||||
test('the same refusal is only surfaced once, however many rows it hits', () async {
|
||||
// A category the backend refuses orphans every measurement pointing at
|
||||
// it. One dialog per orphan is thousands of them, each one re-popping as
|
||||
// the user dismisses the last, so the refusal is reported per reason.
|
||||
// The warning and the dialog sit behind the same gate.
|
||||
when(api.upsert(any)).thenAnswer(
|
||||
(_) async => http.Response(
|
||||
json.encode({'error': 'Forbidden', 'details': 'Measurement references an object'}),
|
||||
200,
|
||||
),
|
||||
);
|
||||
|
||||
for (final id in ['m1', 'm2', 'm3']) {
|
||||
await conn.processTransaction(
|
||||
txWith(CrudEntry(1, UpdateType.put, 'measurements_measurement', id, 1, {'value': 1})),
|
||||
);
|
||||
}
|
||||
|
||||
expect(logLines(Level.WARNING, 'Backend rejected'), hasLength(1));
|
||||
});
|
||||
|
||||
test('a different refusal is surfaced on its own', () async {
|
||||
when(api.upsert(any)).thenAnswer(
|
||||
(_) async => http.Response(json.encode({'error': 'Forbidden'}), 200),
|
||||
);
|
||||
await conn.processTransaction(
|
||||
txWith(CrudEntry(1, UpdateType.put, 'measurements_measurement', 'm1', 1, {'value': 1})),
|
||||
);
|
||||
|
||||
when(api.upsert(any)).thenAnswer(
|
||||
(_) async => http.Response(json.encode({'error': 'Validation failed'}), 200),
|
||||
);
|
||||
await conn.processTransaction(
|
||||
txWith(CrudEntry(1, UpdateType.put, 'measurements_category', 'c1', 1, {'unit': ''})),
|
||||
);
|
||||
|
||||
expect(logLines(Level.WARNING, 'Backend rejected'), hasLength(2));
|
||||
});
|
||||
|
||||
test('a retryable op leaves the whole transaction queued for a replay', () async {
|
||||
// The op ahead of it was already sent, so the replay re-sends it: this is
|
||||
// the at-least-once delivery the backend handlers have to be idempotent
|
||||
|
||||
Reference in New Issue
Block a user