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

Allow Invocation Filters to Short Circuit / Provide Response #1314

Open
mathewc opened this issue Aug 22, 2017 · 30 comments
Open

Allow Invocation Filters to Short Circuit / Provide Response #1314

mathewc opened this issue Aug 22, 2017 · 30 comments
Labels
Milestone

Comments

@mathewc
Copy link
Member

mathewc commented Aug 22, 2017

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)

@MikeStall
Copy link
Contributor

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.

@mathewc
Copy link
Member Author

mathewc commented Sep 12, 2017

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.

@apmorton
Copy link

+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");
        });
}

@tjgalama
Copy link

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

@mathewc
Copy link
Member Author

mathewc commented May 14, 2018

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

@mathewc mathewc closed this as completed May 14, 2018
@Gorthog
Copy link

Gorthog commented May 22, 2018

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

@paulbatum
Copy link
Member

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

@paulbatum paulbatum reopened this May 22, 2018
@mathewc
Copy link
Member Author

mathewc commented May 22, 2018

Sorry for accidentally closing this guys

@getkanchan
Copy link

+1 on a feature like this.
Any updates ?

@Gorthog
Copy link

Gorthog commented Sep 2, 2018

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.

@ginomessmer
Copy link

+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.

@patricknolan
Copy link

+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.

@adrianknight89
Copy link

adrianknight89 commented Nov 17, 2018

@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.

@artmasa
Copy link

artmasa commented Mar 30, 2019

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

@SeanFeldman
Copy link

Could really use this feature for what doing now.

@joshua-hayes
Copy link

+1 for this

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

@osnielvaldivia
Copy link

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

@geoffrussel
Copy link

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.

@kzu
Copy link

kzu commented Aug 4, 2020

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

@vijaymarada
Copy link

+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?

@MrMaheshwari
Copy link

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

@mike1880
Copy link

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

@kzu
Copy link

kzu commented Sep 25, 2020

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.

@1TheMuffinMan
Copy link

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. 🤣

@ftballguy45
Copy link

2021 and still nothing? 😖

@jooki-tech
Copy link

Much wanted feature!

@kamalkishorm
Copy link

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

@schutztj
Copy link

schutztj commented Feb 1, 2022

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

@shelbaz
Copy link

shelbaz commented Feb 4, 2022

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

@mauve
Copy link

mauve commented Jun 24, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests