Unknown Coder
Unknown Coder

Reputation: 1740

Error handling with Azure Function ServiceBusTrigger

I have an Azure Function as below. I came to know that ServiceBusTrigger provides a retry mechanism and by default retry five times and after that write a message to a poison queue. In this case, how to handle errors that comes within function context?

e.g. In the following code, I'm adding a product to the database and I want to handle errors for that code block using Try-Catch. Is it a better way? will it affect the standard retry mechanism? please advice.

[FunctionName("SaveProductData")]
public void Run([ServiceBusTrigger("mytopicname", Connection = "ServiceBusConnectionString")]
ProductItemUpdate message, ILogger log)
{
    var product = new Domain.Entities.Product()
    {
       ... prop init goes here..... 
    };

    log.LogInformation($"Processed Product - Sain: {message.ProductId}");
    _productRepository.Add(product);
 }

Upvotes: 1

Views: 2775

Answers (1)

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4776

Thanks to Anand Sowmithran. Posting your valuable suggestions as an answer to help other community members.

SQL Exceptions can be logged in the log source using simple ILogger that will write in App Insights.

For SQL constraint violation, Retry Mechanism is not useful. Azure will do retry only if your code fails to complete its functionality.

A retry policy is evaluated whenever an execution results in an uncaught exception. As a best practice, you should catch all exceptions in your code and rethrow any errors that should result in a retry. - From MS Docs.

Message retry is useful and applicable only for transient errors, see this page to understand for which type of scenarios retry makes sense.

Upvotes: 2

Related Questions