djangosnippets.org: Latest snippets tagged with 'exception'https://djangosnippets.org/tags/exception/2011-03-06T12:40:27.569218-06:00Other approach of making middleware (by decorators)
2011-03-06T12:40:27.569218-06:00divermanhttps://djangosnippets.org/snippets/2383/<p>Other approach of making middleware. Advantage of is to specify, which middleware is used for which view function and in what order. Middleware function gets all arguments, that are passed to view function.
</p>
<p><strong>Example usage</strong>
</p>
<pre><code>@RequestMiddleware
def print_params_middleware(request, *args, **kwargs):
print 'GET params:', request.GET
@ResponseMiddleware
def modify_headers_middleware(request, response, *args, **kwargs): …</code></pre>
Freely redistributableReportBug() (exception emails - ala django debug style)
2010-09-12T09:45:48.935050-05:00sleepycalhttps://djangosnippets.org/snippets/2191/<p>ReportBug() allows you to send exception details to you, via email, but with
far more detail than the default. It uses the base function for the traceback
used by the Debug mode on Django.
</p>
<p>This is a first revision, so the emails have no decent styling, but it works,
and …</p>
Freely redistributableView decorator to convert DoesNotExist (ObjectDoesNotExist) exceptions into Http404 exceptions
2009-09-26T03:58:57.691763-05:00jammycakeshttps://djangosnippets.org/snippets/1743/<p>A decorator that can be applied to your views to turn ObjectDoesNotExist exceptions into Http404 exceptions. This means people will see a "Page not found" error rather than an "Internal Server Error" when they are request something that does not exist in the database.
</p>
<p>Example:
</p>
<pre><code>@raise_404
def show_event(request, id):
event …</code></pre>
Freely redistributablePrint Exceptions to the Console
2009-07-26T14:51:37.619312-05:00ericflohttps://djangosnippets.org/snippets/1654/<p>Put this in an <strong>init</strong>.py somewhere that will be executed on initialization and all errors will be printed out to stderr. Useful for debugging Facebook apps, javascript calls, etc.
</p>
Freely redistributableTRAC-Ticket on exception
2009-07-10T06:29:17.332134-05:00identifyhttps://djangosnippets.org/snippets/1622/<p>This is a small approach to have a middleware which automatically creates a ticket in an existing Trac environment.</p>
<p><strong>Note:</strong> you must have the <a href="http://trac-hacks.org/wiki/XmlRpcPlugin">XML-RPC-Plugin</a> installed.</p>
<p>Extend the attrs-dict to your needs. For example: in my case I have the SensitiveTicket-Plugin installed - automatically created tickets are marked as sensitive …</p>
Freely redistributableCrashKit Middleware
2009-05-04T05:31:01.204622-05:00wizhttps://djangosnippets.org/snippets/1483/<p>Catch exceptions and send it along with useful data to <a href="http://crashkitapp.appspot.com/">CrashKit</a>.</p>
Freely redistributableNo Password E-mail
2009-02-15T22:17:17.612087-06:00jefferyahttps://djangosnippets.org/snippets/1326/<p>Sometimes when a Django site's authentication backend goes down, a login will fail with a 500 error. This has happened to me when using an LDAP backend for authentication. A glitch with the settings, or ldap temporarily disappearing can make logins flake out for a short period of time.</p>
<p>That's …</p>
Freely redistributableMessage exception
2008-10-14T12:31:31.903374-05:00marinhohttps://djangosnippets.org/snippets/1143/<p>This exception is util when you want to raise an exception but want its message be shown as a message to the user, with no error 500 or 404 pages.</p>
<p>To use it, just append the middleware in the MIDDLEWARE_CLASSES setting and raises HttpMessage when necessary.</p>
Freely redistributableSuper User Conditional Page Exception Reporting
2008-07-31T14:03:04.389511-05:00zbyte64https://djangosnippets.org/snippets/935/<p><strong>Step 1</strong>
Save somewhere in your project directory</p>
<p><strong>Step 2</strong>
Add to your settings.py</p>
<pre><code>MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'utils.debug.UserBasedExceptionMiddleware',
)
</code></pre>
<p>Normal users will get your 500.html when debug = False, but If you are logged in as a super user then you get to see the stack …</p>
Freely redistributableAjax error handling
2008-06-13T11:57:10.784569-05:00kcarnoldhttps://djangosnippets.org/snippets/802/<p>Handles exceptions from AJAX code, giving more useful errors and tracebacks.</p>
<p>(By the way, all these new snippets are extracts from <a href="http://code.google.com/p/django-webapp/">django-webapp</a>.)</p>
<p>This exact code is not well tested, but it's refactored from some code we use in production.</p>
Freely redistributableexception handling middleware
2008-04-29T04:33:58.401087-05:00ageldamahttps://djangosnippets.org/snippets/732/<p><code>
is_loaded = False
if not is_loaded:
is_loaded = True
#... ExceptionHandlingMiddleware as EHM
import views
EHM.append(views.AuthFailError, views.auth_fail) # when AuthFailError thrown it redirects to <code>auth_fail</code> view function.
</code></p>
Freely redistributableLog errors to a file
2008-03-10T16:08:35.561047-05:00kcarnoldhttps://djangosnippets.org/snippets/639/<p>This is another example use of the <a href="http://www.djangosnippets.org/snippets/638/">exception middleware</a>. It shows how to log exceptions to a file. Someone wanted to do this to avoid DOS-ing the email server in case of a silly error.</p>
<p>(untested.)</p>
Freely redistributableExtensible exception handling middleware
2008-03-10T15:54:38.410531-05:00kcarnoldhttps://djangosnippets.org/snippets/638/<p>This exception middleware abstracts the functionality of the builtin exception handling mechanisms, but makes them extensible by inheritance.</p>
<p>Just add it (or some subclass) to the top of your active middleware list.</p>
<p>You can use this to <a href="http://www.djangosnippets.org/snippets/631/">make your admin emails more informative</a> or <a href="http://www.djangosnippets.org/snippets/639/">log errors to a file</a>.</p>
Freely redistributableMask sensitive POST fields in error e-mails
2008-03-09T16:37:07.656475-05:00derivinhttps://djangosnippets.org/snippets/634/<p>For PyCon we have our crash messages go to a mailman group so that people working on the site would be aware of issues. This saved us many times. But sensitive information would some times come up such as login passwords and fields we did not want going on the …</p>
Freely redistributableJSON view decorator
2008-02-28T10:52:27.940699-06:00kcarnoldhttps://djangosnippets.org/snippets/622/<p>Use this decorator on a function that returns a dict to get a JSON view, with error handling.</p>
<p>Features:</p>
<ul>
<li>response always includes a 'result' attribute ('ok' by default)</li>
<li>catches all errors and mails the admins</li>
<li>always returns JSON even on errors</li>
</ul>
Freely redistributable