This repository was archived by the owner on Jan 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandlerMiddleware.cs
More file actions
73 lines (63 loc) · 3.05 KB
/
ExceptionHandlerMiddleware.cs
File metadata and controls
73 lines (63 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// copied from https://github.com/Azure/azure-functions-dotnet-worker/blob/main/samples/CustomMiddleware/ExceptionHandlingMiddleware.cs
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker.Middleware;
using Microsoft.Extensions.Logging;
namespace CustomMiddleware
{
/// <summary>
/// This middleware catches any exceptions during function invocations and
/// returns a response with 500 status code for http invocations.
/// </summary>
internal sealed class ExceptionHandlingMiddleware : IFunctionsWorkerMiddleware
{
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
public ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
try
{
await next(context);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing invocation");
var httpReqData = await context.GetHttpRequestDataAsync();
if (httpReqData != null)
{
// Create an instance of HttpResponseData with 500 status code.
var newHttpResponse = httpReqData.CreateResponse(HttpStatusCode.InternalServerError);
// You need to explicitly pass the status code in WriteAsJsonAsync method.
// https://github.com/Azure/azure-functions-dotnet-worker/issues/776
await newHttpResponse.WriteAsJsonAsync(new { FooStatus = "Invocation failed!" }, newHttpResponse.StatusCode);
var invocationResult = context.GetInvocationResult();
var httpOutputBindingFromMultipleOutputBindings = GetHttpOutputBindingFromMultipleOutputBinding(context);
if (httpOutputBindingFromMultipleOutputBindings is not null)
{
httpOutputBindingFromMultipleOutputBindings.Value = newHttpResponse;
}
else
{
invocationResult.Value = newHttpResponse;
}
}
}
}
private OutputBindingData<HttpResponseData> GetHttpOutputBindingFromMultipleOutputBinding(FunctionContext context)
{
// The output binding entry name will be "$return" only when the function return type is HttpResponseData
var httpOutputBinding = context.GetOutputBindings<HttpResponseData>()
.FirstOrDefault(b => b.BindingType == "http" && b.Name != "$return");
return httpOutputBinding;
}
}
}