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

Make error handler run always #761

Merged
merged 9 commits into from Feb 1, 2022

Conversation

aviramha
Copy link
Contributor

@aviramha aviramha commented Dec 18, 2019

EDIT by @Kludex : Closes #739

@aviramha
Copy link
Contributor Author

Should I add a test that checks in case a response started that the handler is still called?

@aviramha
Copy link
Contributor Author

@tomchristie Thoughts?

@florimondmanca
Copy link
Member

florimondmanca commented Jan 11, 2020

@aviramha I'm not sure I understand the use case here. Can you clarify why we'd want/need to move the call to the installed error handler separately from the other cases?

@aviramha
Copy link
Contributor Author

@aviramha I'm not sure I understand the use case here. Can you clarify why we'd want/need to move the call to the installed error handler separately from the other cases?

The use case are exception handlers not being called on background tasks since the response already started. This changes the logic so the handlers will be called even if response is returned. It's extremely important for usage of error loggers like sentry etc.

@aviramha
Copy link
Contributor Author

Any update? :)

Copy link
Sponsor Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me.

Thanks for the PR @aviramha ! Also, sorry for taking so long to act here.

I've added a test, but I'm unsure what needs to be added to the documentation, as it seems that the following lines already covers what is here:

image

Specifically, the part: "within the application".

@Kludex Kludex requested a review from a team January 29, 2022 17:57
@Kludex
Copy link
Sponsor Member

Kludex commented Jan 29, 2022

I'll wait to see if someone else wants to comment here.

@adriangb
Copy link
Member

I think this makes sense, for example if users log within their exception handlers.

My main two concerns would be:

  • How this interacts with the work to get exception handlers for websockets
  • We'll have to document the behavior. I could see users getting confused as to why the response they return from error handlers isn't used

@Kludex
Copy link
Sponsor Member

Kludex commented Jan 30, 2022

How this interacts with the work to get exception handlers for websockets

This is only for HTTP, no interaction.

@Kludex
Copy link
Sponsor Member

Kludex commented Jan 30, 2022

Docs added. Suggestions welcome about the wording, if any.

@aviramha
Copy link
Contributor Author

Looks great. Thanks @Kludex for seeing this through.

else:
response = await run_in_threadpool(self.handler, request, exc)

# This flow overrides the former response, if there was any.
if not response_started:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand the change footprint here.

From the discussion I would have thought that the change would be to move everything outside the if not response started block except await response(scope, receive, send).

(Ie. keep the behaviour the same, except for the fact that we always run the handler)

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like this:

            request = Request(scope)
            if self.debug:
                # In debug mode, return traceback responses.
                response = self.debug_response(request, exc)
            elif self.handler is None:
                # Use our default 500 error handler.
                response = self.error_response(request, exc)
            else:
                # Use an installed 500 error handler.
                if asyncio.iscoroutinefunction(self.handler):
                    response = await self.handler(request, exc)
                else:
                    response = await run_in_threadpool(self.handler, request, exc)

            if not response_started:
                await response(scope, receive, send)

?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that 👍

It doesn't make a practical difference, as the first two conditionals don't perform any logic besides sending back a response object, but I agree it's cleaner.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, yup. I think it's easier to understand that way round. (And it's a more obvious review too.)

@Kludex
Copy link
Sponsor Member

Kludex commented Feb 1, 2022

@tomchristie Would you like to get the last 👍 here?

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, yup.

@Kludex Kludex merged commit 400e240 into encode:master Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Background tasks exception handler
5 participants