Skip to content

feat(PubSub): Add SubscriberErrorListener sample #2102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pubsub/api/src/subscriber_error_listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Copyright 2025 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
*/

namespace Google\Cloud\Samples\PubSub;

# [START pubsub_subscriber_error_listener]
use Google\Cloud\PubSub\PubSubClient;

/**
* Subscribes with an error listener
*
* @param string $projectId The Google project ID.
* @param string $topicName The Pub/Sub topic name.
* @param string $subscriptionId The ID of the subscription.
*/
function subscriber_error_listener(
string $projectId,
string $topicName,
string $subscriptionId
): void {

$pubsub = new PubSubClient([
'projectId' => $projectId,
]);
$subscription = $pubsub->subscription($subscriptionId, $topicName);

try {
$messages = $subscription->pull();
foreach ($messages as $message) {
printf('PubSub Message: %s' . PHP_EOL, $message->data());
$subscription->acknowledge($message);
}
} catch (\Exception $e) { // Handle unrecoverable exceptions
printf('Exception Message: %s' . PHP_EOL, $e->getMessage());
printf('StackTrace: %s' . PHP_EOL, $e->getTraceAsString());
}
}
# [END pubsub_subscriber_error_listener]
require_once __DIR__ . '/../../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
53 changes: 53 additions & 0 deletions pubsub/api/test/pubsubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,57 @@ public function testCreateAndDeleteUnwrappedSubscription()
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
}

public function testSubscriberErrorListener()
{
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
$subscription = 'test-subscription-' . rand();

// Create subscription
$output = $this->runFunctionSnippet('create_subscription', [
self::$projectId,
$topic,
$subscription,
]);
$this->assertMatchesRegularExpression('/Subscription created:/', $output);
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);

// Publish Message
$testMessage = 'This is a test message';
$output = $this->runFunctionSnippet('publish_message', [
self::$projectId,
$topic,
$testMessage,
]);
$this->assertMatchesRegularExpression('/Message published/', $output);

// Pull messages from subscription with error listener
$output = $this->runFunctionSnippet('subscriber_error_listener', [
self::$projectId,
$topic,
$subscription
]);
// Published message should be received as expected and no exception should be thrown
$this->assertMatchesRegularExpression(sprintf('/PubSub Message: %s/', $testMessage), $output);
$this->assertDoesNotMatchRegularExpression('/Exception Message/', $output);

// Delete subscription
$output = $this->runFunctionSnippet('delete_subscription', [
self::$projectId,
$subscription,
]);
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);

// Pull messages from a non-existent subscription with error listener
$subscription = 'test-subscription-' . rand();
$output = $this->runFunctionSnippet('subscriber_error_listener', [
self::$projectId,
$topic,
$subscription
]);
// NotFound exception should be caught and printed
$this->assertMatchesRegularExpression('/Exception Message/', $output);
$this->assertMatchesRegularExpression(sprintf('/Resource not found \(resource=%s\)/', $subscription), $output);
}
}