#34427: Improve error message for bad context processors
-------------------------------------------+------------------------
               Reporter:  Adam Johnson     |          Owner:  nobody
                   Type:  New feature      |         Status:  new
              Component:  Template system  |        Version:  dev
               Severity:  Normal           |       Keywords:
           Triage Stage:  Unreviewed       |      Has patch:  0
    Needs documentation:  0                |    Needs tests:  0
Patch needs improvement:  0                |  Easy pickings:  0
                  UI/UX:  0                |
-------------------------------------------+------------------------
 If you write a template context processor but return the wrong type, for
 example `None` by missing the return statement:

 {{{
 def data(request):
     data = {"something": True}
 }}}

 Then currently the error message is very mysterious:

 {{{
 Internal Server Error: /
 Traceback (most recent call last):
   ...
   File "/..../site-packages/django/template/backends/django.py", line 61,
 in render
     return self.template.render(context)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/..../site-packages/django/template/base.py", line 173, in render
     with context.bind_template(self):
   File "/.../lib/python3.11/contextlib.py", line 137, in __enter__
     return next(self.gen)
            ^^^^^^^^^^^^^^
   File "/..../site-packages/django/template/context.py", line 254, in
 bind_template
     updates.update(processor(self.request))
 TypeError: 'NoneType' object is not iterable
 }}}

 If a view returns the wrong type, Django raises a nice error message:

 {{{
 The view example.views.index didn't return an HttpResponse object. It
 returned None instead.
 }}}

 I suggest we do the same for context processors. If we use try/except
 around the `updates.update()` line, it will not slow down normal
 processing any noticeable amount, thanks to Python 3.11's “zero-cost”
 exception handling: https://docs.python.org/3.11/whatsnew/3.11.html#misc

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34427>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070187049cdbe2-91ab6e34-a258-4546-853e-4c3834fc421d-000000%40eu-central-1.amazonses.com.

Reply via email to