2023-05-03 11:06:20 +02:00
== Why is this an issue?
2022-06-01 08:40:39 +02:00
The top-most level of an Azure Function code should include a try/catch block to capture and log all errors so you can monitor the health of the application effectively.
In case a retry policy has been defined for your Azure Function, you should rethrow any errors that should result in a retry.
// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2022-06-01 08:40:39 +02:00
[source,csharp]
----
[FunctionName("HttpExample")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
{
// Noncompliant
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
// do stuff
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2022-06-01 08:40:39 +02:00
[source,csharp]
----
[FunctionName("HttpExample")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
{
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
// do stuff
}
catch (Exception ex)
{
// do stuff
}
}
----
2023-05-03 11:06:20 +02:00
== Resources
2022-06-01 08:40:39 +02:00
* https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-error-pages?tabs=csharp[Azure Functions error handling and retries]
* https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-error-pages?tabs=csharp#retry-policies-preview[Azure Functions retry policies]