How can I get uncaught exceptions to appear in the Django log - django

When there are uncaught exceptions in my Django project the log only shows a simple "HTTP 500" message. The HTTP response will contain the full stack trace as HTML but this is not very useful when I am developing web services not being consumed by a web browser. How can I get the full stack trace of uncaught exceptions to appear in the Django log?
I have tried installing custom middleware with a "process_exception" method and custom signals handler for the "got_request_exception" event but neither of these handlers ever got invoked.
I am using Django 1.6.1 and Tastypie 0.11.0 and I'm running in debug mode.

In your django settings set:
DEBUG_PROPAGATE_EXCEPTIONS = True
TASTYPIE_FULL_DEBUG = True
Setting both of these options will allow Tastypie exceptions to propagate upwards.
https://docs.djangoproject.com/en/3.2/ref/settings/#debug-propagate-exceptions
http://django-tastypie.readthedocs.org/en/latest/settings.html#tastypie-full-debug

Related

django error reporting request url - how to use this locally?

I have a django project which used the normal email admins on an unhandled exception when debug is set to False. ie in production.
I normally just review the error message and the stack trace. However I clicked on the request url link, which managed to recreate the error on the prouduction site (which then fired off another email).
What is this request url? Does it recreate the full http request (including the session etc..) which resulted in the original error?
Can I get the link to point to a local version of the site? (As after fixing a previous error clicking on the earlier request url has manged to create a recent error that we have been unable to reproduce, so it would be good to recreate this locally so it can be debugged.

How to ignore flasks http 400 exception in Sentry

I have setup a flask project in Sentry but have noticed a problem that I need to fix.
Currently if the flask application throws a HTTPException (for example for a validation exception) that exception creates an issue in Sentry. This clutters up the issues since it creates issues even for HTTP 400.
Are there any way to configure Sentry so it ignores all HTTPExceptions with code 4xx but still create Issues for all HTTPExceptions with code 5xx?
If I’m not mistaken, Sentry should only send not handled exceptions. So you could setup custom error handlers: http://flask.pocoo.org/docs/1.0/patterns/errorpages/?highlight=error%20handler#error-handlers
If you want to handle all default exceptions you could register an error handler like this:
from werkzeug.exceptions import default_exceptions
def register_error_handlers(app):
""" Register error handler for default exceptions """
for code in default_exceptions:
app.register_error_handler(code, your_error_handler)
where app is your flask App instance and your_error_handler takes the error as argument and returns a response. So you could filter for the 400 codes in this for loop to only handle 4xx errors.

Removing powered by Jetty

I am using Jetty server with embedded web app. However, whenever I hit any resource which is not present it serves a default page which shows a message "Powered by Jetty".
This page is being served from org.eclipse.jetty.server.handler.DefaultHandler.handle.
I want write a custom handler for this, however while trying to register custom Handler in jetty.xml file, I am getting syntax exception and server doesn't start anymore.
I was able to resolve this issue by doing some tweaks along with changes mentioned in Deactivate Jetty's default 404 error handler.

How can I make hoptoad in Django 1.2 log errors to log file as well as send airbrake notifications?

We're using django 1.2.7 and the hoptoad logging middleware (hoptoad.middleware.HoptoadNotifierMiddleware).
When an error occurs, the error message gets sent to airbrake, but it does not seem to get logged to the django log file. It seems like it's intercepted by hoptoad exclusively.
Does anyone know how to also get the errors logged to the local django log file on disk? I couldn't find any such option in the hoptoad documentation.
Is there a custom modification needed to the hoptoad middleware class?

Catching Django Errors When Client Is Not A Web Browser?

I'm building a Django web service that is called from an application. When it throws an exception, I can't see the Django debug page, and can't get to it because the calling application doesn't behave like a web browser (and I don't have control over that application).
Is there a way to redirect the Django error page to a a log file rather than to the calling client, possibly via changing the FastCGI config (I'm using lighty + FastCGI)? Or maybe a "dump to file" config option or some sort of LogExceptionToFile() method within the framework itself?
You might try just creating custom ExceptionMiddleware. Just change the process_exception method to log the exception and request data somewhere.
Here's an example: http://www.peterbe.com/plog/who-was-logged-in-during-a-django-exception
If the exception in the django app is not caught, and DEBUG = True, then the exception should be sent to the client.
Some options to help you get debugging info:
Enable and configure logging
Set up email error reporting
Use something like Wireshark to inspect the HTTP request and responses.