交互式调试器

交互式应用程序调试

Things break, and when they do, quickly pinpointing what went wrong and why makes a huge difference. By default, Pylons uses a customized version of Ian Bicking's <http://blog.ianbicking.org/>_ EvalException middleware that also includes full Myghty Traceback information.

See the Enabling Debugging section of the Getting Started <getting_started.html>_ guide to enable the interactive debugging.

调试界面

The debugging screen has three tabs at the top:

Traceback

Extra Data

Myghty

Since Myghty compiles its templates to Python modules, it can be difficult to accurately figure out what line of the template resulted in the error. The Myghty tab provides the full Myghty traceback which contains accurate line numbers for your templates, and where the error originated from. If your exception was triggered before a template was rendered, no Myghty information will be available in this section.

== 示例: 探索 Traceback

Using the interactive debugger can also be useful to gain a deeper insight into objects present only during the web request like Myghty's m object, or the session and request objects. The thing to keep in mind with some of these objects is that you won't be able to explore them through the interactive debugger unless you first use a supplied Controller method to bind them to your controller <class-pylons.controllers.Controller.html#_attach_locals>_.

To trigger an error so that we can explore what's happening just raise an exception inside an action you're curious about. In this example, we'll raise an error in the action that's used to display the page you're reading this on. Here's what the docs controller looks like:

   1     class DocsController(BaseController):
   2         def view(self, url):
   3             if request.path_info.endswith('docs'):
   4                 redirect_to('/docs/')
   5             return render_response('/docs/' + url)

Since we want to explore the session and request, we'll need to bind them first. Here's what our action now looks like with the binding and raising an exception:

#!python

}}}

Here's what exploring the Traceback from the above example looks like (Excerpt of the relevant portion):

.. image:: /img/screenshots/doctraceback.gif

Email 选项

You can make all sorts of changes to how the debugging works. For example if you disable the debug variable in the config file Pylons will email you an error report instead of displaying it as long as you provide your email address at the top of the config file:

    error_email_from = [email protected]

This is very useful for a production site. Emails are sent via SMTP so you need to specify a valid SMTP server too.

修改调试器主题

If you are using Pylons in a commercial company it is useful to be able to change the theme of the debugger so that if an error occurs, a page with your company logo appears. You might also decide to remove the Pylons logo if you use the debugger a lot so that there is more space to view the traceback.

You can change the theme by creating a new template. For example, a very simple template might look like this:

   1     my_error_template = '''
   2         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   3           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   4         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   5         <head>
   6         <title>Server Error</title>
   7         %(head)s
   8         <body id="documentation">
   9             %(extra_data)s
  10             %(myghty_data)s
  11             %(traceback_data)s
  12         </body>
  13         </html>
  14     '''

The values are automatically substituted by the error middleware. You can also add %(prefix)s which is replaced by the path to your application so you can include CSS files or images. For example if your application had a file called style.css in a directory called css within your public directory, you could add the following line to your template to ensure that the CSS file was always correctly found:

    <link rel="stylesheet" href="%(prefix)s/css/style.css" type="text/css" media="screen" />

If you want to retain the ability to switch between the different error displays you need a slightly more complicated example:

   1     my_error_template = '''
   2         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   3           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   4         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   5         <head>
   6         <title>Server Error</title>
   7         %(head)s
   8         <body id="documentation" onload="switch_display('%(set_tab)s')">
   9         <ul id="navlist">
  10           <li id='traceback_data_tab' class="active">
  11             <a href="javascript:switch_display('traceback_data')" id='traceback_data_link'>Traceback</a>
  12           </li>
  13           <li id='extra_data_tab' class="">
  14             <a href="javascript:switch_display('extra_data')" id='extra_data_link'>Extra Data</a>
  15           </li>
  16           <li id='myghty_data_tab'>
  17             <a href="javascript:switch_display('myghty_data')" id='myghty_data_link'>Myghty</a>
  18           </li>
  19         </ul>
  20         <div id="extra_data" class="hidden-data">
  21             %(extra_data)s
  22         </div>
  23         <div id="myghty_data" class="hidden-data">
  24             %(myghty_data)s
  25         </div>
  26         <div id="traceback_data">
  27             %(traceback_data)s
  28         </div>
  29         </body>
  30         </html>
  31     '''

In this case when you click on a link the relevant tab is displayed. As long as you keep the same IDs and class names, you can specify your own styles and create a theme like the one used by Pylons by default.

Now that you have a template you need to use it in your application. In config/middleware.py change the following lines:

#!python

}}}

to use your template:

   1     # @@@ Error Handling @@@
   2     my_error_template = '''
   3         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   4           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   5         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   6         <head>
   7         <title>Server Error</title>
   8         %(head)s
   9         <body id="documentation">
  10             %(extra_data)s
  11             %(myghty_data)s
  12             %(traceback_data)s
  13         </body>
  14         </html>
  15     '''
  16     app = ErrorHandler(app, global_conf, error_template=my_error_template, **config.errorware)

Your interactive debugger will now be themed with the new template.