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

Custom Exception Filters are unable to update the result, returning a 200 Status Code and an empty page #1820

Closed
tiefling opened this issue Nov 16, 2016 · 3 comments

Comments

@tiefling
Copy link

I have created a custom Exception filter which looks like this...

    public class EntityNotFoundResultHandlerAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            if (!(context.Exception is ArgumentOutOfRangeException)) return;

            context.ExceptionHandled = true;
            context.Result = new NotFoundResult();
        }
    }

I changed it to this, hoping for it to either blow up or loop itself, but the end result was the same...

    public class EntityNotFoundResultHandlerAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            if (!(context.Exception is ArgumentOutOfRangeException)) return;

            context.ExceptionHandled = true;
            context.Result = new ViewResult { ViewName = "ImAFish" };
        }
    }

This is being used within an action like this...

        [EnsureServicePortalContext]
        [EntityNotFoundResultHandler]
        public async Task<IActionResult> Details(Guid id)
        {
            var model = await employerViewModelBuilder.Build(id);

            if (session.CurrentPortalContext.CurrentContextId != null && !model.LinkedServiceOperatorIDs.Contains(session.CurrentPortalContext.CurrentContextId.Value))
                return new NotFoundResult();

            return View(model);
        }

I have stepped through the code to validate that the OnException method of the filter is being run (it runs fine) and to check that code execution is not returning to the Action (it doesn't).

If I remove the exception handled line then the filter executes twice then the expected result (a 404) is returned...

    public class EntityNotFoundResultHandlerAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            if (!(context.Exception is ArgumentOutOfRangeException)) return;

            //context.ExceptionHandled = true;
            context.Result = new NotFoundResult();
        }
    }
@oyKris
Copy link

oyKris commented Dec 13, 2016

We had the same issue and found a solution to this problem until it is fixed. Add the following line at the end of the code:

context.Result.ExecuteResultAsync(context);

I don't know if this is a good idea but it seems to work and give the right result

@rynowak
Copy link
Member

rynowak commented Dec 13, 2016

/cc @Eilon - something funky is going on with 1.1.0 and exception filters - we made changes in this area for 1.1.0 so we should have someone investigate this as a potential patch candidate.

@Eilon
Copy link
Member

Eilon commented Mar 5, 2017

This issue was moved to aspnet/Mvc#5901

@Eilon Eilon closed this as completed Mar 5, 2017
natemcmaster pushed a commit that referenced this issue Nov 14, 2018
* More FrameConnection refactoring
- This change reverts the change to complete the writer with an
exception on abort because of the number of first chance exceptions
that get thrown.
- This change also moves connection logging into FrameConnection instead
of being split between the ConnectionHandler and FrameConnection.
- Fixed issues with LibuvOutputConsumerTests that leak WriteReq since
cancelled writes no longer end the connection.
Tratcher added a commit that referenced this issue Feb 14, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants