Refactor: class-based error views - #3439
Conversation
status_code attribute used by child classes to ensure the correct response infer template_name from status_code if not provided apply common method decorators in dispatch
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
function handler still required for CSRF_FAILURE_VIEW setting
function handler still required for handler500 override
only active in the local environment
BaseErrorView needs to implement post to avoid a 405 Method Not Allowed and actually trigger the CSRF error handler
we should return the correct error code here instead of 400
821865f to
5c97744
Compare
lalver1
left a comment
There was a problem hiding this comment.
This looks really good! While in general the required changes turned out to be relatively straightforward, there's a good bit going on in this PR (for example CSRF failures and Django specific requirements on the function signature of a view) so thanks for the notes 👍 they helped a lot with the review! I also tested locally and saw the expected templates/response codes.
I only had a general question about the templates that are shown for each type of HTTP error. For responses that return 400 and 403 errors, we return the 400.html template which alludes to the service for the site being down. I think the text in the template is meant to be generic to cover a bad request (400), a request for a forbidden resource (403), and a request that is rejected by the CSRF protection (403), but for most of these cases the issue would not originate with Benefits but with the client (browser) itself, correct?
Yes I think this is correct, we wanted something more generic to let the user know something is wrong, without trying to diagnose exactly what might be wrong and provide potentially misleading or inaccurate information. This all came out of the error pages work that Sarah initially did (maybe 2 years ago? Or more...) Since we didn't attempt change the actual language in this PR, I think we just leave everything as-is and perhaps review in another round with @indexing and @cmajel, maybe in concert with some ideas we've had around user-facing outage alerts/notifications. |
Closes #3391
It turns out this was pretty straightforward, so instead of writing follow-up tickets to implement each of the error handlers, this PR just does it.
The Django error handlers (e.g.
handler400) expect:For function-based views, we inherently have a callable to assign to these props (the function view itself). But this is also exactly the purpose of a CBV
ViewClass.as_view()helper function:So just like our standard URL path registrations for other CBVs that make use of the
.as_view()helper, these error handlers can do the same thing!There is a minor caveat for 2 of the error handlers:
handler500is very strict about the function signature, and.as_view()doesn't pass this strictness testCSRF_FAILURE_VIEWis defined insettings.py, where we don't want to import a view class (to avoid circular imports)In both cases, we define a small helper function that simply wraps
.as_view().What this PR does
dispatchwith our common Middlewares forViewedPageEventandIndexOrAgencyIndexOriginto reduce duplicationstatus_codeattribute for the eventualHttpResponseTemplateViewand optionally calculates thetemplate_namefrom thestatus_codefor easier inheritancebenefits.viewslocalonly) for testing error handlersReviewing
/test400(400)/test403(403)/test404(404)/test500(500)/testcsrf(click to submit the form and trigger the handler - 403)