Re: Getting app instance; hooking exceptions; log detail

40 views
Skip to first unread message

Daniel Norton

unread,
Oct 9, 2012, 6:55:22 PM10/9/12
to mod...@googlegroups.com
Soon after posting that I realized that the exception isn't in my app's source, but in mod_wsgi, complaining of the header values after the app exited, so there is no source line.

I found the offending header content and fixed the problem, but I'm not sure what happened to cause Unicode values to be returned in various places in this server config with Python 2.7, vs the other server config with Python 2.6...

Graham Dumpleton

unread,
Oct 9, 2012, 6:55:55 PM10/9/12
to mod...@googlegroups.com
On 10 October 2012 05:37, Daniel Norton <dano...@gmail.com> wrote:
> There's probably a shorter path to my solution, so here's some background:
>
> I'm no Python expert, but neither is the creator of the app, and I
> understand it better than he does.
> Somebody switched the app from a CENTOS server running Python 2.6 to an
> Ubuntu server running Python 2.7
> App crashes on new server
> Error log doesn't show where the error is, only this:
>
> mod_wsgi (pid=21820): Exception occurred processing WSGI script
> '/var/www/.../myscript'.
> TypeError: expected byte string object for header value, value of type
> unicode found

This is because your WSGI application is returning a response header
where the value is a Unicode string. All response header values must
be normal byte strings.

In mod_wsgi 3.X, the validation is potentially only done at the time
headers are being flushed I think so lost context. In mod_wsgi 4.X I
may from memory be validating when start_response() called and
throwing an error back to the application. Can't remember right now.

> I think I can use log_exception(), but I need the instance of the handler to
> hook it

Don't think that will help. May need a WSGI middleware wrapper which
provides a start_response() which validates headers and raises an
exception immediately.

> I surmise that mod_wsgi is acting as if it's creating an instance of the
> server, then calling handlerInstance.run(myapp)

If I understand what you are saying, no. It only makes use of the
'application' object and calls it for each request. There is no
calling of any mainloop by way of a run() function provided by your
application or framework.

> Is there an easier way to extract line number information out of the server?
> (currently: LogLevel info)
> Else, is there a better way to get the stack trace other than hooking
> log_exception?
> Else, what's a better way to hook log_exception?
> Else, how do I grab the instance of the handler class?

Anyway, just saw your response, but will send this anyway.


Graham
Reply all
Reply to author
Forward
0 new messages