Skip to content

Commit 569810c

Browse files
Gracefully handle failure to acknowledge a pubsub message. (#5015)
If the pubsub message has expired for whatever reason, we should catch the exception and continue with the rest of the code path.
1 parent 24a0c28 commit 569810c

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

auto_submit/lib/request_handling/pubsub.dart

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,21 @@ class PubSub {
5959
///
6060
/// The PubSub system can remove the relevant messages from the subscription.
6161
Future<void> acknowledge(String subscription, String ackId) async {
62-
final Client httpClient = await clientViaApplicationDefaultCredentials(
63-
scopes: <String>[pubsub.PubsubApi.pubsubScope],
64-
);
65-
final pubsubApi = pubsub.PubsubApi(httpClient);
66-
final ackIds = <String>[ackId];
67-
final acknowledgeRequest = pubsub.AcknowledgeRequest(ackIds: ackIds);
68-
await pubsubApi.projects.subscriptions.acknowledge(
69-
acknowledgeRequest,
70-
'${Config.pubsubSubscriptionsPrefix}/$subscription',
71-
);
62+
try {
63+
final Client httpClient = await clientViaApplicationDefaultCredentials(
64+
scopes: <String>[pubsub.PubsubApi.pubsubScope],
65+
);
66+
final pubsubApi = pubsub.PubsubApi(httpClient);
67+
final ackIds = <String>[ackId];
68+
final acknowledgeRequest = pubsub.AcknowledgeRequest(ackIds: ackIds);
69+
await pubsubApi.projects.subscriptions.acknowledge(
70+
acknowledgeRequest,
71+
'${Config.pubsubSubscriptionsPrefix}/$subscription',
72+
);
73+
} catch (e) {
74+
log.error(
75+
'Failed to acknowledge message $ackId for subscription $subscription: $e',
76+
);
77+
}
7278
}
7379
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2026 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:auto_submit/request_handling/pubsub.dart';
6+
import 'package:test/test.dart';
7+
8+
void main() {
9+
group('PubSub', () {
10+
test('acknowledge handles exceptions gracefully', () async {
11+
const pubsub = PubSub();
12+
// This should not throw even if credentials are missing or API fails.
13+
await expectLater(
14+
pubsub.acknowledge('test-sub', 'test-ack-id'),
15+
completes,
16+
);
17+
});
18+
});
19+
}

0 commit comments

Comments
 (0)