Skip to content
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

Exception thrown from a trigger and caught in the middleware has incorrect Source value #2265

Closed
SeanFeldman opened this issue Feb 5, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@SeanFeldman
Copy link
Contributor

Description

When an exception is thrown from a function (ASB trigger) and is caught and handled in the custom middleware, the exception Source is set incorrectly to the middleware rather than the throwing function.
image

The source (1) is the namespace rather than the location where the exception was thrown, as indicated in the StackTrace (2)

Steps to reproduce

Function

public class AsbTrigger
{
    [Function("AsbTrigger")]
    public async Task Run([ServiceBusTrigger("myqueue", Connection = "AzureServiceBus")] ServiceBusReceivedMessage message, FunctionContext context)
    {
        var logger = context.GetLogger("AsbTrigger");
        
        logger.LogInformation("C# ServiceBus queue trigger function processed message: {MessageId}", message.MessageId);
        
        throw new Exception("boom!");
    }
}

Middleware

internal class ServiceBusMiddleware(IAzureClientFactory<ServiceBusClient> azureClientFactory, ILogger<ServiceBusMiddleware> logger) : IFunctionsWorkerMiddleware
{
    private readonly ServiceBusClient asbClient = azureClientFactory.CreateClient("default");

    public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
    {
        try
        {
            await next(context);
        }
        catch (Exception exception)
        {
               logger.LogDebug("Failed at {Source}", exception.Source);
               throw;
        }
    }
}
@SeanFeldman SeanFeldman added the bug Something isn't working label Feb 5, 2024
@SeanFeldman SeanFeldman changed the title Exception from trigger has incorrect exception.Source Exception thrown from a trigger and caught in the middleware has incorrect Source value Feb 5, 2024
@SeanFeldman
Copy link
Contributor Author

This is aligned with normal exception handling. I got confused by the previous implementation where AggregateException has the source set (#993).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant