Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

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

Closed
Eilon opened this issue Mar 5, 2017 · 5 comments
Assignees

Comments

@Eilon
Copy link
Member

Eilon commented Mar 5, 2017

From @tiefling on November 16, 2016 12:43

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();
        }
    }

Copied from original issue: dotnet/aspnetcore#1820

@Eilon
Copy link
Member Author

Eilon commented Mar 5, 2017

From @oyKris on December 13, 2016 14:12

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

@Eilon
Copy link
Member Author

Eilon commented Mar 5, 2017

From @rynowak on December 13, 2016 19:29

/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 Author

Eilon commented Mar 5, 2017

@ryanbrandenburg can you investigate and see whether there's something patch-worthy here?

@rynowak
Copy link
Member

rynowak commented Mar 6, 2017

I believe that this is #5594

@Eilon
Copy link
Member Author

Eilon commented Mar 6, 2017

Ah, this did sound super familiar to me.

@Eilon Eilon closed this as completed Mar 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants