""" adapted for deluge-webui: -edit-box with traceback for cut+paste. -pretty errors for well known exceptions. debugerror.py : This is adapted from Django with modifications Copyright (C) Martijn Voncken 2008 Copyright (c) 2005, the Lawrence Journal-World Used under the modified BSD license: http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5 """ __all__ = ["debugerror", "djangoerror"] import utils pretty_errors_str = { "org.freedesktop.DBus.Error.ServiceUnknown": """ Webui Lost the connection to deluge
Unable to reconnect, please restart deluge. """, "InvalidUniqueIDError:": """ this torrent was removed, click here to go to the torrent-list """ } pretty_errors_cls = { type(utils.UnknownTorrentError):""" this torrent was removed, click here to go to the torrent-list """ } import sys, urlparse, pprint from web import websafe from web import template import web #import lib.webpy022.webapi as web import webserver_common as ws from traceback import format_tb from deluge import common Template = template.Template import os, os.path whereami = os.path.join(os.getcwd(), __file__) whereami = os.path.sep.join(whereami.split(os.path.sep)[:-1]) djangoerror_t = """\ $def with (exception_type, exception_value, frames, exception_message, version_info, tback_txt) $exception_type at $ctx.path

$exception_type : $exception_value

Oops, Deluge Broke :-( , You might have found a bug, or you did something really stupid ;-).
If the error persists :
Read the Faq.
Try downloading the latest version at deluge-torrent.org
Visit the forum or the buglist for more info.

Paste the contents of this text-box when you are asked for a traceback.
Try to explain what you where doing, and how you could work around the problem.
Don't paste without context and expect us to know what went wrong.

Use a pastebin on IRC!

Traceback (innermost first)

    $for frame in frames:
  • $frame.filename in $frame.function $if frame.context_line:
    $if frame.pre_context:
      $for line in frame.pre_context:
    1. $line
    1. $frame.context_line ...
    $if frame.post_context:
      $for line in frame.post_context:
    1. $line
    $if frame.vars:
    Local vars $# $inspect.formatargvalues(*inspect.getargvalues(frame['tb'].tb_frame))
    $:dicttable(frame.vars, kls='vars', id=('v' + str(frame.id)))
$if ctx.output or ctx.headers:

Response so far

HEADERS

$for kv in ctx.headers: $kv[0]: $kv[1]
$else: [no headers]

BODY

$ctx.output

Request information

INPUT

$:dicttable(web.input()) $:dicttable(web.cookies())

META

$ newctx = [] $# ) and (k not in ['env', 'output', 'headers', 'environ', 'status', 'db_execute']): $for k, v in ctx.iteritems(): $if not k.startswith('_') and (k in x): $newctx.append(kv) $:dicttable(dict(newctx))

ENVIRONMENT

$:dicttable(ctx.env)
""" dicttable_t = r"""$def with (d, kls='req', id=None) $if d: $ temp = d.items() $temp.sort() $for kv in temp:
VariableValue
$kv[0]
$prettify(kv[1])
$else:

No data.

""" dicttable_r = Template(dicttable_t, filter=websafe) djangoerror_r = Template(djangoerror_t, filter=websafe) def djangoerror(): def _get_lines_from_file(filename, lineno, context_lines): """ Returns context_lines before and after lineno from file. Returns (pre_context_lineno, pre_context, context_line, post_context). """ try: source = open(filename).readlines() lower_bound = max(0, lineno - context_lines) upper_bound = lineno + context_lines pre_context = \ [line.strip('\n') for line in source[lower_bound:lineno]] context_line = source[lineno].strip('\n') post_context = \ [line.strip('\n') for line in source[lineno + 1:upper_bound]] return lower_bound, pre_context, context_line, post_context except (OSError, IOError): return None, [], None, [] exception_type, exception_value, tback = sys.exc_info() exception_message = 'Error' try: exception_message = exception_value.message except AttributeError: exception_message = 'no message' exception_type = exception_type.__name__ """ for err_str in pretty_errors: if err_str in exception_message: #from render import render return render.error(pretty_errors[err_str]) """ if exception_type in pretty_errors_cls: from render import render return render.error(pretty_errors_cls[exception_type]) version_info = "WebUi : %sr%s\nPython %s:" % ( common.get_version() ,common.get_revision(),sys.version) try: import dbus version_info += '\ndbus:' + str(dbus.__version__) except: pass tback_txt = ''.join(format_tb(tback)) frames = [] while tback is not None: filename = tback.tb_frame.f_code.co_filename function = tback.tb_frame.f_code.co_name lineno = tback.tb_lineno - 1 pre_context_lineno, pre_context, context_line, post_context = \ _get_lines_from_file(filename, lineno, 7) frames.append(web.storage({ 'tback': tback, 'filename': filename, 'function': function, 'lineno': lineno, 'vars': tback.tb_frame.f_locals, 'id': id(tback), 'pre_context': pre_context, 'context_line': context_line, 'post_context': post_context, 'pre_context_lineno': pre_context_lineno, })) tback = tback.tb_next frames.reverse() urljoin = urlparse.urljoin def prettify(x): try: out = pprint.pformat(x) except Exception, e: out = '[could not display: <' + e.__class__.__name__ + \ ': '+str(e)+'>]' return out dt = dicttable_r dt.globals = {'prettify': prettify} t = djangoerror_r t.globals = {'ctx': web.ctx, 'web':web, 'dicttable':dt, 'dict':dict, 'str':str} return t(exception_type, exception_value, frames, exception_message, version_info, tback_txt) def deluge_debugerror(): """ A replacement for `internalerror` that presents a nice page with lots of debug information for the programmer. (Based on the beautiful 500 page from [Django](http://djangoproject.com/), designed by [Wilson Miner](http://wilsonminer.com/).) """ web.ctx.headers = [ ('Content-Type', 'text/html') ] web.ctx.output = djangoerror() if __name__ == "__main__": urls = ( '/', 'index' ) class index: def GET(self): thisdoesnotexist web.internalerror = web.debugerror web.run(urls)