Retry on error

Stay organized with collections Save and categorize content based on your preferences.

Tells your function whether or not to retry execution when an error happens.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


using CloudNative.CloudEvents;
using Google.Cloud.Functions.Framework;
using Google.Events.Protobuf.Cloud.PubSub.V1;
using Microsoft.Extensions.Logging;
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Retry
    public class Function : ICloudEventFunction<MessagePublishedData>
        private readonly ILogger _logger;

        public Function(ILogger<Function> logger) =>
            _logger = logger;

        public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)
            bool retry = false;
            string text = data.Message?.TextData;

            // Get the value of the "retry" JSON parameter, if one exists.
            if (!string.IsNullOrEmpty(text))
                JsonElement element = JsonSerializer.Deserialize<JsonElement>(data.Message.TextData);

                retry = element.TryGetProperty("retry", out var property) &&
                    property.ValueKind == JsonValueKind.True;

            // Throwing an exception causes the execution to be retried.
            if (retry)
                throw new InvalidOperationException("Retrying...");
                _logger.LogInformation("Not retrying...");
            return Task.CompletedTask;


// Package tips contains tips for writing Cloud Functions in Go.
package tips

import (

// PubSubMessage is the payload of a Pub/Sub event.
// See the documentation for more details:
type PubSubMessage struct {
	Data []byte `json:"data"`

// RetryPubSub demonstrates how to toggle using retries.
func RetryPubSub(ctx context.Context, m PubSubMessage) error {
	name := string(m.Data)
	if name == "" {
		name = "World"

	// A misconfigured client will stay broken until the function is redeployed.
	client, err := MisconfiguredDataClient()
	if err != nil {
		log.Printf("MisconfiguredDataClient (retry denied):  %v", err)
		// A nil return indicates that the function does not need a retry.
		return nil

	// Runtime error might be resolved with a new attempt.
	if err = FailedWriteOperation(client, name); err != nil {
		log.Printf("FailedWriteOperation (retry expected): %v", err)
		// A non-nil return indicates that a retry is needed.
		return err

	return nil


import functions.eventpojos.PubsubMessage;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.logging.Logger;

public class RetryPubSub implements BackgroundFunction<PubsubMessage> {
  private static final Logger logger = Logger.getLogger(RetryPubSub.class.getName());

  // Use Gson ( to parse JSON content.
  private static final Gson gson = new Gson();

  public void accept(PubsubMessage message, Context context) {
    String bodyJson = new String(
        Base64.getDecoder().decode(message.getData()), StandardCharsets.UTF_8);
    JsonElement bodyElement = gson.fromJson(bodyJson, JsonElement.class);

    // Get the value of the "retry" JSON parameter, if one exists
    boolean retry = false;
    if (bodyElement != null && bodyElement.isJsonObject()) {
      JsonObject body = bodyElement.getAsJsonObject();

      if (body.has("retry") && body.get("retry").getAsBoolean()) {
        retry = true;

    // Retry if appropriate
    if (retry) {
      // Throwing an exception causes the execution to be retried
      throw new RuntimeException("Retrying...");
    } else {"Not retrying...");


 * Background Cloud Function that demonstrates
 * how to toggle retries using a promise
 * @param {object} event The Cloud Functions event.
 * @param {object} Data included with the event.
 * @param {object} User-supplied parameter that tells the function whether to retry.
exports.retryPromise = event => {
  const tryAgain = !!;

  if (tryAgain) {
    throw new Error('Retrying...');
  } else {
    console.error('Not retrying...');
    return Promise.resolve();

 * Background Cloud Function that demonstrates
 * how to toggle retries using a callback
 * @param {object} event The Cloud Functions event.
 * @param {object} Data included with the event.
 * @param {object} User-supplied parameter that tells the function whether to retry.
 * @param {function} callback The callback function.
exports.retryCallback = (event, callback) => {
  const tryAgain = !!;
  const err = new Error('Error!');

  if (tryAgain) {
    console.error('Retrying:', err);
  } else {
    console.error('Not retrying:', err);


use Google\CloudFunctions\CloudEvent;

function tipsRetry(CloudEvent $event): void
    $cloudEventData = $event->getData();
    $pubSubData = $cloudEventData['message']['data'];

    $json = json_decode(base64_decode($pubSubData), true);

    // Determine whether to retry the invocation based on a parameter
    $tryAgain = $json['some_parameter'];

    if ($tryAgain) {
         * Functions with automatic retries enabled should throw exceptions to
         * indicate intermittent failures that a retry might fix. In this
         * case, a thrown exception will cause the original function
         * invocation to be re-sent.
        throw new Exception('Intermittent failure occurred; retrying...');

     * If a function with retries enabled encounters a non-retriable
     * failure, it should return *without* throwing an exception.
    $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');
    fwrite($log, 'Not retrying' . PHP_EOL);


from import error_reporting

error_client = error_reporting.Client()

def retry_or_not(data, context):
    """Background Cloud Function that demonstrates how to toggle retries.

        data (dict): The event payload.
        context ( The event metadata.
        None; output is written to Stackdriver Logging

    # Retry based on a user-defined parameter
    try_again ='retry') is not None

        raise RuntimeError('I failed you')
    except RuntimeError:
        if try_again:
            raise  # Raise the exception and try again
            pass   # Swallow the exception and don't retry


require "functions_framework"

FunctionsFramework.cloud_event "retry_or_not" do |event|
  try_again =["retry"]

    # Simulate a failure
    raise "I failed!"
  rescue RuntimeError => e
    logger.warn "Caught an error: #{e}"
    if try_again
      # Raise an exception to return a 500 and trigger a retry. "Trying again..."
      raise ex
      # Return normally to end processing of this event. "Giving up."

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.