azure-webjobs-sdk icon indicating copy to clipboard operation
azure-webjobs-sdk copied to clipboard

Allow Invocation Filters to Short Circuit / Provide Response

Open mathewc opened this issue 6 years ago • 30 comments

Provide a way for filters to specify a result (e.g. in a validation filter scenario, allow the filter to specify the HttpResponse for a 400 BadRequest).

As part of this we should also consider adding a way for filters to short circuit/skip the function invocation, e.g. as ASP.NET filters allow via a Cancel property on the context)

mathewc avatar Aug 22 '17 18:08 mathewc

One challenge we have beyond ASP.Net is that ASP.Net is http-specific; whereas our filters are trigger agnostic. So providing "400" on a QueueTrigger doesn't make sense. This feature is tied to the trigger mechanism. This specific example (400) will likely involve cooperation from the Http Trigger extension.

MikeStall avatar Aug 28 '17 14:08 MikeStall

In one prototype (code here) I experimented with a custom exception type FunctionResultException which allows a function or filter to specify the return value for the function (in the case of an http function the http response).

Initially I had prototyped with ASP.NET's HttpResponseException, but in .NET Core that went away. So if we want to use an exception model we'd define our own.

mathewc avatar Sep 12 '17 22:09 mathewc

+1 on a feature like this. Without the ability to provide http responses through exceptions, or otherwise wrap function execution and provide a http response it is very difficult to cleanly handle certain cross cutting validation concerns - especially when migrating code from a traditional app service to a functions app.

In the meantime, I have emulated the required behavior with minimal changes to my code by wrapping the body of each function that might throw HttpResponseException with a method like the following:

private static async Task<HttpResponseMessage> HandleHttpExceptions(Func<Task<HttpResponseMessage>> action)
{
    try
    {
        return await action();
    }
    catch (HttpResponseException e)
    {
        return e.Response;
    }
}

[FunctionName("MyFunction")]
public static async Task<HttpResponseMessage> MyFunction(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get")]
    HttpRequestMessage req,
    TraceWriter log)
{
    return await HandleHttpExceptions(async () =>
        {
            EnsureUserInRole(req, "Admin");  // this may throw HttpResponseException

            // complex logic here

            return req.CreateResponse(HttpStatusCode.OK, "ok");
        });
}

apmorton avatar Oct 30 '17 20:10 apmorton

Im currently trying to migrate our WebApi to function apps for effective granular scaling. One of the things we need to migrate are the several types of middleware (intercepting HttpRequests for authentication, authorization, exceptionpromotion). For this we think we need to use IFunctionFilter derived types .(?) Can anyone tell what the current status is regarding a solution for:

  • Short circuiting a function (cancelling sequential function filters and the function itself)
  • setting an adequate 'result' object in the FunctionExecutingContext, FunctionExecutedContext and/or FunctionExceptionContext

tjgalama avatar May 14 '18 14:05 tjgalama

The status is as the issue description states - we haven't addressed this issue yet.

mathewc avatar May 14 '18 17:05 mathewc

Why is this issue closed if it hasn't been addressed yet?

Gorthog avatar May 22 '18 10:05 Gorthog

I'm going to assume that @mathewc did not intend to close this issue and reopen.

paulbatum avatar May 22 '18 17:05 paulbatum

Sorry for accidentally closing this guys

mathewc avatar May 22 '18 17:05 mathewc

+1 on a feature like this. Any updates ?

getkanchan avatar Aug 31 '18 15:08 getkanchan

We would also like an update and if possible provide your recommended way of handling cross cutting concerns such as authentication until this feature is implemented.

Gorthog avatar Sep 02 '18 09:09 Gorthog

+1 I second that. This seems pretty essential, especially when it comes down to API services that are being built on top of Azure Functions.

ginomessmer avatar Sep 05 '18 13:09 ginomessmer

+1 - I agree that without the ability to return a certain result object, such as BadRequestObjectResult based on the exception, the FunctionExceptionFilterAttribute isn't overly useful.

patricknolan avatar Sep 27 '18 21:09 patricknolan

@MikeStall +1. No progress since 2017? I'm pretty sure many people want a feature like this. I need to send a proper response message when authentication tokens are not valid. Filters are a great fit for this purpose.

adrianknight89 avatar Nov 17 '18 14:11 adrianknight89

Take a look at this for auth purposes: https://blog.darkloop.com/post/bringing-authorizeattribute-to-net-azure-functions-v2 Yes, exceptions are thrown, but the authentication flow happens with correct status codes sent to the caller

artmasa avatar Mar 30 '19 13:03 artmasa

Could really use this feature for what doing now.

SeanFeldman avatar Apr 18 '19 23:04 SeanFeldman

+1 for this

This is a much needed feature coming across from the many additional features that dotnet core api filters provide.

joshua-hayes avatar Jun 02 '19 03:06 joshua-hayes

Will 2020 be the year we get this? This feature would reduce a lot of unnecessary boilerplate in our functions projects.

osnielvaldivia avatar Jan 22 '20 17:01 osnielvaldivia

Adding another +1 for this! I've built Web APIs using .NET and now .NET Core for years and Exception Filters make error handling so so so easy. I am dreading wrapping the code at the function level for every API endpoint as we consider moving our platform serverless.

geoffrussel avatar May 20 '20 03:05 geoffrussel

Logged https://github.com/Azure/azure-functions-host/issues/6445 just in case it gets more visibility over there. Without some way of affecting the resulting output in some way, it's not overly useful for things like validation which is mentioned explicitly as one of the scenarios in the wiki on filters

kzu avatar Aug 04 '20 21:08 kzu

+1, Even am counting on this.

So many duplicate threads on the same issue. Although IFunctionInvocationFilter, IFunctionExceptionFilter APis are obsoleted, shall We can consider using this until this ticket is addressed?

vijaymarada avatar Aug 24 '20 12:08 vijaymarada

Any ETA for this to release, as this is in high demand?

MrMaheshwari avatar Aug 27 '20 11:08 MrMaheshwari

Three years on and still no progress? This is really NOT optional, it needs doing.

mike1880 avatar Sep 24 '20 16:09 mike1880

So many duplicate threads on the same issue. Although IFunctionInvocationFilter, IFunctionExceptionFilter APis are obsoleted, shall We can consider using this until this ticket is addressed?

@vijaymarada the only reason they are marked Obsolete is that there's no equivalent [Preview] attribute they could use. My understanding from interacting with the team is that they have every intention to ship the feature as stable at some point.

kzu avatar Sep 25 '20 00:09 kzu

There's absolutely no way it's been 3 years and this still isn't a feature. You guys are like trying to come out with your own car on Azure and it's released without a defroster system for the windshield after years. 🤣

1TheMuffinMan avatar Oct 18 '20 19:10 1TheMuffinMan

2021 and still nothing? 😖

ftballguy45 avatar Feb 04 '21 16:02 ftballguy45

Much wanted feature!

jooki-tech avatar Mar 13 '21 21:03 jooki-tech

Any update on it or is there an alternative approach that can help us for now? 😖

kamalkishorm avatar May 21 '21 09:05 kamalkishorm

+1 for 2022. Could we please get an update here?

schutztj avatar Feb 01 '22 20:02 schutztj

Were now 5 years later 2017-2022 .... Neeeeed this

shelbaz avatar Feb 04 '22 19:02 shelbaz

Isn't this possible in out-of-process functions?

mauve avatar Jun 24 '22 14:06 mauve