Skip to content

Add context to exception handler. #2236

Closed

Description

@gaffney

I propose to add the request (or some request context object) to the exception handler method signature.

exception_handler(exc) to exception_handler(request, exc)

This would cover a legitimate use case wherein the user needs contextual information to determine how to handle the error. For instance, the user might want to silently ignore harmless errors (say, requests that have 'WhiteHat Security' in the HTTP_USER_AGENT), whereas for all other requests the user would want to track the exception via analytics tools and/or bug tracking applications.

Activity

  1. jpadilla commented on Dec 9, 2014

    @jpadilla
    Contributor

    Worth noting that the last conversation about this is was at #1671.

  2. gaffney commented on Dec 9, 2014

    @gaffney
    ContributorAuthor

    Thanks, I searched and did not see that.

    I feel this is a pretty common use case for exception handling, not an edge case. Makes me sad I will have to hack around this one.

  3. lovelydinosaur commented on Dec 9, 2014

    @lovelydinosaur
    Member

    Okay let's leave this open to reconsideration. This sort of use case is more compelling than previously presented.

  4. lovelydinosaur commented on Dec 9, 2014

    @lovelydinosaur
    Member

    So (exc, request) and (exc, context) both valid options. I guess we might want to lean towards context?

  5. maryokhin commented on Dec 9, 2014

    @maryokhin
    Contributor

    To me it would make sense to call context if it would be like in the serializer, request + something else that you can pass yourself, if it's just the request, then request makes sense.

  6. lovelydinosaur commented on Dec 9, 2014

    @lovelydinosaur
    Member

    The view instance, and URL args, kwargs might also be reasonable things to put in the context.

  7. gaffney commented on Dec 10, 2014

    @gaffney
    ContributorAuthor

    Glad this is open for reconsideration. Also wanted to point out that Django's process_exception passes the request, so I would be more inclined to do it that way for the sake of consistency. Not sure how useful additional context would be, but maybe I have not thought of some use cases you have.

  8. kevin-brown commented on Dec 10, 2014

    @kevin-brown
    Contributor

    Right now in the JSON API renderers, we have to try to detect when an error response is coming through, as JSON API has a custom error format. While we can currently work around only having the exception, having additional context within the exception handler would also be useful.

    The view instance, and URL args, kwargs might also be reasonable things to put in the context.

    👍 If we could pass something similar to the serializer context that'd cover most of the bases.

  9. jpadilla commented on Dec 13, 2014

    @jpadilla
    Contributor

    Thinking about working on this. I'd stick with context and have it include view, args, kwargs and request. I was actually thinking about just "recycling" get_renderer_context(). Also, thought about just injecting context into exc. That way the signature on existing exception handlers doesn't change.

    exc.context = self.get_renderer_context()
    response = self.settings.EXCEPTION_HANDLER(exc)

    Thoughts?

  10. lovelydinosaur commented on Dec 13, 2014

    @lovelydinosaur
    Member

    No problem with adding another keyword argument to the signature - it's very easy to handle both cases during the deprecation period.

    Agree wrt the context, seems like the best approach.

  11. added this to the 3.0.2 Release milestone on Dec 15, 2014
  12. lovelydinosaur commented on Dec 15, 2014

    @lovelydinosaur
    Member

    Not sure why I milestoned for 3.0.2 - should be 3.1.0, given the API deprecation.

  13. changed the title [-]exception_handler(exc) should also take in a request / context argument[/-] [+]Add context to exception handler.[/+] on Dec 15, 2014
  14. lovelydinosaur commented on Dec 15, 2014

    @lovelydinosaur
    Member

    Now resolved in the version-3.1 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions