Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #643 (closed defect: fixed)

Opened 11 years ago

Last modified 10 years ago

[PATCH] better error recovery when returning None from TG function

Reported by: dalke@… Owned by: anonymous
Priority: normal Milestone: 0.9a2
Component: TurboGears Version:
Severity: normal Keywords:
Cc: kevin

Description

With

class Root(controllers.RootController):
    @turbogears.expose(template="dasypus.templates.welcome")
    def index(self):
        return

(that is, returning None instead of a dictionary) I get

500 Internal error

The server encountered an unexpected condition which prevented it from fulfilling the request.

Page handler: <bound method Root.index of <dasypus.controllers.Root object at 0x20e2ff0>>
Traceback (most recent call last):
  File "/usr/local/lib/python2.4/site-packages/CherryPy-2.2.0betadev_r980-py2.4.egg/cherrypy/_cphttptools.py", line 99, in _run
    self.main()
  File "/usr/local/lib/python2.4/site-packages/CherryPy-2.2.0betadev_r980-py2.4.egg/cherrypy/_cphttptools.py", line 248, in main
    body = page_handler(*virtual_path, **self.params)
  File "<string>", line 3, in index
  File "/usr/local/lib/python2.4/site-packages/TurboGears-0.9a1-py2.4.egg/turbogears/controllers.py", line 210, in expose
    tg_format, html, fragment, *args, **kw)
  File "/usr/local/lib/python2.4/site-packages/TurboGears-0.9a1-py2.4.egg/turbogears/database.py", line 193, in run_with_transaction
    retval = func(*args, **kw)
  File "/usr/local/lib/python2.4/site-packages/TurboGears-0.9a1-py2.4.egg/turbogears/controllers.py", line 233, in _execute_func
    return _process_output(tg_format, output, html, fragment)
  File "/usr/local/lib/python2.4/site-packages/TurboGears-0.9a1-py2.4.egg/turbogears/controllers.py", line 83, in _process_output
    output = unicodechars.sub(
TypeError: expected string or buffer

What about adding a check? - if the returned value is None then give a better error message.

Attachments

controllers.py.diff Download (591 bytes) - added by simon 11 years ago.
Assert output is a (base)string.
controllers.py.2.diff Download (1.1 KB) - added by simon 11 years ago.
Assert valid outout with informative message.
controllers.py.3.diff Download (1.1 KB) - added by simon 11 years ago.
Same as above, minus syntax error.

Change History

comment:1 Changed 11 years ago by dalke@…

Strangely, the error occurs when I make the request from Safari but not from Firefox. It looks like the problem arises in special-case code in TurboGears-0.9a1-py2.4.egg/turbogears/controllers.py

    if ua.browser == "safari":
        if isinstance(output, str):
            output = output.decode("utf8")
        output = unicodechars.sub(
            lambda m: "&#x%x;" % ord(m.group(1)), output).encode("ascii")

Changed 11 years ago by simon

Assert output is a (base)string.

comment:2 Changed 11 years ago by simon

  • Type changed from enhancement to defect
  • Component changed from CherryPy to TurboGears
  • Milestone set to 0.9

[Thinking out loud:] Is there any sane situation in which output would be something else than string or dict?

comment:3 Changed 11 years ago by godoy

Just for safety, I'd allow a list as well... But I don't have any use case with it.

comment:4 Changed 11 years ago by dalke@…

"Is there any sane situation in which output would be something else than string or dict?"

There's also dictionary-like objects, perhaps where some keys are determined only when requested.

comment:5 Changed 11 years ago by simon

  • Cc kevin added

The thing is, right now we only handle dicts (and subclasses) and (base)strings. For anything else behaviour is more or less undefined (e.g. depending on browser used).

Kevin what say you?

comment:6 Changed 11 years ago by kevin

I think it's fine for an exception to be raised when something other than a string or dict gets returned. The only problem with the current behavior is that you have no idea what returned the illegal value. That's what really needs to be improved here.

Changed 11 years ago by simon

Assert valid outout with informative message.

comment:7 Changed 11 years ago by simon

  • Summary changed from better error recovery when returning None from TG function to [PATCH] better error recovery when returning None from TG function

Will a message:

Method ClassName?.method_name returned unexpected output. Output should be of type basestring or dict.

do?

Changed 11 years ago by simon

Same as above, minus syntax error.

comment:8 Changed 11 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed
  • Milestone changed from 0.9 to 0.9a2

Committed in [1010]. Thanks!

Note: See TracTickets for help on using tickets.