2

I've set up a Django project hosted on Dreamhost using Passenger WSGI according to their wiki instructions and ran into the problem of internal errors popping up and crashing mod_wsgi.

I installed Python Paste by following their instructions to get comprehensive logs displayed in the webpage, but it doesn't work. IE: Anytime I have internal errors, I still get django's "internal error" page without any other information in the browser.

When I tested paste's install by removing "test" in front of the "def testapplication", that worked fine. But whenever I have an internal error, I still don't get paste's output with complete error information. Instead I get the same cryptic internal error page with no information.

According to their wiki, my configuration is correct:

My passenger_wsgi.py file:

import os, sys
import django.core.handlers.wsgi

INTERP = os.path.join(os.environ['HOME'], 'env', 'bin', 'python')
if sys.executable != INTERP:
    os.execl(INTERP, INTERP, *sys.argv)

os.environ['DJANGO_SETTINGS_MODULE'] = "abc.settings"

sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.getcwd(), 'abc'))
log = file('/home/abcadmin/passengerwsgi.log', 'a')
print >>log, "Running %s" % (sys.executable)

application = django.core.handlers.wsgi.WSGIHandler()

from paste.exceptions.errormiddleware import ErrorMiddleware
sys.stdout = sys.stderr

# remove the "test" below to test paste
def testapplication(environ, start_response):
    status = '200 OK'
    output = 'Hello World! Running Python version ' + sys.version + '\n\n'
    response_headers = [('Content-type', 'text/plain'),
               ('Content-Length', str(len(output)))]
    # to test paste's error catching prowess, uncomment the following line
    # while this function is the "application"
    raise("error")
    start_response(status, response_headers)    
    return [output]

application = ErrorMiddleware(application, debug=True)

My web server error log (error.log) is empty, and my access log (access.log) is full of messages like the following (latest line - edited with bogus links). There's logging happening somewhere, because the access.log is logging something, but the errors aren't showing up anywhere - except in that cryptic "internal error" page.

46.246.117.3 - - [21/Feb/2012:00:27:04 -0800] "GET /admin/ HTTP/1.1" 200 2326 "http://abc.acme.com/admin/userProfile/profile/16/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20100101 Firefox/10.0.2"

The Django project is running well on my dev environment, but there's some kind of server problem happening when it's running off Passenger WSGI on the server - I installed Paste to try and debug these errors. When my install runs into any kind of non-Django error, I get that cryptic "internal error" message.

serpah
  • 348
  • 2
  • 9

2 Answers2

1

If you have an compiler error somewhere, it probably will not even get to the point where it knows you want to use the paste error handler. Have a look at your error log.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
  • If you remove the "test" to enable it; but also leave out the `ErrorMiddleware`, does it work? If so, I think we have ruled out the paste stuff, right? I figure it might expect a `WSGIHandler` instead of a single method (`testapplication`) – Has QUIT--Anony-Mousse Feb 22 '12 at 07:51
  • removed the "test", also left out ErrorMiddleware, and the paste output is showing. Put the ErrorMiddleware back in and it's back to where it was – serpah Mar 07 '12 at 09:03
  • I guess a django WSGIHandler and your method called `application` just aren't API compatible. Try making "application" an actual class, derived from `BaseHandler` or `WSGIHandler`. I figure that `ErrorMiddleware` expects a complete WSGI handler as first parameter. – Has QUIT--Anony-Mousse Mar 07 '12 at 16:15
  • According to the config file, application is a WSGIHandler(). – serpah Mar 14 '12 at 02:37
  • In *your* code, `application`, once you remove the `test` is a plain method, not a `WSGIHandler`. – Has QUIT--Anony-Mousse Mar 14 '12 at 07:12
  • Turns out that paste was working fine, just that I had no errors that were triggering it. – serpah May 15 '12 at 08:28
0

I too have failed with paste in this context, but for problems with the wsgi setup on a server--- this matter could be about such a problem--- see Debugging Techniques which is an "official" modwsgi doc I think. Do a search in that document for "For more complicated problems" and try out the free middlware code there that is immediately below that paragraph--- but correct the syntax error in pprint.pprint((status, headers)+args), stream=self.__oheaders) by replacing it with pprint.pprint(((status, headers)+args), stream=self.__oheaders).

I also changed the classes to "new classes" by adding "(object)" to the class declaration... sorry but I forget now if that's meaningfull or not.

With that middleware you'll get full request and response headers and content, as it all happens going to and from your application.

I have also found it appropriate to use Djano's built-in Python logging enhancements, which are implemented using the LOGGING setting in settings.py. Here are the docs.

Mike O'Connor
  • 2,494
  • 1
  • 15
  • 17
  • It's been 2 years since I looked at this issue, and I seem to recall using LOGGING in settings.py – serpah Oct 31 '14 at 16:04