Error handler middleware
Error handling middleware
error_catching_wsgi_app = ErrorMiddleware(wsgi_app)
- If true, then tracebacks will be shown in the browser.
- an email address (or list of addresses) to send exception reports to
- a filename to append tracebacks to
- If true, then errors will be printed to wsgi.errors (frequently a server error log, or stderr).
- from_address, smtp_server, error_subject_prefix, smtp_username, smtp_password, smtp_use_tls:
- variables to control the emailed exception reports
- When debug mode is off, the error message to show to users.
- When this key (default _) is in the request GET variables (not POST!), expect that this is an XMLHttpRequest, and the response should be more minimal; it should not be a complete HTML page.
- If this setting in the request environment is true, then this middleware is disabled. This can be useful in a testing situation where you don’t want errors to be caught and transformed.
- When this middleware encounters an exception listed in this environment variable and when the start_response has not yet occurred, the exception will be re-raised instead of being caught. This should generally be set by middleware that may (but probably shouldn’t be) installed above this middleware, and wants to get certain exceptions. Exceptions raised after start_response have been called are always caught since by definition they are no longer expected.
Class that represents a interactive interface. It has its own namespace. Use eval_context.exec_expr(expr) to run commands; the output of those commands is returned, as are print statements.
This is essentially what doctest does, and is taken directly from doctest.
Exception-catching middleware that allows interactive debugging.
This middleware catches all unexpected exceptions. A normal traceback, like produced by weberror.exceptions.errormiddleware.ErrorMiddleware is given, plus controls to see local variables and evaluate expressions in a local context.
This can only be used in single-process environments, because subsequent requests must go back to the same process that the exception originally occurred in. Threaded or non-concurrent environments both work.
This shouldn’t be used in production in any way. That would just be silly.
Handles capturing an exception and turning it into an interactive exception explorer
Static path where images and other files live
Relay a request to a remote machine for JS proxying
Returns a JSON-format summary of all the cached exception reports
View old exception reports
This module processes Python exceptions that relate to HTTP exceptions by defining a set of exceptions, all subclasses of HTTPException. Each exception, in addition to being a Python exception that can be raised and caught, is also a WSGI application and webob.Response object.
This module defines exceptions according to RFC 2068  : codes with 100-300 are not really errors; 400’s are client errors, and 500’s are server errors. According to the WSGI specification  , the application can call start_response more then once only under two conditions: (a) the response has not yet been sent, or (b) if the second and subsequent invocations of start_response have a valid exc_info argument obtained from sys.exc_info(). The WSGI specification then requires the server or gateway to handle the case where content has been sent and then an exception was encountered.
The HTTPException class is complicated by 4 factors:
- The content given to the exception may either be plain-text or as html-text.
- The template may want to have string-substitutions taken from the current environ or values from incoming headers. This is especially troublesome due to case sensitivity.
- The final output may either be text/plain or text/html mime-type as requested by the client application.
- Each exception has a default explanation, but those who raise exceptions may want to provide additional detail.
Subclass attributes and call parameters are designed to provide an easier path through the complications.
- the HTTP status code for the exception
- remainder of the status line (stuff after the code)
- a plain-text explanation of the error message that is not subject to environment or header substitutions; it is accessible in the template via %(explanation)s
- a plain-text message customization that is not subject to environment or header substitutions; accessible in the template via %(detail)s
- a content fragment (in HTML) used for environment and header substitution; the default template includes both the explanation and further detail provided in the message
- a plain-text override of the default detail
- a list of (k,v) header pairs
- a plain-text additional information which is usually stripped/hidden for end-users
- a string.Template object containing a content fragment in HTML that frames the explanation and further detail
To override the template (which is HTML content) or the plain-text explanation, one must subclass the given exception; or customize it after it has been created. This particular breakdown of a message into explanation, detail and template allows both the creation of plain-text and html messages for various clients as well as error-free substitution of environment variables and headers.
The subclasses of _HTTPMove (HTTPMultipleChoices, HTTPMovedPermanently, HTTPFound, HTTPSeeOther, HTTPUseProxy and HTTPTemporaryRedirect) are redirections that require a Location field. Reflecting this, these subclasses have two additional keyword arguments: location and add_slash.
- to set the location immediately
- set to True to redirect to the same URL as the request, except with a / appended
Relative URLs in the location will be resolved to absolute.
Formatters for the exception data that comes from ExceptionCollector.
Removes any frames that should be hidden, according to the values of traceback_hide, self.show_hidden_frames, and the hidden status of the final frame.
Called after each frame ends; may return None to output no text.
Called before each frame starts; may return None to output no text.
Returns true if the list contains items that are long, and should be more nicely formatted.
Formats the string as a triple-quoted string when it contains newlines.
Convert a string to HTML. Try to be really safe about it, returning a quoted version of the string if nothing else works.
Truncate the string to the limit number of characters
Like make_wrappable() but intended for text that will go in a <pre> block, so wrap on a line-by-line basis.
An exception collector that finds traceback information plus supplements
Produces a data structure that can be used by formatters to display exception reports.
If you define one of these variables in your local scope, you can add information to tracebacks that happen in that context. This allows applications to add all sorts of extra information about the context of the error, including URLs, environmental variables, users, hostnames, etc. These are the variables we look for:
You can define this locally or globally (unlike all the other variables, which must be defined locally).
__traceback_supplement__ is a tuple of (factory, arg1, arg2...). When there is an exception, factory(arg1, arg2, ...) is called, and the resulting object is inspected for supplemental information.
If set and true, this indicates that the frame should be hidden from abbreviated tracebacks. This way you can hide some of the complexity of the larger framework and let the user focus on their own errors.
By setting it to 'before', all frames before this one will be thrown away. By setting it to 'after' then all frames after this will be thrown away until 'reset' is found. In each case the frame where it is set is included, unless you append '_and_this' to the value (e.g., 'before_and_this').
Note that formatters will ignore this entirely if the frame that contains the error wouldn’t normally be shown according to these rules.
The actually interpretation of these values is largely up to the reporters and formatters.
collect_exception(*sys.exc_info()) will return an object with several attributes:
The list of frames goes innermost first. Each frame has these attributes; some values may be None if they could not be determined.
__traceback_supplement__ is thrown away, but a fixed set of attributes are captured; each of these attributes is optional.
These are used to create an object with attributes of the same names (getInfo becomes a string attribute, not a method). __traceback_supplement__ implementations should be careful to produce values that are relatively static and unlikely to cause further errors in the reporting system – any complex introspection should go in getInfo() and should ultimately return a string.
Note that all attributes are optional, and under certain circumstances may be None or may not exist at all – the collector can only do a best effort, but must avoid creating any exceptions itself.
Formatters may want to use __traceback_hide__ as a hint to hide frames that are part of the ‘framework’ or underlying system. There are a variety of rules about special values for this variables that formatters should be aware of.
More attributes in __traceback_supplement__? Maybe an attribute that gives a list of local variables that should also be collected? Also, attributes that would be explicitly meant for the entire request, not just a single frame. Right now some of the fixed set of attributes (e.g., source_url) are meant for this use, but there’s no explicit way for the supplement to indicate new values, e.g., logged-in user, HTTP referrer, environment, etc. Also, the attributes that do exist are Zope/Web oriented.
More information on frames? cgitb, for instance, produces extensive information on local variables. There exists the possibility that getting this information may cause side effects, which can make debugging more difficult; but it also provides fodder for post-mortem debugging. However, the collector is not meant to be configurable, but to capture everything it can and let the formatters be configurable. Maybe this would have to be a configuration value, or maybe it could be indicated by another magical variable (which would probably mean ‘show all local variables below this frame’)
This represents one frame of the exception. Each frame is a context in the call stack, typically represented by a line number and module name in the traceback.
Return the source of the current line of this frame. You probably want to .strip() it as well, as it is likely to have leading whitespace.
If context is given, then that many lines on either side will also be returned. E.g., context=1 will give 3 lines.
Collection an exception from sys.exc_info().
try: blah blah except: exc_data = collect_exception(*sys.exc_info())