Skip to content

Commit 2073a8a

Browse files
authored
feat(PubSub): Add SubscriberErrorListener sample (#2102)
1 parent 9e871a4 commit 2073a8a

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_subscriber_error_listener]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Subscribes with an error listener
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $subscriptionId The ID of the subscription.
36+
*/
37+
function subscriber_error_listener(
38+
string $projectId,
39+
string $topicName,
40+
string $subscriptionId
41+
): void {
42+
43+
$pubsub = new PubSubClient([
44+
'projectId' => $projectId,
45+
]);
46+
$subscription = $pubsub->subscription($subscriptionId, $topicName);
47+
48+
try {
49+
$messages = $subscription->pull();
50+
foreach ($messages as $message) {
51+
printf('PubSub Message: %s' . PHP_EOL, $message->data());
52+
$subscription->acknowledge($message);
53+
}
54+
} catch (\Exception $e) { // Handle unrecoverable exceptions
55+
printf('Exception Message: %s' . PHP_EOL, $e->getMessage());
56+
printf('StackTrace: %s' . PHP_EOL, $e->getTraceAsString());
57+
}
58+
}
59+
# [END pubsub_subscriber_error_listener]
60+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,57 @@ public function testCreateAndDeleteUnwrappedSubscription()
509509
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
510510
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
511511
}
512+
513+
public function testSubscriberErrorListener()
514+
{
515+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
516+
$subscription = 'test-subscription-' . rand();
517+
518+
// Create subscription
519+
$output = $this->runFunctionSnippet('create_subscription', [
520+
self::$projectId,
521+
$topic,
522+
$subscription,
523+
]);
524+
$this->assertMatchesRegularExpression('/Subscription created:/', $output);
525+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
526+
527+
// Publish Message
528+
$testMessage = 'This is a test message';
529+
$output = $this->runFunctionSnippet('publish_message', [
530+
self::$projectId,
531+
$topic,
532+
$testMessage,
533+
]);
534+
$this->assertMatchesRegularExpression('/Message published/', $output);
535+
536+
// Pull messages from subscription with error listener
537+
$output = $this->runFunctionSnippet('subscriber_error_listener', [
538+
self::$projectId,
539+
$topic,
540+
$subscription
541+
]);
542+
// Published message should be received as expected and no exception should be thrown
543+
$this->assertMatchesRegularExpression(sprintf('/PubSub Message: %s/', $testMessage), $output);
544+
$this->assertDoesNotMatchRegularExpression('/Exception Message/', $output);
545+
546+
// Delete subscription
547+
$output = $this->runFunctionSnippet('delete_subscription', [
548+
self::$projectId,
549+
$subscription,
550+
]);
551+
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
552+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
553+
554+
// Pull messages from a non-existent subscription with error listener
555+
$subscription = 'test-subscription-' . rand();
556+
$output = $this->runFunctionSnippet('subscriber_error_listener', [
557+
self::$projectId,
558+
$topic,
559+
$subscription
560+
]);
561+
// NotFound exception should be caught and printed
562+
$this->assertMatchesRegularExpression('/Exception Message/', $output);
563+
$this->assertMatchesRegularExpression(sprintf('/Resource not found \(resource=%s\)/', $subscription), $output);
564+
}
512565
}

0 commit comments

Comments
 (0)